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 PutShoppingList(int id, ShoppingListModelDTO list)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ShoppingModelList entity = UserRecord.ShoppingListModels.FirstOrDefault(x => x.Id == id);

            if (entity == null)
            {
                return(NotFound());
            }

            entity.Name        = list.Name;
            entity.Description = list.Description;

            UnitOfWork.ShoppingModelListRepository.Update(entity);

            try
            {
                UnitOfWork.Save();
            }
            catch (DbUpdateConcurrencyException e)
            {
                return(InternalServerError(new Exception(string.Format("Failed to update cluster {0}", entity.Id), e)));
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Example #3
0
        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)));
        }
        public IHttpActionResult GetShoppingModelList(int id)
        {
            ShoppingModelList entity = UserRecord.ShoppingListModels.FirstOrDefault(x => x.Id == id);

            if (entity == null)
            {
                return(NotFound());
            }

            return(Ok(Mapper.Map <GetShoppingListModelDTO>(entity)));
        }
        public IHttpActionResult GetShoppingItems(int listId)
        {
            ShoppingModelList entity = UserRecord.ShoppingListModels.FirstOrDefault(x => x.Id == listId);

            if (entity == null)
            {
                return(NotFound());
            }


            return(Ok(Mapper.Map <IEnumerable <GetShoppingItemModelDTO> >(entity.ShoppingModelItems)));
        }
        public IHttpActionResult DeleteShoppingList(int id)
        {
            ShoppingModelList entity = UserRecord.ShoppingListModels.FirstOrDefault(x => x.Id == id);

            if (entity == null)
            {
                return(NotFound());
            }

            UnitOfWork.ShoppingModelListRepository.Delete(id);
            UnitOfWork.Save();

            return(Ok(Mapper.Map <GetShoppingListModelDTO>(entity)));
        }
        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)));
        }
        public IHttpActionResult PostShoppingList(NewShoppingListModelDTO list)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var entity = new ShoppingModelList()
            {
                Name        = list.Name,
                Description = list.Description,
                User        = UserRecord
            };

            UnitOfWork.ShoppingModelListRepository.Insert(entity);
            UnitOfWork.Save();

            return(CreatedAtRoute("GetShoppingListModel", new { id = entity.Id }, Mapper.Map <GetShoppingListModelDTO>(entity)));
        }
        public IHttpActionResult PutShoppingItem(int listId, int id, ShoppingItemModelDTO item)
        {
            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());
            }

            var grocery = UnitOfWork.GroceryRepository.GetByID(item.GroceryId);

            if (grocery == null)
            {
                return(BadRequest("Grocery not found"));
            }


            itemEntity.Comment = item.Comment;
            itemEntity.Grocery = grocery;
            itemEntity.ToBuy   = item.ToBuy;

            UnitOfWork.ShoppingModelItemRepository.Update(itemEntity);
            UnitOfWork.Save();


            return(CreatedAtRoute("GetShoppingItemModel", new { id = entity.Id }, Mapper.Map <GetShoppingItemModelDTO>(itemEntity)));
        }