public ActionResult Update(int id, EditItemModel model) { if (!ModelState.IsValid) { return(BadRequest()); } var item = moneyRepository.GetItem(model.Id); if (item == null) { return(NotFound()); } if (item.UserId != CurrentUserId) { return(Unauthorized()); } moneyRepository.UpdateItem(new Item { Id = model.Id, Name = model.Description, CategoryId = model.CategoryId, Cost = model.Cost.Value, Type = model.Type }); return(Ok()); }
public ActionResult GetItemById(int id) { var item = moneyRepository.GetItem(id); if (item == null) { return(NotFound()); } if (item.UserId != CurrentUserId) { return(BadRequest()); } var vm = new EditItemModel { Id = item.Id, CategoryId = item.CategoryId, Cost = item.Cost, Description = item.Name, Type = item.Type }; return(Ok(vm)); }
public async Task <IActionResult> EditItem(long itemId, [FromBody] EditItemModel model) { try { model.UserId = CurrentUserId; model.Id = itemId; var result = await itemLib.EditItemAsync(model); return(CustomResult(result)); } catch (System.Exception exp) { return(CustomError(exp)); } }
public async Task <ItemModel> EditItemAsync(EditItemModel model) { var entity = await itemRepo.FirstAsync(x => x.Id == model.Id); if (entity == null) { throw new Exception("Item Not Exist"); } if (entity.UserId != model.UserId) { throw new Exception("User Doesn't Owner Of Item"); } entity.Name = model.Name; itemRepo.Update(entity); await unitOfWork.CommitAsync(); return(ConvertItemToItemModel(entity)); }
private void InitData() { Model = new EditItemModel(); }