Sky.Net/API/Controllers/ProductsController.cs

51 lines
1.7 KiB
C#
Raw Normal View History

2022-05-09 14:41:15 -07:00
using Core.Entities;
using Microsoft.AspNetCore.Mvc;
2022-05-09 17:07:59 -07:00
using Core.Interfaces;
2022-05-10 15:45:47 -07:00
using Core.Specifications;
2022-05-09 14:41:15 -07:00
namespace API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
2022-05-10 15:45:47 -07:00
private readonly IGenericRepository<Product> _productsRepo;
private readonly IGenericRepository<ProductBrand> _productBrandRepo;
private readonly IGenericRepository<ProductType> _productTypeRepo;
public ProductsController(IGenericRepository<Product> productsRepo, IGenericRepository<ProductBrand> productBrandRepo, IGenericRepository<ProductType> productTypeRepo)
2022-05-09 14:41:15 -07:00
{
2022-05-10 15:45:47 -07:00
_productTypeRepo = productTypeRepo;
_productBrandRepo = productBrandRepo;
_productsRepo = productsRepo;
2022-05-09 14:41:15 -07:00
}
[HttpGet]
public async Task<ActionResult<List<Product>>> GetProducts()
{
2022-05-10 15:45:47 -07:00
var spec = new ProductsWithTypesAndBrandsSpecification();
var products = await _productsRepo.ListAsync(spec);
2022-05-09 14:41:15 -07:00
return Ok(products);
}
[HttpGet("{id}")]
public async Task<ActionResult<Product>> GetProduct(int id)
{
2022-05-10 15:45:47 -07:00
var spec = new ProductsWithTypesAndBrandsSpecification(id);
return await _productsRepo.GetEntityWithSpec(spec);
2022-05-09 14:41:15 -07:00
}
2022-05-10 10:26:43 -07:00
[HttpGet("brands")]
public async Task<ActionResult<IReadOnlyList<ProductBrand>>> GetProductBrands()
{
2022-05-10 15:45:47 -07:00
return Ok(await _productBrandRepo.ListAllAsync());
2022-05-10 10:26:43 -07:00
}
[HttpGet("types")]
public async Task<ActionResult<IReadOnlyList<ProductType>>> GetProductTypes()
{
2022-05-10 15:45:47 -07:00
return Ok(await _productTypeRepo.ListAllAsync());
2022-05-10 10:26:43 -07:00
}
2022-05-09 14:41:15 -07:00
}
}