public bool IsBoilWaterNeeded(string appUserId, DateTime dateTime) { Teapot teapot = _dbContext.Teapots .Include(t => t.TeapotLogs) .ThenInclude(tl => tl.HowOften) .FirstOrDefault(t => t.AppUserId == appUserId); TeapotLog currentTeapotLog = teapot.TeapotLogs.FirstOrDefault(tl => tl.IsRepeatable != true && IsDateTimesEquals(tl.Date, dateTime)); if (currentTeapotLog != null) { UpdateTeapotWhenBoil(teapot, currentTeapotLog); return(true); } List <TeapotLog> repeatableTeapotLogs = teapot.TeapotLogs.Where(tl => tl.IsRepeatable == true).ToList(); currentTeapotLog = CheckIfBoilWaterNeeded(repeatableTeapotLogs, dateTime); if (currentTeapotLog != null) { teapot = UpdateTeapotWhenBoil(teapot, currentTeapotLog); return(true); } _dbContext.SaveChanges(); return(false); }
private TeapotLog CheckIfBoilWaterNeeded(List <TeapotLog> teapotLogs, DateTime dateTime) { TeapotLog result = null; List <int> dayOfWeeksId = new List <int>() { 3, 4, 5, 6, 7, 8, 9 }; foreach (TeapotLog teapotLog in teapotLogs) { if (IsDateTimesEquals(dateTime, teapotLog.Date) && _dbContext.TeapotLogs.FirstOrDefault(tl => tl.TeapotId == teapotLog.TeapotId && IsDateTimesEquals(teapotLog.Date, tl.Date)) == null) { result = teapotLog; } if (teapotLog.HowOften.Id == 2 && IsTimesEquals(dateTime, teapotLog.Date)) { result = teapotLog; } else if (teapotLog.HowOften.Id == 10 && IsDayOfWeekWeekDay(dateTime) && IsTimesEquals(dateTime, teapotLog.Date)) { result = teapotLog; } else if (teapotLog.HowOften.Id == 11 && !IsDayOfWeekWeekDay(dateTime) && IsTimesEquals(dateTime, teapotLog.Date)) { result = teapotLog; } else if (dayOfWeeksId.Contains(teapotLog.HowOften.Id) && IsDayOfWeekAndTimeEquals(teapotLog.Date, dateTime)) { result = teapotLog; } } return(result); }
public void BoilWater(TeapotLog teapotLog) { _dbContext.TeapotLogs.Add(teapotLog); if (IsDateTimesEquals(DateTime.Now, teapotLog.Date)) { Teapot teapot = _dbContext.Teapots.FirstOrDefault(t => t.Id == teapotLog.TeapotId); teapot = UpdateTeapotWhenBoil(teapot, teapotLog); } _dbContext.SaveChanges(); }
public JsonResult BoilWater(RequestTeapotLog requestTeapotLog) { object teapotState = GetCurrentUserTeapotState(requestTeapotLog.appUserId); if (teapotState != null) { TeapotLog teapotLog = RequestTeapotLogToTeapotLog(requestTeapotLog); _teapotRepository.BoilWater(teapotLog); return(new JsonResult(new { message = "ok", teapotState = GetCurrentUserTeapotState(requestTeapotLog.appUserId) })); } else { return(new JsonResult(new { message = "error", teapotState = GetCurrentUserTeapotState(requestTeapotLog.appUserId) })); } }
private Teapot UpdateTeapotWhenBoil(Teapot teapot, TeapotLog teapotLog) { teapot.CurrentTemperature = teapotLog.Temperature; return(teapot); }