using Core.Entities; using Microsoft.AspNetCore.Mvc; using Core.Interfaces; using Core.Specifications; using API.Dtos; using AutoMapper; using API.Errors; using API.Helpers; namespace API.Controllers { public class ProductsController : BaseApiController { private readonly IGenericRepository _productsRepo; private readonly IGenericRepository _productBrandRepo; private readonly IGenericRepository _productTypeRepo; private readonly IMapper _mapper; public ProductsController(IGenericRepository productsRepo, IGenericRepository productBrandRepo, IGenericRepository productTypeRepo, IMapper mapper) { _mapper = mapper; _productTypeRepo = productTypeRepo; _productBrandRepo = productBrandRepo; _productsRepo = productsRepo; } [HttpGet] public async Task>>> GetProducts([FromQuery]ProductSpecParams productParams) { var spec = new ProductsWithTypesAndBrandsSpecification(productParams); var countSpec = new ProductWithFiltersForCountSpecifications(productParams); var totalItems = await _productsRepo.CountAsync(countSpec); var products = await _productsRepo.ListAsync(spec); var data = _mapper.Map, IReadOnlyList>(products); return Ok(new Pagination(productParams.PageIndex, productParams.PageSize, totalItems, data)); } [HttpGet("{id}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)] public async Task> GetProduct(int id) { var spec = new ProductsWithTypesAndBrandsSpecification(id); var product = await _productsRepo.GetEntityWithSpec(spec); if (product == null) return NotFound(new ApiResponse(404)); return _mapper.Map(product); } [HttpGet("brands")] public async Task>> GetProductBrands() { return Ok(await _productBrandRepo.ListAllAsync()); } [HttpGet("types")] public async Task>> GetProductTypes() { return Ok(await _productTypeRepo.ListAllAsync()); } } }