Пример #1
0
        public void AddItemToShoppingCart()
        {
            var itemToAdd = new ShoppingCartItem {
                ShoppingCartItemId = Guid.Parse("d28888e9-2ba9-473a-a40f-e38cb54f0103"), UserId = "d28888e9-2ba9-473a-a40f-e38cb54f0201", CatalogItemId = new Guid("90d6da79-e0e2-4ba8-bf61-2d94d90df803"), Amount = 3
            };

            _repository.AddItemToShoppingCart(itemToAdd);
            _repository.Save();

            var itemRetrived = _repository.GetItemFromShoppingCart(itemToAdd);

            Assert.Equal(itemToAdd.CatalogItemId, itemRetrived.CatalogItemId);
            Assert.Equal(itemToAdd.UserId, itemRetrived.UserId);
            Assert.Equal(itemToAdd.Amount, itemRetrived.Amount);
        }
        public async Task <ActionResult <ShoppingCartItemDto> > AddItemToShoppingCart(ShoppingCartItemDto shoppingCartItemDto)
        {
            if (shoppingCartItemDto.CatalogItemId == null ||
                shoppingCartItemDto.UserId == null)
            {
                return(BadRequest("Provided object does not contain necessary information."));
            }

            var shoppingCartItemEntity = new ShoppingCartItem(shoppingCartItemDto);

            // If item already exists in users shoppingcart increase amount with one, else add one new item.
            var itemInDb = _shoppingCartRepository.GetItemFromShoppingCart(shoppingCartItemEntity);

            if (itemInDb == null)
            {
                shoppingCartItemEntity.Amount = 1;
                _shoppingCartRepository.AddItemToShoppingCart(shoppingCartItemEntity);
            }
            else
            {
                itemInDb.Amount++;
            }

            if (!await _shoppingCartRepository.Save())
            {
                return(BadRequest("Save item to shoppingcart failed."));
            }

            return(Ok());
        }