public IQueryable <ITimerGroup> GroupTimers(IQueryable <Timer> timers, TimersGroupFlags groupBy) { IQueryable <ITimerGroup> grouped = null; if ((groupBy & TimersGroupFlags.day) == TimersGroupFlags.day) { grouped = timers .Include(x => x.Project) // Todo Lazy loading is not implemented in ef core 1.0. So use eager loading .GroupBy(x => x.Started.Date) .Select(g => new DayTimerGroup { Date = g.Key, Childs = g.AsQueryable(), Duration = GetDuration(g.AsQueryable <Timer>()) }); } if ((groupBy & TimersGroupFlags.month) == TimersGroupFlags.month) { if ((groupBy & TimersGroupFlags.day) == TimersGroupFlags.day) { // We have already grouped by day. Just add group by month. grouped = grouped .GroupBy(x => new DateTime(x.Date.Year, x.Date.Month, 0)) .Select(g => new MonthTimerGroup { Date = g.Key, Childs = g.AsQueryable() }); } else { grouped = timers .GroupBy(x => new DateTime(x.Started.Date.Year, x.Started.Date.Month, 0)) .Select(g => new MonthTimerGroup { Date = g.Key, Childs = g.AsQueryable() }); } } return(grouped); }
public async Task <IActionResult> Statistics(int projectId, [FromQuery] DateTime start, [FromQuery] DateTime?end, [FromQuery] List <string> groupBy) { var project = await _projects.GetProjectAsync(projectId); if (project == null) { return(NotFound()); } if (!await _authorizationService.AuthorizeAsync(User, project, "IsOwner")) { return(NotFound()); } TimeZoneInfo timeZone = _projects.GetTimeZone(project); var timers = _timers.GetTimersInInterval(project, start, end); IQueryable grouped = null; if (groupBy.Count != 0) { TimersGroupFlags groupByFlag = TimersGroupFlags.none; if (groupBy.Contains("day")) { groupByFlag |= TimersGroupFlags.day; } if (groupBy.Contains("month")) { groupByFlag |= TimersGroupFlags.month; } grouped = _timers.GroupTimers(timers, groupByFlag); } return(new OkObjectResult(grouped ?? timers)); }