public async Task<ActionResult<ProductToReturnDto>> GetProduct(int id) { ProductsWithBrandsAndTypesSpec spec = new ProductsWithBrandsAndTypesSpec(id); var product = _mapper.Map<Product, ProductToReturnDto>(await _uow.Repository<Product>().GetEntityBySpecAsync(spec)); if (product == null) return NotFound(new ApiResponse(404)); return product; }
public async Task<ActionResult<Pagination<ProductToReturnDto>>> GetProductsAsync([FromQuery]ProductSpecParam productSpecParam) { ProductsWithBrandsAndTypesSpec spec = new ProductsWithBrandsAndTypesSpec(productSpecParam); ProuductsWithFilterCountSpec countProductsAfterFilter = new ProuductsWithFilterCountSpec(productSpecParam); int count = await _uow.Repository<Product>().GetCountAsync(countProductsAfterFilter); var productDto = _mapper.Map<IReadOnlyList<Product>, IReadOnlyList<ProductToReturnDto>>(await _uow.Repository<Product>().GetAllBySpecAsync(spec)); Pagination<ProductToReturnDto> pagination = new Pagination<ProductToReturnDto>(productSpecParam.PageSize, productSpecParam.PageIndex, count, productDto); return Ok(pagination); }
public async Task <ActionResult <ProductToReturnDto> > GetProduct(int id) { ProductsWithBrandsAndTypesSpec spec = new ProductsWithBrandsAndTypesSpec(id); var product = _mapper.Map <Product, ProductToReturnDto>(await _repo.GetEntityBySpecAsync(spec)); if (product == null) { return(NotFound(new ApiResponse(404))); } return(product); }
public async Task <ActionResult <Product> > GetProduct(int id) { var spec = new ProductsWithBrandsAndTypesSpec(id); var product = await _uow.Repository <Product>().GetEntityWithSpec(spec); if (product == null) { return(NotFound(new ApiResponse(404))); } return(Ok(_mapper.Map <Product, ProductToReturnDto>(product))); }
public async Task <ActionResult <List <Product> > > GetProducts([FromQuery] ProductSpecParams productParams) { var spec = new ProductsWithBrandsAndTypesSpec(productParams); var countSpec = new ProductsWithFiltersForCountSpec(productParams); var totalItems = await _uow.Repository <Product>().CountAsync(countSpec); var products = await _uow.Repository <Product>().ListAsync(spec); var data = _mapper.Map <IReadOnlyList <Product>, IReadOnlyList <ProductToReturnDto> >(products); return(Ok(new Pagination <ProductToReturnDto>(productParams.PageIndex, productParams.PageSize, totalItems, data))); }
public async Task<ActionResult<ProductToReturnDto>> UpdateProductPrice(int id, decimal price) { if (price <= 0) return BadRequest(new ApiResponse(400, "Price should be greater than 0")); ProductsWithBrandsAndTypesSpec spec = new ProductsWithBrandsAndTypesSpec(id); var product = await _uow.Repository<Product>().GetEntityBySpecAsync(spec); if (product == null) return NotFound(new ApiResponse(404)); product.Price = price; _uow.Repository<Product>().Update(product); int count = await _uow.Complete(); if (count <= 0) return BadRequest(new ApiResponse(400, "Error occured while updating product")); return _mapper.Map<Product, ProductToReturnDto>(product); }