private void ValidateList(Cluster cluster, Entity.Shopping.ShoppingList list) { foreach (Entity.Shopping.ShoppingItem item in list.ShoppingItems) { // On verifie si le produit est dans le cluster var clusterGrocery = cluster.ClusterGroceries.FirstOrDefault(x => x.Grocery.Id == item.GroceryId); if (clusterGrocery != null) { clusterGrocery.Quantity += item.Brought; UnitOfWork.ClusterGroceriesRepository.Update(clusterGrocery); } // Sinon on l'ajoute else { var grocery = UnitOfWork.GroceryRepository.GetByID(item.GroceryId); if (grocery != null) { clusterGrocery = new ClusterGrocery() { Cluster = cluster, Grocery = grocery, Quantity = item.Brought, UpdateTime = DateTime.Now }; UnitOfWork.ClusterGroceriesRepository.Insert(clusterGrocery); UnitOfWork.Save(); } } } }
public IHttpActionResult PostShoppingList(int clusterId, NewShoppingListDTO list) { 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()); } Entity.Shopping.ShoppingList entity = new Entity.Shopping.ShoppingList() { Name = list.Name, Description = list.Description, CreateDate = DateTime.Now, Validated = false, ValidatedDate = null }; cluster.ShoppingLists.Add(entity); UnitOfWork.ClusterRepository.Update(cluster); UnitOfWork.Save(); return(CreatedAtRoute("GetShoppingList", new { clusterId = clusterId, id = entity.Id }, Mapper.Map <GetShoppingListDTO>(entity))); }
public IHttpActionResult PostShoppingListFromModel(int clusterId, int modelId) { Cluster cluster = UnitOfWork.ClusterRepository.GetByID(clusterId); if (cluster == null) { return(NotFound()); } else if (cluster.ApplicationUsers.FirstOrDefault(x => x.Id == UserRecord.Id) == null) { return(Unauthorized()); } ShoppingModelList model = UnitOfWork.ShoppingModelListRepository.GetByID(modelId); if (model == null) { return(NotFound()); } Entity.Shopping.ShoppingList entity = new Entity.Shopping.ShoppingList() { Name = model.Name, Description = model.Description, CreateDate = DateTime.Now, Validated = false, ValidatedDate = null }; cluster.ShoppingLists.Add(entity); UnitOfWork.ClusterRepository.Update(cluster); UnitOfWork.Save(); entity.ShoppingItems = new List <ShoppingItem>(); foreach (ShoppingModelItem item in model.ShoppingModelItems) { ShoppingItem newItem = new ShoppingItem() { AddDate = DateTime.Now, LastUpdate = DateTime.Now, Brought = 0, ToBuy = item.ToBuy, Validated = false, Comment = item.Comment, ValidatedDate = null, Grocery = item.Grocery, GroceryId = item.Grocery.Id }; entity.ShoppingItems.Add(newItem); } UnitOfWork.ShoppingListRepository.Update(entity); UnitOfWork.Save(); return(CreatedAtRoute("GetShoppingList", new { clusterId = clusterId, id = entity.Id }, Mapper.Map <GetShoppingListDTO>(entity))); }