Exemple #1
0
        public CoinChangesDto Handle(CoinBackCommand request)
        {
            var coinReturned = new CoinChangesDto
            {
                CoinChanges = new List <CoinChangeDto>()
            };

            foreach (var coinInserted in request.CoinsInserted)
            {
                var coinToBeRemoved =
                    _dbContext.VendingMachineCoins.Include(x => x.Currency)
                    .First(x => x.Currency.Id == coinInserted.CurrencyId);

                coinToBeRemoved.Pieces = coinToBeRemoved.Pieces - coinInserted.Pieces;
                coinReturned.CoinChanges.Add(new CoinChangeDto
                {
                    CurrencyId   = coinToBeRemoved.Currency.Id,
                    Pieces       = coinInserted.Pieces,
                    CurrencyName = coinToBeRemoved.Currency.CoinName,
                    Value        = coinToBeRemoved.Currency.Value
                });
            }

            _dbContext.SaveChanges();

            return(coinReturned);
        }
Exemple #2
0
        public CoinChangesDto Handle(OrderProductCommand request)
        {
            var vendingMachineProduct =
                _dbContext.VendingMachineProducts.First(x => x.Id == request.VendingMachineProductId);

            vendingMachineProduct.Portion = vendingMachineProduct.Portion - 1;

            var change = request.AmountInserted - vendingMachineProduct.Price;

            var coinsInMachine =
                _dbContext.VendingMachineCoins.Include(x => x.Currency).Where(x => x.Pieces != 0).OrderByDescending(x => x.Currency.Value).ToList();

            var changes = new CoinChangesDto
            {
                CoinChanges = new List <CoinChangeDto>()
            };

            while (change != 0)
            {
                foreach (var coin in coinsInMachine)
                {
                    while (change >= coin.Currency.Value && coin.Pieces != 0)
                    {
                        change      = change - coin.Currency.Value;
                        coin.Pieces = coin.Pieces - 1;
                        if (changes.CoinChanges.FirstOrDefault(x => x.CurrencyId == coin.Currency.Id) == null)
                        {
                            changes.CoinChanges.Add(new CoinChangeDto
                            {
                                CurrencyName = coin.Currency.CoinName,
                                Value        = coin.Currency.Value,
                                Pieces       = 1,
                                CurrencyId   = coin.Currency.Id
                            });
                        }
                        else
                        {
                            var updateCountOfChange = changes.CoinChanges.First(x => x.CurrencyId == coin.Currency.Id);

                            updateCountOfChange.Pieces = updateCountOfChange.Pieces + 1;
                        }
                    }
                }

                if (change != 0)
                {
                    break;
                }

                _dbContext.SaveChanges();
            }



            return(changes);
        }
Exemple #3
0
 public bool Handle(AddCoinsCommand request)
 {
     foreach (var coinWithPiece in request.CoinsWithPieces.Coins)
     {
         var currency = _dbContext.VendingMachineCoins.Include(x => x.Currency).First(x => x.Currency.Id == coinWithPiece.CurrencyId);
         currency.Pieces = currency.Pieces + coinWithPiece.Pieces;
     }
     _dbContext.SaveChanges();
     return(true);
 }
Exemple #4
0
        public bool Handle(EmptyCoinsInVendingMachineCommand request)
        {
            var coinsInVendingMachine = _dbContext.VendingMachineCoins.ToList();

            foreach (var vendingMachineCoin in coinsInVendingMachine)
            {
                vendingMachineCoin.Pieces = 0;
            }

            _dbContext.SaveChanges();
            return(true);
        }
Exemple #5
0
        public bool Handle(AddProductCommand request)
        {
            foreach (var product in request.Products.Products)
            {
                var productToBeUpdated =
                    _dbContext.VendingMachineProducts.Include(x => x.Product)
                    .First(x => x.Product.Id == product.ProductId);

                productToBeUpdated.Portion = productToBeUpdated.Portion + product.Portion;
                productToBeUpdated.Price   = product.Price;
            }
            _dbContext.SaveChanges();
            return(true);
        }
Exemple #6
0
        public ProductsAvailableDto Handle(GetProductsAvailableInVendingMachineWithCoinsQuery request)
        {
            decimal insertedMoney = 0;

            if (request.CoinsInserted == null)
            {
                request.CoinsInserted = new List <CoinInsertedDto>();
            }
            var productsDisplay = new ProductsAvailableDto
            {
                ProductsAvailable   = new List <ProductAvailableDto>(),
                CoinsInserted       = new List <CoinInsertedDto>(),
                TotalAmountInserted = 0
            };


            if (request.CoinsInserted.Count != 0)
            {
                foreach (var coinInserted in request.CoinsInserted)
                {
                    var currency = _dbContext.Currencies.First(x => x.Id == coinInserted.CurrencyId);
                    var amount   = currency.Value;
                    insertedMoney = insertedMoney + (amount * coinInserted.Pieces);
                    productsDisplay.CoinsInserted.Add(new CoinInsertedDto
                    {
                        CurrencyId = coinInserted.CurrencyId,
                        Pieces     = coinInserted.Pieces
                    });
                    var currencyInVendingMachine =
                        _dbContext.VendingMachineCoins.Include(x => x.Currency).First(x => x.Currency.Id == currency.Id);

                    currencyInVendingMachine.Pieces = currencyInVendingMachine.Pieces + coinInserted.Pieces;
                }

                productsDisplay.TotalAmountInserted = insertedMoney;

                _dbContext.SaveChanges();
            }


            var vendingMachineProducts = _dbContext.VendingMachineProducts.Include(x => x.Product).ToList();

            foreach (var vendingMachineProduct in vendingMachineProducts)
            {
                var product = new ProductAvailableDto
                {
                    ProductName             = vendingMachineProduct.Product.ProductName,
                    Price                   = vendingMachineProduct.Price,
                    VendingMachineProductId = vendingMachineProduct.Id
                };

                if (vendingMachineProduct.Portion != 0 && vendingMachineProduct.Price <= insertedMoney && new ChangeHelper().CanGiveChange(insertedMoney, vendingMachineProduct.Id))
                {
                    product.Available = true;
                    product.Message   = "Available";
                }

                else
                {
                    product.Available = false;

                    if (vendingMachineProduct.Portion == 0)
                    {
                        product.Message = "Not Available";
                    }

                    if (vendingMachineProduct.Price > insertedMoney)
                    {
                        product.Message = "Insufficient Amount";
                    }

                    if (new ChangeHelper().CanGiveChange(insertedMoney, vendingMachineProduct.Id))
                    {
                        product.Message = "Cannot Give Change";
                    }
                }

                productsDisplay.ProductsAvailable.Add(product);
            }

            return(productsDisplay);
        }