Exemplo n.º 1
0
        public async Task <Basket> CreateBasket(CreateBasketCommand command, CancellationToken cancellationToken = default)
        {
            command.Validate();

            var productsInStock = new List <Product>();

            async void action(Product product)
            {
                var inStock = await _productProviderService.ValidateProductStock(product.EAN);

                if (inStock)
                {
                    productsInStock.Add(product);
                }
            }

            command.Products
            .ToList()
            .ForEach(action);

            if (!productsInStock.Any())
            {
                throw new InvalidOperationException("none of the products have stock.");
            }

            var basket = await Task.FromResult(new Basket(command.User, productsInStock));

            return(basket);
        }
Exemplo n.º 2
0
 public BasketController(CreateBasketCommand createBasketCommand,
                         BasketQuery basketQuery,
                         DeleteBasketCommand deleteBasketCommand,
                         AddItemToBasketCommand addItemToBasketCommand,
                         UpdateItemQuantityCommand updateItemQuantityCommand)
 {
     _createBasketCommand       = createBasketCommand;
     _basketQuery               = basketQuery;
     _deleteBasketCommand       = deleteBasketCommand;
     _addItemToBasketCommand    = addItemToBasketCommand;
     _updateItemQuantityCommand = updateItemQuantityCommand;
 }
Exemplo n.º 3
0
        public async Task CreateBasket(CreateBasketDTO dto, CancellationToken cancellationToken = default)
        {
            var user     = new User(dto.ContractNumber, dto.CardNumber);
            var products = new List <Product>();

            // assemble DTO in Domain
            dto.Products
            .ToList()
            .ForEach(product => products.Add(product.Assemble()));

            // create new basket with CreateBasketCommand
            var createCommand = new CreateBasketCommand(user, products);
            var basket        = await _basketService.CreateBasket(createCommand, cancellationToken);

            // save the basket into the cache
            _cacheService.SetValue($"basket_{user.ContractNumber}_{user.CardNumber}",
                                   basket,
                                   TimeSpan.FromDays(1));
        }
Exemplo n.º 4
0
        private void CreateBasket()
        {
            CreateBasketCommand command = new CreateBasketCommand(_basketRepository);

            _commandResult = command.Do(_consumerId);
        }