public ActionResult UpdateItem(Guid id, UpdatedItemDto itemDto) { var existingItem = repository.GetItem(id); if (existingItem is null) { return(NotFound()); } Item updatedItem = existingItem with { Name = itemDto.Name, Price = itemDto.Price }; repository.UpdateItem(updatedItem); return(NoContent()); }
public async Task <ActionResult> UpdateProductRecord(uint id, [FromBody] UpdatedItemDto updatedData) { try { if (updatedData == null) { return(StatusCode(400)); } if (!ModelState.IsValid) { return(new InvalidInputResponse(ModelState)); } await _Mediator.Send(updatedData); return(StatusCode(204)); } catch (NotFoundException e) { return(StatusCode(404, e.Message)); } }
public IActionResult UpdateItem([FromBody] UpdatedItemDto updatedItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var itemToUpdate = _itemsRepository.GetItem(updatedItem.Id); if (itemToUpdate != null) { if (string.IsNullOrEmpty(updatedItem.Title)) { return(BadRequest("Title missing")); } if (updatedItem.Cost == 0) { return(BadRequest("Unacceptable item cost")); } /* * if (updatedItem.ItemTraits != null) * { * var itemTraits = itemToUpdate.ItemTraits.ToList(); * itemTraits.RemoveAll(x => !updatedItem.ItemTraits.Select(y => y.TraitId).Contains(x.TraitId)); * var newTraits = updatedItem.ItemTraits.Where(x => !itemTraits.Select(y => y.TraitId).Contains(x.TraitId)).ToList(); * itemTraits.AddRange(newTraits); * itemToUpdate.ItemTraits = itemTraits; * }*/ /* * if (updatedItem.ItemCategories != null) * { * var itemCategories = itemToUpdate.ItemCategories.ToList(); * itemCategories.RemoveAll(x => !updatedItem.ItemCategories.Select(y => y.CategoryId).Contains(x.CategoryId)); * var newCategories = updatedItem.ItemCategories.Where(x => !itemCategories.Select(y => y.CategoryId).Contains(x.CategoryId)).ToList(); * itemCategories.AddRange(newCategories); * itemToUpdate.ItemCategories = itemCategories; * } */ if (itemToUpdate.Cost != updatedItem.Cost) { itemToUpdate.Cost = updatedItem.Cost; } if (itemToUpdate.Description != updatedItem.Description) { itemToUpdate.Description = updatedItem.Description; } if (itemToUpdate.PictureLocation != updatedItem.PictureLocation) { itemToUpdate.PictureLocation = updatedItem.PictureLocation; } if (itemToUpdate.Title != updatedItem.Title) { itemToUpdate.Title = updatedItem.Title; } _itemsRepository.Update(itemToUpdate); return(Ok("Item updated successfully")); } return(NotFound("Item does not exist in the database")); }