public void Handle(AddItemToBasketCommand cmd)
        {
            var basket = this.cBRepo.Get(cmd.CustomerId);

            basket.AddItem(cmd.ProductId, cmd.Quantity);
            this.cBRepo.Save(basket);
        }
Example #2
0
        public async Task When_Change_Quantity_Is_Made_With_Zero_Then_Item_Should_Be_Removed_From_The_Basket()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            Guid      basketId               = Faker.Random.Uuid();
            Guid      itemId                 = Faker.Random.Uuid();
            const int quantity               = 2;
            const int toQuantity             = 0;
            var       addItemToBasketCommand = new AddItemToBasketCommand(basketId, itemId, quantity);
            var       changeQuantityCommand  = new ChangeQuantityCommand(basketId, itemId, toQuantity);

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            await Sut.Send(addItemToBasketCommand);

            await Sut.Send(changeQuantityCommand);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            Basket basket = await Read(async context => { return(await context.Baskets.Include(x => x.Items).FirstOrDefaultAsync(x => x.Id == basketId)); });

            basket.Should().NotBeNull();
            basket.Items.Count.Should().Be(0);
            basket.Items.Should().NotContain(x => x.Id == itemId);
        }
Example #3
0
        public async Task Quantity_Should_Be_Changed_When_New_Quantity_Provided_For_Same_Item()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            Guid      basketId               = Faker.Random.Uuid();
            Guid      itemId                 = Faker.Random.Uuid();
            const int quantity               = 2;
            const int toQuantity             = 4;
            var       addItemToBasketCommand = new AddItemToBasketCommand(basketId, itemId, quantity);
            var       changeQuantityCommand  = new ChangeQuantityCommand(basketId, itemId, toQuantity);

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            await Sut.Send(addItemToBasketCommand);

            await Sut.Send(changeQuantityCommand);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            Basket basket = await Read(async context => { return(await context.Baskets.Include(x => x.Items).FirstOrDefaultAsync(x => x.Id == basketId)); });

            basket.Should().NotBeNull();
            basket.Items.Count.Should().Be(1);
            basket.Items.Should().Contain(x => x.Id == itemId);
            basket.Items.First(x => x.Id == itemId).Quantity.Should().Be(toQuantity);
        }
Example #4
0
        public async Task Basket_Should_Be_Created_With_Items()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            Guid      basketId = Faker.Random.Uuid();
            Guid      itemId   = Faker.Random.Uuid();
            const int quantity = 2;
            var       command  = new AddItemToBasketCommand(basketId, itemId, quantity);

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            await Sut.Send(command);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            Basket basket = await Read(async context => { return(await context.Baskets.Include(x => x.Items).FirstOrDefaultAsync(x => x.Id == basketId)); });

            basket.Should().NotBeNull();
            basket.Items.Count.Should().Be(1);
            basket.Items.Should().Contain(x => x.Id == itemId);
            basket.Items.First(x => x.Id == itemId).Quantity.Should().Be(quantity);
        }
Example #5
0
        public async Task Clear_Basket_Should_Clear_All_Items_In_The_Basket()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            Guid      basketId = Faker.Random.Uuid();
            Guid      itemId   = Faker.Random.Uuid();
            const int quantity = 2;

            var addItemToBasketCommand = new AddItemToBasketCommand(basketId, itemId, quantity);
            var clearBasketCommand     = new ClearBasketCommand(basketId);

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            await Sut.Send(addItemToBasketCommand);

            await Sut.Send(clearBasketCommand);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            Basket basket = await Read(async context => { return(await context.Baskets.Include(x => x.Items).FirstOrDefaultAsync(x => x.Id == basketId)); });

            basket.Should().NotBeNull();
            basket.Items.Count.Should().Be(0);
            basket.Items.Should().NotContain(x => x.Id == itemId);
        }
Example #6
0
        public ActionResult AddItem(AddItemToBasketCommand command)
        {
            if (CurrentUser != null && CurrentUser.Id != Guid.Empty)
            {
                command.UserId = CurrentUser.Id;
                var result = _commandBus.Send(command);
                return(JsonMessage(result));
            }

            var cookieId      = _cookieQueryService.Retrieve(KadobinCookieId);
            var cookieIdValue = Guid.NewGuid().ToString();

            if (string.IsNullOrEmpty(cookieId))
            {
                _cookieQueryService.Save(KadobinCookieId, cookieIdValue, DateTime.Now.AddDays(30));
            }
            else
            {
                cookieIdValue = cookieId;
            }

            var unauthorizedBasketCommand = new AddItemToUnauthorizedBasketCommand()
            {
                Quantity   = command.Quantity,
                CookieId   = Guid.Parse(cookieIdValue),
                ProductIds = command.ProductIds
            };

            var unauthorizedResult = _commandBus.Send(unauthorizedBasketCommand);

            return(JsonMessage(unauthorizedResult));
        }
        public IActionResult AddItemToBasket(int customerId, int productId, int quantity)
        {
            var cmd = new AddItemToBasketCommand {
                CustomerId = customerId, ProductId = productId, Quantity = quantity
            };

            this.basketService.Submit(cmd);

            return(CreatedAtAction(nameof(GetBasketById), new { customerId }, null));
        }
Example #8
0
 public BasketController(CreateBasketCommand createBasketCommand,
                         BasketQuery basketQuery,
                         DeleteBasketCommand deleteBasketCommand,
                         AddItemToBasketCommand addItemToBasketCommand,
                         UpdateItemQuantityCommand updateItemQuantityCommand)
 {
     _createBasketCommand       = createBasketCommand;
     _basketQuery               = basketQuery;
     _deleteBasketCommand       = deleteBasketCommand;
     _addItemToBasketCommand    = addItemToBasketCommand;
     _updateItemQuantityCommand = updateItemQuantityCommand;
 }
        public BasketDto AddItemToBasket(AddItemToBasketCommand msg)
        {
            BasketDto basketDto = null;

            var uri = this.GenerateRequestUri($"{msg.CustomerId}", "item");

            var response = this.Post(uri, new { productId = msg.ProductId, quantity = msg.Quantity });

            if (response.IsSuccessStatusCode)
            {
                basketDto = this.GetBasket(new GetBasketQuery {
                    CustomerId = msg.CustomerId
                });
            }
            return(basketDto);
        }
Example #10
0
        public void ShouldAddAnItemToBasketGivenItemAddMessage()
        {
            //arrange
            var msg = new AddItemToBasketCommand
            {
                CustomerId = expectedBasketDto.CustomerId,
                ProductId  = expectedBasketDto.Items[0].ProductId,
                Quantity   = expectedBasketDto.Items[0].Quantity
            };
            var sut = new BasketManagementClient(this.clientBaseUri);

            //act
            var result = sut.AddItemToBasket(msg);

            //assert
            Assert.Equal(1, result.Items.Count);
            Assert.Equal(expectedBasketDto.Items[0].ProductId, result.Items[0].ProductId);
            Assert.Equal(expectedBasketDto.Items[0].Quantity, result.Items[0].Quantity);
        }
        public async Task <BasketItemDto> AddItemToBasket(AddItemToBasketCommand command)
        {
            try
            {
                var isBasketExit = await _basketRepository.AnyAsync(x => x.Id == command.BasketId);

                if (!isBasketExit)
                {
                    throw new BasketException(ErrorCode.BasketNotFound, string.Format(ErrorMessageConstants.BasketNotFound, command.BasketId));
                }

                var itemToAdd = await _productRepository.Table.FirstOrDefaultAsync(x => x.Id == command.ItemId);

                if (itemToAdd == null)
                {
                    throw new BasketException(ErrorCode.ItemNotFound, string.Format(ErrorMessageConstants.ItemNotFound, command.ItemId));
                }

                if (itemToAdd.Quantity < command.Quantity)
                {
                    throw new BasketException(ErrorCode.OutOfStock, string.Format(ErrorMessageConstants.OutOfStock, itemToAdd.Quantity));
                }

                _basketItemRepository.Add(new BasketItem
                {
                    Id       = Guid.NewGuid(),
                    Quantity = command.Quantity,
                    BasketId = command.BasketId,
                    ItemId   = command.ItemId,
                });

                itemToAdd.Quantity -= command.Quantity;
                _productRepository.Update(itemToAdd);

                await _unitOfWork.CommitAsync();

                return(await GetBasketItems(new GetBasketItemQuery(command.BasketId)));
            }
            catch (Exception e)
            {
                throw new BasketException(ErrorCode.InvalidOperation, ErrorMessageConstants.CanNotAddItem, e);
            }
        }
        public async Task Basket_Should_Be_Created_With_Items()
        {
            var customerId = Faker.Random.Int(min: 1);
            var itemId     = Faker.Random.Int(min: 1);
            var quantity   = Faker.Random.Int(min: 1);

            var command = new AddItemToBasketCommand(customerId, itemId, quantity);

            var basketRepository = new Mock <IRepository <Basket> >();
            var itemStockChecker = new Mock <IItemStockChecker>();

            itemStockChecker.Setup(mr => mr.IsAvaliable(itemId, quantity)).Returns(Task.FromResult(true));

            var handler =
                new AddItemToBasketCommandHandler(basketRepository.Object, itemStockChecker.Object);

            var exception =
                await Record.ExceptionAsync(async() => await handler.Handle(command, new CancellationToken()));

            exception.Should().BeNull();
        }
 public void Setup()
 {
     _stockRepository  = Substitute.For <IStockRepository>();
     _basketRepository = Substitute.For <IBasketRepository>();
     _command          = new AddItemToBasketCommand(_stockRepository, _basketRepository);
 }
Example #14
0
        public async Task <IActionResult> AddItemToBasket([FromBody] AddItemToBasketCommand command)
        {
            var result = await _basketService.AddItemToBasket(command);

            return(Ok(result));
        }
Example #15
0
        private void AddItemToBasket(string productReference)
        {
            AddItemToBasketCommand command = new AddItemToBasketCommand(_basketRepository);

            command.Do(_basketId, productReference);
        }