public IHttpActionResult PostShoppingItem(int listId, ShoppingItemModelDTO item) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var list = UserRecord.ShoppingListModels.FirstOrDefault(x => x.Id == listId); if (list == null) { return(NotFound()); } var grocery = UnitOfWork.GroceryRepository.GetByID(item.GroceryId); if (grocery == null) { return(BadRequest("Grocery not found")); } var entity = new ShoppingModelItem() { GroceryId = grocery.Id, Comment = item.Comment, ToBuy = item.ToBuy }; list.ShoppingModelItems.Add(entity); UnitOfWork.ShoppingModelListRepository.Update(list); UnitOfWork.Save(); return(CreatedAtRoute("GetShoppingItemModel", new { id = entity.Id }, Mapper.Map <GetShoppingItemModelDTO>(entity))); }
public IHttpActionResult DeleteShoppingItem(int listId, int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } ShoppingModelList entity = UserRecord.ShoppingListModels.FirstOrDefault(x => x.Id == listId); if (entity == null) { return(NotFound()); } ShoppingModelItem itemEntity = entity.ShoppingModelItems.FirstOrDefault(x => x.Id == id); if (itemEntity == null) { return(NotFound()); } UnitOfWork.ShoppingModelItemRepository.Delete(itemEntity); UnitOfWork.Save(); return(Ok(Mapper.Map <GetShoppingItemModelDTO>(itemEntity))); }
public IHttpActionResult GetShoppingItem(int listId, int id) { ShoppingModelList entity = UserRecord.ShoppingListModels.FirstOrDefault(x => x.Id == listId); if (entity == null) { return(NotFound()); } ShoppingModelItem item = entity.ShoppingModelItems.FirstOrDefault(x => x.Id == id); if (item == null) { return(NotFound()); } return(Ok(Mapper.Map <GetShoppingItemModelDTO>(item))); }