public async Task <ActionResult <PutProductDTO> > PutProduct(string id, PutProduct putProduct) { var productToPut = await _context.Products.FindAsync(id); PutProduct putProductModel = putProduct; if (productToPut == null) { return(BadRequest(this.GetError(Error.PRODUCT_NOT_FOUND))); } if (!this.ValidTokenAdmin(putProductModel.AccessToken)) { return(BadRequest(this.GetError(Error.INVALID_TOKEN))); } if (putProductModel.Name != null && putProductModel.Name.Length > 0) { productToPut.Name = putProduct.Name; } if (putProductModel.Type != null && putProductModel.Type.Length > 0) { productToPut.Type = putProduct.Type; } if (putProductModel.Description != null && putProductModel.Description.Length > 0) { productToPut.Description = putProduct.Description; } if (putProductModel.ImagePath != null && putProductModel.ImagePath.Length > 0) { productToPut.ImagePath = putProduct.ImagePath; } if (putProductModel.Price != null && putProductModel.Price.Length > 0) { productToPut.Price = Convert.ToDecimal(putProductModel.Price); } if (putProductModel.Status != null && putProductModel.Status.Length > 0) { productToPut.Status = putProduct.Status; } _context.Entry(productToPut).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } PutProductDTO putProductDTO = new PutProductDTO(productToPut); return(putProductDTO); }
public async Task <ApiResponse> Put(int id, [FromBody] PutProductDTO arg) { try { await productServiceService.UpdateAsync(id, arg); return(new ApiResponse(InfoMessages.ProductUpdated, null, HttpStatusCode.OK.ToInt())); } catch (ValidationException ex) { throw new ApiException(ex.Errors, ex.StatusCode); } catch (CustomException ex) { throw new ApiException(ex, ex.StatusCode); } catch (Exception ex) { throw new ApiException(ex); } }