protected async override Task <ScheduleQueryResponse> HandleCore(ScheduleQuery request) { var requestedWeek = new WorkingWeek(request.Date ?? DateTime.Today); var previousWeek = requestedWeek.GetPrevious(); var nextWeek = requestedWeek.GetNext(); var entries = await AddWeekFilter(_supportContext.ScheduleEntries, requestedWeek) .Include(e => e.Engineer) .OrderBy(e => e.Date) .ThenBy(e => e.Shift) .ToListAsync(); var hasPrevious = await AddWeekFilter(_supportContext.ScheduleEntries, previousWeek).AnyAsync(); var hasNext = await AddWeekFilter(_supportContext.ScheduleEntries, nextWeek).AnyAsync(); return(new ScheduleQueryResponse { Start = requestedWeek.Start, End = requestedWeek.End, NextWeek = hasNext ? nextWeek.Start.ToQueryParam() : null, PreviousWeek = hasPrevious ? previousWeek.Start.ToQueryParam() : null, Entries = entries.Select(e => (new ScheduleEntryDto { Date = DateTime.SpecifyKind(e.Date, DateTimeKind.Utc), Engineer = e.Engineer.Name, Shift = e.Shift })).ToList() }); }
public List <ScheduleEntry> GenerateSchedule(ScheduleGenerationRequest request) { var date = request.Date; //var week = WorkingWeek.GetThisOrUpcoming(date); var week = new WorkingWeek(date); var lastDueDayEntries = request.LastDueDayEntries; var lastEntryDate = lastDueDayEntries.LastOrDefault()?.Date; if (!date.IsWeekDay() || week.Contains(lastEntryDate)) { week = week.GetNext(); } if (lastEntryDate.HasValue) { //if week have entries or future entries exists just exit if (!week.IsAfter(lastEntryDate)) { return(new List <ScheduleEntry>()); } //entries is too old to consider var previousWeek = week.GetPrevious(); if (!previousWeek.Contains(lastEntryDate)) { lastDueDayEntries = new List <ScheduleEntry>(); } } var result = GenerateSchedule(request.Engineers, week, lastDueDayEntries); if (week.Contains(date)) //first call was for current week let generate one more { result.AddRange( GenerateSchedule( request.Engineers, week.GetNext(), result.TakeLast(Constants.ShiftsPerDay).ToList())); } return(result); }