public async Task <ActionResult> PostAsync([FromForm] ProductSaveRequest request) { if (!ModelState.IsValid) { return(BadRequest(new ErrorResponse { Error = ModelState.GetErrorMessages(), Status = false })); } Product product = mapper.Map <ProductSaveRequest, Product>(request); ProductStatusResponse response = await productsService.SaveAsync(request, product); if (response.Status) { return(Ok(mapper.Map <Product, ProductResponse>(response.Resource))); } return(BadRequest(new ErrorResponse { Error = response.Message, Status = response.Status })); }
public async Task <ProductStatusResponse> SaveAsync(ProductSaveRequest request, Product product) { try { if ((await categoryRepository.FindByIdAsync(request.CategoryId)) == null) { return(new ProductStatusResponse("Invald Category Id.")); } for (int i = 0; i < request.Images.Count; i++) { product.Images.Add(new ImagePath { Filename = await fileService.StoreImage(config.GetSection("ProductsImages").Value, request.Images[i]) }); } Product newAddedProduct = await productsRepository.AddAsync(product); return(new ProductStatusResponse(newAddedProduct)); } catch (Exception e) { return(new ProductStatusResponse(e.Message)); } }