Пример #1
0
        public BuyResponse Buy(BuyRequest request)
        {
            var response = new BuyResponse();

            try
            {
                // Подсчитываем общее кол-во монет с учетом вновь прибывших
                var coinsBuffer = _coinService.GetAllCoins().OrderByDescending(x => x.Value);
                var totalCoins  = 0;
                foreach (var coin in coinsBuffer)
                {
                    foreach (var coinOrder in request.Coins)
                    {
                        if (coin.Id == coinOrder.Key)
                        {
                            totalCoins += coin.Value * coinOrder.Value;
                            coin.Count += coinOrder.Value;
                        }
                    }
                }

                var refund = totalCoins - request.TotalCost;
                if (refund != 0)
                {
                    response.Refund = CalculateRefund(refund, coinsBuffer);
                }

                foreach (var drinkId in request.Drinks)
                {
                    var drink = _drinkService.GetDrinkById(drinkId);
                    if (drink != null)
                    {
                        drink.Count += -1;
                        _drinkService.Update(drink);
                    }
                }

                if (response.Refund != null && response.Refund.Count > 0)
                {
                    foreach (var itemDictionary in response.Refund)
                    {
                        itemDictionary.Key.Count += -itemDictionary.Value;
                        _coinService.Update(itemDictionary.Key);
                    }
                }
            }
            catch (Exception e)
            {
                response.ErrorCode = 1;
                response.Message   = e.Message;
            }
            return(response);
        }
 public CoinsListResponse GetDrinks()
 {
     try
     {
         return(new CoinsListResponse
         {
             Coins = _coinService.GetAllCoins()
         });
     }
     catch
     {
         return(null);
     }
 }