/// <summary> /// Update information of the given grocery item dto. /// The <see cref="GroceryItemDto.DateTimeCreated"/> property is ignored. /// </summary> /// <exception cref="ArgumentException">Thrown if the given groceryItemDto instance does not have value for Id property.</exception> /// <exception cref="ObjectNotFoundException">Thrown if no grocery item can be found with the given Id.</exception> public void Update(GroceryItemDto groceryItemDto) { if (groceryItemDto == null) { throw new ArgumentNullException("groceryItemDto"); } if (!groceryItemDto.Id.HasValue) { throw new ArgumentException("In order to update a grocery item, its id must be provided."); } var groceryItem = this._groceryItemRepository.GetGroceryItem(groceryItemDto.Id.Value); if (groceryItem == null) { throw new ObjectNotFoundException("The object with the given id could not be found."); } // Update grocery item properties groceryItem.Bought = groceryItemDto.Bought; groceryItem.Name = groceryItemDto.Name; this._groceryItemRepository.Update(groceryItem); this._groceryItemRepository.SaveChanges(); }
// PUT api/<controller> public IHttpActionResult Put([FromBody] GroceryItemDto groceryItem) { object result; if (this.ModelState.IsValid) { this._groceryItemService.Update(groceryItem); result = new { Success = true, Message = MessageResource.GroceryItemSuccessfullySaved }; } else { result = new { Success = false, Errors = this.ModelState.Where(kv => kv.Value.Errors.Any()) .ToDictionary(kv => kv.Key, kv => kv.Value.Errors.Select(e => e.ErrorMessage)) }; } return(Ok(result)); }
/// <summary> /// Adds a new grocery item into database /// </summary> public void Add(GroceryItemDto groceryItemDto) { if (groceryItemDto == null) { throw new ArgumentNullException("groceryItemDto"); } this._groceryItemRepository.Create(groceryItemDto.Name, groceryItemDto.Bought); this._groceryItemRepository.SaveChanges(); }