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); }
private CoffeeDeviceState CoffeDeviceToCoffeeSeviceState(CoffeeDevice coffeeDevice) { return(new CoffeeDeviceState() { CurrentCoffeeAmount = coffeeDevice.CurrentCoffeeAmount, CurrentMilkAmount = coffeeDevice.CurrentMilkAmount, CurrentWaterAmount = coffeeDevice.CurrentWaterAmount }); }
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(); }
// in controller set water, milk, coffee amount to random number public bool AddCoffeeDevice(CoffeeDevice coffeeDevice) { if (_dbContext.CoffeDevices.FirstOrDefault(cd => cd.AppUserId == coffeeDevice.AppUserId) == null) { _dbContext.CoffeDevices.Add(coffeeDevice); _dbContext.SaveChanges(); return(true); } return(false); }
public CoffeeDevice UpdateCoffeSeviceState(string appUserId) { CoffeeDevice coffeeDevice = GetCoffeeDeviceByUserId(appUserId); coffeeDevice.CurrentCoffeeAmount = 100; coffeeDevice.CurrentMilkAmount = 100; coffeeDevice.CurrentWaterAmount = 100; _dbContext.SaveChanges(); return(coffeeDevice); }
private CoffeeDeviceState GetCurrentUserCoffeeDeviceState(string appUserId) { CoffeeDevice coffeeDevice = _coffeeRepository.GetCoffeeDeviceByUserId(appUserId); if (coffeeDevice != null) { return(CoffeDeviceToCoffeeSeviceState(coffeeDevice)); } else { return(null); } }
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); }
public List <CoffeeLog> GetPreferableCoffeeTimes(string appUserId) { List <CoffeeLog> result = new List <CoffeeLog>(); CoffeeDevice coffeeDevice = _dbContext.CoffeDevices.FirstOrDefault(cd => cd.AppUserId == appUserId); if (coffeeDevice != null && _dbContext.CoffeeLogs.Where(cl => cl.CoffeeDeviceId == coffeeDevice.Id && cl.IsRepeatable == true).FirstOrDefault() != null) { result = _dbContext.CoffeeLogs.Where(cl => cl.CoffeeDeviceId == coffeeDevice.Id && cl.IsRepeatable == true).ToList(); } else if (coffeeDevice != null) { result = null; } return(result); }
public List <CoffeeType> GetFavouriteCoffeeTypes(string appUserId) { List <CoffeeType> coffeeTypes = new List <CoffeeType>(); CoffeeDevice coffeeDevice = _dbContext.CoffeDevices .Include(cd => cd.CoffeeLogs) .ThenInclude(cl => cl.CoffeeType) .FirstOrDefault(cd => cd.AppUserId == appUserId); if (coffeeDevice != null && coffeeDevice.CoffeeLogs != null) { coffeeTypes.Add(coffeeDevice.CoffeeLogs.GroupBy(cl => cl.CoffeeType).OrderByDescending(cl => cl.Count()).FirstOrDefault().Key); coffeeTypes.Add(coffeeDevice.CoffeeLogs.GroupBy(cl => cl.CoffeeType).OrderByDescending(cl => cl.Count()).Skip(1).Take(1).FirstOrDefault().Key); coffeeTypes.Add(coffeeDevice.CoffeeLogs.GroupBy(cl => cl.CoffeeType).OrderByDescending(cl => cl.Count()).Skip(2).Take(1).FirstOrDefault().Key); } return(coffeeTypes); }
public JsonResult AddCoffeeDevice(string appUserId) { Random random = new Random(); CoffeeDevice coffeeDevice = new CoffeeDevice() { Id = Guid.NewGuid(), AppUserId = appUserId, CurrentCoffeeAmount = random.Next(0, 100), CurrentMilkAmount = random.Next(0, 100), CurrentWaterAmount = random.Next(0, 100) }; bool isAdded = _coffeeRepository.AddCoffeeDevice(coffeeDevice); if (isAdded) { return(new JsonResult(new { message = "ok", coffeDeviceState = GetCurrentUserCoffeeDeviceState(appUserId) })); } else { return(new JsonResult(new { message = "error", coffeDeviceState = GetCurrentUserCoffeeDeviceState(appUserId) })); } }