public JsonResult MakeCupOfCoffee(RequestCoffeeLog requestCoffeeLog)
        {
            CoffeeDeviceState coffeeDeviceState = GetCurrentUserCoffeeDeviceState(requestCoffeeLog.AppUserId);

            if (coffeeDeviceState != null)
            {
                if (IsResourcesAmountEnough(coffeeDeviceState))
                {
                    CoffeeLog coffeeLog = RequestCoffeeLogToCoffeeLog(requestCoffeeLog);
                    _coffeeRepository.MakeCupOfCoffee(coffeeLog);
                    return(new JsonResult(new { message = "ok", coffeDeviceState = GetCurrentUserCoffeeDeviceState(requestCoffeeLog.AppUserId) }));
                }
                else
                {
                    _coffeeRepository.UpdateCoffeSeviceState(requestCoffeeLog.AppUserId);
                    return(new JsonResult(new { message = "error", coffeDeviceState = GetCurrentUserCoffeeDeviceState(requestCoffeeLog.AppUserId) }));
                }
            }
            else
            {
                return(new JsonResult(new
                {
                    message = "error",
                }));
            }
        }
Beispiel #2
0
        public bool MakeCupOfCoffeeIfNeeded(string appUserId, DateTime dateTime)
        {
            CoffeeDevice coffeeDevice = _dbContext.CoffeDevices
                                        .Include(cd => cd.CoffeeLogs)
                                        .ThenInclude(cl => cl.CoffeeType)
                                        .Include(cd => cd.CoffeeLogs)
                                        .ThenInclude(cl => cl.HowOften)
                                        .FirstOrDefault(cd => cd.AppUserId == appUserId);

            CoffeeLog currentCoffeeLog = coffeeDevice.CoffeeLogs.FirstOrDefault(cl => cl.IsRepeatable != true && IsDateTimesEquals(cl.Date, dateTime));

            if (currentCoffeeLog != null)
            {
                MinusCoffeeCosts(coffeeDevice, currentCoffeeLog);
                return(true);
            }

            List <CoffeeLog> repeatableCoffeLogs = coffeeDevice.CoffeeLogs.Where(cl => cl.IsRepeatable == true).ToList();

            currentCoffeeLog = IsCupOfCoffeeNeeded(repeatableCoffeLogs, dateTime);

            if (currentCoffeeLog != null)
            {
                coffeeDevice = MinusCoffeeCosts(coffeeDevice, currentCoffeeLog);
                return(true);
            }

            _dbContext.SaveChanges();
            return(false);
        }
Beispiel #3
0
        private CoffeeLog IsCupOfCoffeeNeeded(List <CoffeeLog> coffeeLogs, DateTime dateTime)
        {
            CoffeeLog  result       = null;
            List <int> dayOfWeeksId = new List <int>()
            {
                3, 4, 5, 6, 7, 8, 9
            };

            foreach (CoffeeLog coffeeLog in coffeeLogs)
            {
                if (IsDateTimesEquals(dateTime, coffeeLog.Date) && _dbContext.CoffeeLogs.FirstOrDefault(cl => cl.CoffeeDeviceId == coffeeLog.CoffeeTypeId && coffeeLog.Date == cl.Date) == null)
                {
                    result = coffeeLog;
                }
                if (coffeeLog.HowOften.Id == 2 && IsTimesEquals(dateTime, coffeeLog.Date))
                {
                    result = coffeeLog;
                }
                else if (coffeeLog.HowOften.Id == 10 && IsDayOfWeekWeekDay(dateTime) && IsTimesEquals(dateTime, coffeeLog.Date))
                {
                    result = coffeeLog;
                }
                else if (coffeeLog.HowOften.Id == 11 && !IsDayOfWeekWeekDay(dateTime) && IsTimesEquals(dateTime, coffeeLog.Date))
                {
                    result = coffeeLog;
                }
                else if (dayOfWeeksId.Contains(coffeeLog.HowOften.Id) && IsDayOfWeekAndTimeEquals(coffeeLog.Date, dateTime))
                {
                    result = coffeeLog;
                }
            }

            return(result);
        }
Beispiel #4
0
 public void MakeCupOfCoffee(CoffeeLog coffeeLog)
 {
     _dbContext.CoffeeLogs.Add(coffeeLog);
     if (IsDateTimesEquals(DateTime.Now, coffeeLog.Date))
     {
         CoffeeDevice coffeeDevice = _dbContext.CoffeDevices.FirstOrDefault(cd => cd.Id == coffeeLog.CoffeeDeviceId);
         coffeeDevice = MinusCoffeeCosts(coffeeDevice, coffeeLog);
     }
     _dbContext.SaveChanges();
 }
Beispiel #5
0
        private CoffeeDevice MinusCoffeeCosts(CoffeeDevice coffeeDevice, CoffeeLog coffeeLog)
        {
            coffeeDevice.CurrentWaterAmount  -= 10;
            coffeeDevice.CurrentCoffeeAmount -= 5;
            List <string> coffeeTypesWithMilk = new List <string>()
            {
                "Latte", "Cappuccino", "Mochaccino", "Macchiato", "Flat White"
            };

            if (coffeeTypesWithMilk.Contains(_dbContext.CoffeeTypes.FirstOrDefault(ct => ct.Id == coffeeLog.CoffeeTypeId).Name))
            {
                coffeeDevice.CurrentMilkAmount -= 10;
            }

            return(coffeeDevice);
        }