public IHttpActionResult PostShoppingItem(int clusterId, int listId, ShoppingItemDTO item) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Cluster cluster = UnitOfWork.ClusterRepository.GetByID(clusterId); if (cluster == null) { return(NotFound()); } else if (cluster.ApplicationUsers.FirstOrDefault(x => x.Id == UserRecord.Id) == null) { return(Unauthorized()); } var list = cluster.ShoppingLists.FirstOrDefault(x => x.Id == listId); if (list == null) { return(NotFound()); } // Unable to modify a validated shopping list else if (list.Validated == true) { return(BadRequest("Unable to modify a validated shopping list")); } Grocery grocery = UnitOfWork.GroceryRepository.GetByID(item.GroceryId); if (grocery == null) { return(BadRequest("Grocery not found")); } Entity.Shopping.ShoppingItem entity = new Entity.Shopping.ShoppingItem() { AddDate = DateTime.Now, LastUpdate = DateTime.Now, Brought = item.Brought, ToBuy = item.ToBuy, Comment = item.Comment, Validated = false, ValidatedDate = null, Grocery = grocery, GroceryId = grocery.Id }; entity.LastUpdate = entity.AddDate; list.ShoppingItems.Add(entity); UnitOfWork.ShoppingListRepository.Update(list); UnitOfWork.Save(); return(CreatedAtRoute("GetShoppingItem", new { clusterId = clusterId, listId = list.Id, id = entity.Id }, Mapper.Map <GetShoppingItemDTO>(entity))); }
public async Task <ActionResult <ShoppingItemDTO> > Delete(Guid id) { try { return(ShoppingItemDTO.FromDbModel(await productsService.RemoveProductAsync(id))); } catch (Exception e) { return(Problem(e.Message, statusCode: StatusCodes.Status500InternalServerError)); } }
public ActionResult <IEnumerable <ShoppingItemDTO> > Get() { try { var items = productsService.GetAll().Select(i => ShoppingItemDTO.FromDbModel(i)); return(Ok(items)); } catch (Exception e) { logger.LogError(e, "Issue getting items!"); return(Problem(e.Message, statusCode: StatusCodes.Status500InternalServerError)); } }
public async Task <ActionResult <ShoppingItemDTO> > Put(Guid id, [FromForm] ShoppingItem shoppingItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { return(ShoppingItemDTO.FromDbModel(await productsService.UpdateProductAsync(id, shoppingItem))); } catch (Exception e) { return(Problem(e.Message, statusCode: StatusCodes.Status500InternalServerError)); } }
public async Task <ActionResult <ShoppingItemDTO> > Get(Guid id) { try { IShoppingItem item = await productsService.GetItemByIDAsync(id); return(Ok(ShoppingItemDTO.FromDbModel(item))); } catch (KeyNotFoundException e) { return(BadRequest(e.Message)); } catch (Exception e) { return(Problem(e.Message, statusCode: StatusCodes.Status500InternalServerError)); } }
public async Task <ActionResult <ShoppingItemDTO> > Post([FromForm] ShoppingItem shoppingItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { return(ShoppingItemDTO.FromDbModel(await productsService.AddProductAsync(shoppingItem))); } catch (ItemCreatedWithoutImageException e) { return(BadRequest(e.Message)); } catch (Exception e) { return(Problem(e.Message, statusCode: StatusCodes.Status500InternalServerError)); } }