public async Task <Response <GetAllProductsViewModel> > Handle(UpdateProductCommand command, CancellationToken cancellationToken) { var product = await _productRepository.GetByIdAsync(command.Id); if (product == null) { throw new ApiException($"Product Not Found."); } else { product.Name = command.Name; product.Price = command.Price; product.Description = command.Description; product.Image = command.Image; product.ProductCategoryId = command.ProductCategoryId; product.Barcode = command.Barcode; await _productRepository.UpdateAsync(product); var result = await _unitOfWork.Commit(cancellationToken); if (result > 0) { var productsViewModel = _mapper.Map <GetAllProductsViewModel>(product); var productCategory = await _productCategoryRepository.GetByIdAsync(productsViewModel.ProductCategoryId); var productCategoryViewModel = _mapper.Map <GetAllProductCategoryViewModel>(productCategory); productsViewModel.ProductCategory = productCategoryViewModel; return(new Response <GetAllProductsViewModel>(productsViewModel)); } return(new Response <GetAllProductsViewModel>(null)); } }
public async Task <IActionResult> UpdateAsync(string id, [FromBody] Product product) { if (product == null || product.Id != id) { return(BadRequest()); } bool updated = await productRepository.UpdateAsync(product); if (!updated) { return(NotFound()); } return(new NoContentResult()); }
public async Task <Response <int> > Handle(UpdateProductCommand command, CancellationToken cancellationToken) { var product = await _productRepository.GetByIdAsync(command.Id); if (product == null) { throw new ApiException($"Product Not Found."); } else { product.Name = command.Name; product.Rate = command.Rate; product.Description = command.Description; await _productRepository.UpdateAsync(product); return(new Response <int>(product.Id)); } }
public async Task <Result <int> > Handle(UpdateProductCommand command, CancellationToken cancellationToken) { var product = await _productRepository.GetByIdAsync(command.Id); if (product == null) { return(Result <int> .Failure($"Product with Id {command.Id} does not exist.")); } else { product.Rate = command.Rate; product.IsActive = command.IsActive; product.Name = command.Name; await _productRepository.UpdateAsync(product); return(Result <int> .Success(product.Id)); } }
public async Task <Response <int> > Handle(DeleteProductByIdCommand command, CancellationToken cancellationToken) { var product = await _productRepository.GetByIdAsync(command.Id); if (product == null) { throw new ApiException($"Product Not Found."); } if (command.Physical) { await _productRepository.DeleteAsync(product); } else { product.IsDeleted = true; await _productRepository.UpdateAsync(product); } await _unitOfWork.Commit(cancellationToken); return(new Response <int>(product.Id)); }
public async Task <Response <UpdateProductDto> > Handle(UpdateProductCommand request, CancellationToken cancellationToken) { var command = _mapper.Map <UpdateProductRequest>(request); var product = await _ProductRepository.GetByIdAsync(command.Id); if (product == null) { throw new ApiException($"Product Not Found."); } else { product.Name = command.Name; product.Description = command.Description; product.Price = command.Price; product.Stock = command.Stock; product.ImageUrl = command.ImageUrl; await _ProductRepository.UpdateAsync(product); return(new Response <UpdateProductDto>(_mapper.Map <UpdateProductDto>(product), "Product Update Successufull")); } }