public ActionResult UpdateItem(Guid id, ItemForUpdate item) { Item itemToUpdate = _warehouseInventoryRepository.GetItem(id); if (itemToUpdate == null) { return(NotFound()); } _mapper.Map(item, itemToUpdate); _warehouseInventoryRepository.UpdateItem(itemToUpdate); _warehouseInventoryRepository.Save(); return(NoContent()); }
// PUT: api/Items/5 public JsonResult PutItem(int id, ItemForUpdate item) { Item itemToEdit = db.Items.Find(id); string message = ""; if (itemToEdit == null) { message = "Item does not exist"; } itemToEdit.Posted = DateTime.Now; if (ModelState.IsValid) { itemToEdit.Title = item.Title; itemToEdit.Description = item.Description; itemToEdit.Price = item.Price; itemToEdit.Image = item.Image; db.SaveChanges(); message = "Success"; } else { foreach (var value in ModelState.Values.ToList()) { foreach (var msv in value.Errors.ToList()) { message += msv.ErrorMessage; if (msv.ErrorMessage != "") { message += "/n"; } } } if (message == "") { message = "Wrong details inserted"; } } return(new JsonResult { Data = message, JsonRequestBehavior = JsonRequestBehavior.AllowGet }); }
public async Task <IActionResult> updateItem(ItemForUpdate prodForUpdate) { //var itemId = await _repo.GetItemId(prodForUpdate.Name); try { var itemFromRepo = await _repo.GetItem(prodForUpdate.Id); if (prodForUpdate.Barcode.ToString().Length == 0) { prodForUpdate.Barcode = itemFromRepo.Barcode; } if (prodForUpdate.Photo == null || prodForUpdate.Photo == "") { prodForUpdate.Photo = itemFromRepo.Photo; } if (prodForUpdate.Quantity.ToString().Length == 0) { prodForUpdate.Quantity = itemFromRepo.Quantity; } if (prodForUpdate.Price.ToString().Length == 0) { prodForUpdate.Price = itemFromRepo.Price; } _mapper.Map(prodForUpdate, itemFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } } catch (Exception e) { return(BadRequest(e.Message)); } return(Ok()); }
public ActionResult <ItemForAdding> PartiallyUpdateItem(Guid id, JsonPatchDocument <ItemForUpdate> patchDocument) { Item itemToUpdate = _warehouseInventoryRepository.GetItem(id); if (itemToUpdate == null) { return(NotFound()); } ItemForUpdate itemForUpdating = _mapper.Map <ItemForUpdate>(itemToUpdate); patchDocument.ApplyTo(itemForUpdating, ModelState); if (!TryValidateModel(itemForUpdating)) { return(ValidationProblem(ModelState)); } _mapper.Map(itemForUpdating, itemToUpdate); _warehouseInventoryRepository.Save(); return(NoContent()); }