// DELETE api/ShoppingItem/5
        public HttpResponseMessage DeleteShoppingItem(int id)
        {
            ShoppingItem shoppingItem = db.ShoppingItems.Find(id);
            if (shoppingItem == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            if (db.Entry(shoppingItem.ShoppingCategoryList).Entity.UserId != User.Identity.Name)
            {
                // Trying to delete a record that does not belong to the user
                return Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            ShoppingItemDto shoppingItemDto = new ShoppingItemDto(shoppingItem);
            db.ShoppingItems.Remove(shoppingItem);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError);
            }

            return Request.CreateResponse(HttpStatusCode.OK, shoppingItemDto);
        }
        // POST api/ShoppingItem
        public HttpResponseMessage PostShoppingItem(ShoppingItemDto shoppingItemDto)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            ShoppingCategoryList shoppingCategoryList = db.ShoppingCategoryLists.Find(shoppingItemDto.ShoppingCategoryListId);
            if (shoppingCategoryList == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            if (shoppingCategoryList.UserId != User.Identity.Name)
            {
                // Trying to add a record that does not belong to the user
                return Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            ShoppingItem shoppingItem = shoppingItemDto.ToEntity();

            // Need to detach to avoid loop reference exception during JSON serialization
            db.Entry(shoppingCategoryList).State = EntityState.Detached;
            db.ShoppingItems.Add(shoppingItem);
            db.SaveChanges();
            shoppingItemDto.ShoppingItemId = shoppingItem.ShoppingItemId;

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, shoppingItemDto);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = shoppingItemDto.ShoppingItemId }));
            return response;
        }
示例#3
0
            protected override void When()
            {
                var newItem = new ShoppingItemDto {
                    Name = NewItemName, ProductType = ProductType.Drink, Quantity = NewQuantity
                };

                _result = SUT.Add(newItem);
            }
示例#4
0
 public static ShoppingItem ToEntity(this ShoppingItemDto shoppingItemDto)
 {
     return(new ShoppingItem
     {
         Name = shoppingItemDto.Name,
         ProductType = shoppingItemDto.ProductType,
         Quantity = shoppingItemDto.Quantity
     });
 }
 protected override void When()
 {
     try
     {
         ResultShoppingItem = SUT.GetShoppingItem("New item");
     }
     catch (KeyNotFoundException exception)
     {
         NewException = exception;
     }
 }
        public void UpdateItemQuantity(ShoppingItemDto item)
        {
            ValidateModel(item);

            var result = _shoppingListRepository.UpdateItemQuantity(item.Drink.Name, item.Quantity);

            if (result <= 0)
            {
                throw new EntityNotFoundException();
            }
        }
            protected override void When()
            {
                ShoppingItems = new List <ShoppingItem>().AsQueryable();
                PrepareSut();

                var newItem = new ShoppingItemDto {
                    Name = NewItemName, ProductType = ProductType.Drink, Quantity = NewQuantity
                };

                SUT.AddNewItem(newItem);
            }
        public void Add(ShoppingItemDto item)
        {
            ValidateModel(item);

            if (IsDrinkAlreadyAdded(item.Drink.Name))
            {
                throw new BusinessException("The selected drink was already added to the shopping list.");
            }

            _shoppingListRepository.Add(item.Drink.Name, item.Quantity);
        }
            protected override void When()
            {
                ShoppingItems = new List <ShoppingItem>().AsQueryable();
                PrepareSut();

                try
                {
                    ResultShoppingItem = SUT.GetShoppingItem("New item");
                }
                catch (KeyNotFoundException exception)
                {
                    NewException = exception;
                }
            }
            protected override void When()
            {
                var newItem = new ShoppingItemDto {
                    Name = ExistingItemName, ProductType = ProductType.Drink, Quantity = 3
                };

                try
                {
                    SUT.AddNewItem(newItem);
                }
                catch (ArgumentException exception)
                {
                    NewException = exception;
                }
            }
        public void AddNewItem(ShoppingItemDto shoppingItem)
        {
            var existingItem = GetItemByName(shoppingItem.Name);

            if (existingItem != null)
            {
                throw new ArgumentException("Product already exists");
            }

            var newShoppingItem = shoppingItem.ToEntity();

            _shoppingItemRepository.Add(newShoppingItem);

            _shoppingItemRepository.Save();
        }
示例#12
0
        public IActionResult AddShoppingItem([FromBody] ShoppingItemDto shoppingItemDto)
        {
            var shoppingItem = _mapper.Map <ShoppingItem>(shoppingItemDto);

            try
            {
                var created = _shoppingItemService.CreateShoppingItem(shoppingItem);
                shoppingItemDto.Id = created.Id;

                return(Ok(shoppingItemDto));
            }
            catch (AppException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
示例#13
0
        public IActionResult UpdateShoppingItem(string id, [FromBody] ShoppingItemDto shoppingItemDto)
        {
            var shoppingItem = _mapper.Map <ShoppingItem>(shoppingItemDto);

            shoppingItemDto.Id = id;

            try
            {
                _shoppingItemService.UpdateShoppingItem(shoppingItem);
                return(Ok(shoppingItemDto));
            }
            catch (AppException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
示例#14
0
        public IHttpActionResult Add(ShoppingItemDto shoppintItemDto)
        {
            _shoppingListService.AddNewItem(shoppintItemDto);

            return(Ok());
        }
 protected override void When()
 {
     ResultShoppingItem = SUT.GetShoppingItem(ExistingItemName);
 }
        // PUT api/ShoppingItem/5
        public HttpResponseMessage PutShoppingItem(int id, ShoppingItemDto shoppingItemDto)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != shoppingItemDto.ShoppingItemId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            ShoppingItem shoppingItem = shoppingItemDto.ToEntity();
            ShoppingCategoryList shoppingcategoryList = db.ShoppingCategoryLists.Find(shoppingItem.ShoppingCategoryListId);
            if (shoppingcategoryList == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            if (shoppingcategoryList.UserId != User.Identity.Name)
            {
                // Trying to modify a record that does not belong to the user
                return Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            // Need to detach to avoid duplicate primary key exception when SaveChanges is called
            db.Entry(shoppingcategoryList).State = EntityState.Detached;
            db.Entry(shoppingItem).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        public IHttpActionResult UpdateItemQuantity([FromBody] ShoppingItemDto item)
        {
            _shoppingListService.UpdateItemQuantity(item);

            return(Ok());
        }
        public IHttpActionResult Add([FromBody] ShoppingItemDto item)
        {
            _shoppingListService.Add(item);

            return(Ok());
        }