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 PostGrocery(int clusterId, GroceryDTO grocery) { 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()); } Category category = null; if (grocery.Category != null) { category = UnitOfWork.CategoryRepository.Get(x => x.Name == grocery.Category).FirstOrDefault(); if (category == null) { return(BadRequest("Category name is unknown")); } } Grocery entity = new Grocery() { Name = grocery.Name, Description = grocery.Description, Category = category, Picture = grocery.Picture, }; var clusterGrocery = new ClusterGrocery() { Cluster = cluster, Grocery = entity, Quantity = grocery.Quantity, Name = entity.Name, UpdateTime = DateTime.Now }; UnitOfWork.ClusterGroceriesRepository.Insert(clusterGrocery); UnitOfWork.Save(); return(CreatedAtRoute("GetGrocery", new { clusterId = clusterId, id = entity.Id }, Mapper.Map <GetGroceryDTO>(clusterGrocery))); }
public IHttpActionResult PostGrocery(int clusterId, int id, ExistingGrocery existingGrocery) { 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()); } Grocery grocery = UnitOfWork.GroceryRepository.GetByID(id); if (cluster == null) { return(NotFound()); } var clusterGrocery = new ClusterGrocery() { Cluster = cluster, Grocery = grocery, Quantity = existingGrocery.Quantity, Name = existingGrocery.Name, UpdateTime = DateTime.Now }; UnitOfWork.ClusterGroceriesRepository.Insert(clusterGrocery); UnitOfWork.Save(); return(CreatedAtRoute("GetGrocery", new { clusterId = clusterId, id = grocery.Id }, Mapper.Map <GetGroceryDTO>(clusterGrocery))); }
protected override void Seed(GrogerContext context) { var manager = new UserManager <ApplicationUser>( new UserStore <ApplicationUser>( context)); var user = new ApplicationUser() { UserName = "******", Email = "*****@*****.**", }; var result = manager.Create(user, "Password1"); var users = new List <ApplicationUser>() { user }; context.Groceries.AddOrUpdate(x => x.Id, new Grocery() { Id = 1, Name = "Sel de mer", Description = "Sel la balaine", CategoryId = 1 }, new Grocery() { Id = 2, Name = "Poivre", Description = "Poivre 5 baies", CategoryId = 1 }, new Grocery() { Id = 3, Name = "Côtes de porc", Description = "Côtes de porc", CategoryId = 1 }, new Grocery() { Id = 4, Name = "Cuisse de poulet", Description = "Poulet de louet", CategoryId = 1 } ); context.Clusters.AddOrUpdate(x => x.Id, new Cluster() { Id = 1, Name = "Cuisine1", Description = "Ma cuisine 1", ApplicationUsers = users }, new Cluster() { Id = 2, Name = "Cuisine2", Description = "Ma cuisine 2", ApplicationUsers = users }, new Cluster() { Id = 3, Name = "Cuisine3", Description = "Ma cuisine 3", ApplicationUsers = users }, new Cluster() { Id = 4, Name = "Cuisine4", Description = "Ma cuisine 4", ApplicationUsers = users } ); var clusterGroceries = new ClusterGrocery { ClusterId = 1, GroceryId = 1, Quantity = 2, UpdateTime = DateTime.Now }; context.ClusterGroceries.AddOrUpdate(clusterGroceries); context.Categories.AddOrUpdate(x => x.Id, new Category() { Id = 1, Name = "Tooout", Description = "Tout est fini entre noooous" } ); }