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-10 16:49:42 -07:00
|
|
|
using API.Dtos;
|
|
|
|
using AutoMapper;
|
2022-05-11 13:18:34 -07:00
|
|
|
using API.Errors;
|
2022-05-09 14:41:15 -07:00
|
|
|
|
|
|
|
namespace API.Controllers
|
|
|
|
{
|
2022-05-11 12:04:56 -07:00
|
|
|
public class ProductsController : BaseApiController
|
2022-05-09 14:41:15 -07:00
|
|
|
{
|
2022-05-10 15:45:47 -07:00
|
|
|
private readonly IGenericRepository<Product> _productsRepo;
|
|
|
|
private readonly IGenericRepository<ProductBrand> _productBrandRepo;
|
|
|
|
private readonly IGenericRepository<ProductType> _productTypeRepo;
|
2022-05-10 16:49:42 -07:00
|
|
|
private readonly IMapper _mapper;
|
|
|
|
public ProductsController(IGenericRepository<Product> productsRepo, IGenericRepository<ProductBrand> productBrandRepo, IGenericRepository<ProductType> productTypeRepo, IMapper mapper)
|
2022-05-09 14:41:15 -07:00
|
|
|
{
|
2022-05-10 16:49:42 -07:00
|
|
|
_mapper = mapper;
|
2022-05-10 15:45:47 -07:00
|
|
|
_productTypeRepo = productTypeRepo;
|
|
|
|
_productBrandRepo = productBrandRepo;
|
|
|
|
_productsRepo = productsRepo;
|
|
|
|
|
2022-05-09 14:41:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
2022-05-10 16:49:42 -07:00
|
|
|
public async Task<ActionResult<IReadOnlyList<ProductToReturnDto>>> GetProducts()
|
2022-05-09 14:41:15 -07:00
|
|
|
{
|
2022-05-10 15:45:47 -07:00
|
|
|
var spec = new ProductsWithTypesAndBrandsSpecification();
|
|
|
|
var products = await _productsRepo.ListAsync(spec);
|
2022-05-10 16:49:42 -07:00
|
|
|
return Ok(_mapper.Map<IReadOnlyList<Product>, IReadOnlyList<ProductToReturnDto>>(products));
|
2022-05-09 14:41:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
2022-05-11 13:18:34 -07:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
|
2022-05-10 16:49:42 -07:00
|
|
|
public async Task<ActionResult<ProductToReturnDto>> GetProduct(int id)
|
2022-05-09 14:41:15 -07:00
|
|
|
{
|
2022-05-10 15:45:47 -07:00
|
|
|
var spec = new ProductsWithTypesAndBrandsSpecification(id);
|
2022-05-10 16:49:42 -07:00
|
|
|
var product = await _productsRepo.GetEntityWithSpec(spec);
|
2022-05-11 13:18:34 -07:00
|
|
|
if (product == null) return NotFound(new ApiResponse(404));
|
2022-05-10 16:49:42 -07:00
|
|
|
return _mapper.Map<Product, ProductToReturnDto>(product);
|
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
|
|
|
}
|
|
|
|
}
|