Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Ecommerce.API.davetn657/Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Ecommerce.API.davetn657.Data;
using Ecommerce.API.davetn657.Data.Dtos;
using Ecommerce.API.davetn657.Services;
using Ecommerce.API.davetn657.Models;
using Microsoft.AspNetCore.Mvc;

namespace Ecommerce.API.davetn657.Controllers;

[ApiController]
[Route("api/[controller]")]
public class CategoriesController : ControllerBase
{
private readonly ICategoryService _categoryService;

public CategoriesController(ICategoryService categoryServices)
{
_categoryService = categoryServices;
}

[HttpGet]
public ActionResult<PagedResponse<CategoriesDto>> AllCategories()
{
return Ok(_categoryService.AllCategories(new PaginationParams()));
}
[HttpGet("id/{id}")]
public ActionResult<CategoriesDto> CategoryById(int id)
{
var result = _categoryService.CategoryById(id);

if (result == null) return NotFound();

return Ok(result);
}
[HttpPost]
public ActionResult<CategoriesDto> CreateCategory(CategoriesDto category)
{
return Ok(_categoryService.CreateCategory(category));
}
[HttpDelete("{id}")]
public ActionResult<string> DeleteCategory(int id)
{
var result = _categoryService.DeleteCategory(id);

if (result == null) return NotFound();

return Ok(result);
}
}
41 changes: 41 additions & 0 deletions Ecommerce.API.davetn657/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Ecommerce.API.davetn657.Data;
using Ecommerce.API.davetn657.Data.Dtos;
using Ecommerce.API.davetn657.Services;
using Ecommerce.API.davetn657.Models;
using Microsoft.AspNetCore.Mvc;


namespace Ecommerce.API.davetn657.Controllers;

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly IProductService _productService;

public ProductsController(IProductService productService)
{
_productService = productService;
}
[HttpGet]
public ActionResult<PagedResponse<ProductsDto>> AllProducts()
{
return Ok(_productService.AllProducts(new PaginationParams()));
}
[HttpGet("category/{category}")]
public ActionResult<PagedResponse<ProductsDto>> ProductsByCategory(string category)
{
return Ok(_productService.ProductsByCategory( category, new PaginationParams()));
}
[HttpGet("id/{id}")]
public ActionResult<ProductsDto> ProductById(int id)
{
return Ok(_productService.ProductById(id));
}
[HttpPost]
public ActionResult<ProductsDto> CreateProduct(ProductsDto product)
{
return Ok(_productService.CreateProduct(product));
}

}
35 changes: 35 additions & 0 deletions Ecommerce.API.davetn657/Controllers/SalesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Ecommerce.API.davetn657.Data;
using Ecommerce.API.davetn657.Data.Dtos;
using Ecommerce.API.davetn657.Models;
using Ecommerce.API.davetn657.Services;
using Microsoft.AspNetCore.Mvc;

namespace Ecommerce.API.davetn657.Controllers;

[ApiController]
[Route("api/[controller]")]
public class SalesController : ControllerBase
{
private readonly ISaleService _saleService;

public SalesController(ISaleService saleService)
{
_saleService = saleService;
}

[HttpGet]
public ActionResult<PagedResponse<Sale>> AllSales()
{
return Ok(_saleService.AllSales(new PaginationParams()));
}
[HttpGet("id/{id}")]
public ActionResult<Sale> SaleById(int id)
{
return Ok(_saleService.SaleById(id));
}
[HttpPost]
public ActionResult<SalesResponseDto> CreateSale(CreateSalesDto sale)
{
return Ok(_saleService.CreateSale(sale));
}
}
8 changes: 8 additions & 0 deletions Ecommerce.API.davetn657/Data/Categories.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Ecommerce.API.davetn657.Data;

public class Category
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public DateTime CreateDate { get; set; }
}
15 changes: 15 additions & 0 deletions Ecommerce.API.davetn657/Data/DatabaseContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;

namespace Ecommerce.API.davetn657.Data;

public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions options) : base(options)
{

}

public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Sale> Sales { get; set; }
}
7 changes: 7 additions & 0 deletions Ecommerce.API.davetn657/Data/Dtos/CategoriesDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Ecommerce.API.davetn657.Data.Dtos;

public class CategoriesDto
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
18 changes: 18 additions & 0 deletions Ecommerce.API.davetn657/Data/Dtos/ProductsDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Ecommerce.API.davetn657.Data.Dtos;

public class ProductsDto
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
public string Category { get; set; } = string.Empty;
public List<SalesDto> Sales { get; set; } = [];
}

public class ProductsSalesDto
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
public string Category { get; set; } = string.Empty;
}
20 changes: 20 additions & 0 deletions Ecommerce.API.davetn657/Data/Dtos/SalesDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Ecommerce.API.davetn657.Data.Dtos;

public class SalesDto
{
public int Id { get; set; }
public List<int> ProductIds { get; set; } = [];
public decimal Total { get; set; }
}

public class CreateSalesDto
{
public List<int> ProductIds { get; set; } = [];
}

public class SalesResponseDto
{
public int Id { get; set; }
public decimal Total { get; set; }
public List<ProductsSalesDto> Products { get; set; } = [];
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading