Exemple #1
0
        public async Task <IQueryable <T> > Sum(
            ODataQueryOptions <T> options,
            IQueryable <T> data)
        {
            var queryStr     = ExtractQueryString(options);
            var filterValue  = ExtractFilter(queryStr);
            var propertyName = ExtractPropertyName(queryStr);

            using (var context = new SumContext(options, filterValue))
            {
                var opt = context.GetOptions();

                // apply filtration to data
                var query = (IQueryable <T>)opt.ApplyTo(data);

                // get stub for hacking odata middleware
                var stub = await query.FirstOrDefaultAsync();

                if (stub == null)
                {
                    stub = new T();
                }

                // execute sum
                var sum = await EntityFrameworkQueryableExtensions.SumAsync(query, GetSumExpression(propertyName));

                // update stub
                GetUpdateStubFunc(propertyName, sum)(stub);

                return(new List <T> {
                    stub
                }
                       .AsQueryable());
            }
        }
Exemple #2
0
        public async Task <float> FloatSumAsync()
        {
            if (typeof(T) != typeof(float))
            {
                throw new InvalidOperationException($"{typeof(T)} cast to double failed");
            }
            var results = await GetGenericMergeEngine(async queryable => await EntityFrameworkQueryableExtensions.SumAsync((IQueryable <float>)queryable));

            return(results.Sum());
        }
        private async Task <TResult> GetSumAsync <TResult>(IQueryable <TResult> newSource)
        {
            var results = await GetStatisDataAsync <TResult>(x => EntityFrameworkQueryableExtensions.SumAsync((dynamic)x), newSource);

            return(Enumerable.Sum((dynamic)results));
        }
 public Task <float?> SumAsync <TSource>(
     IQueryable <TSource> source,
     Expression <Func <TSource, float?> > selector,
     CancellationToken cancellationToken = default) =>
 EntityFrameworkQueryableExtensions.SumAsync(source, selector, cancellationToken);
 public Task <float> SumAsync(IQueryable <float> source, CancellationToken cancellationToken = default) =>
 EntityFrameworkQueryableExtensions.SumAsync(source, cancellationToken);
Exemple #6
0
 /// <inheritdoc cref="EntityFrameworkQueryableExtensions.SumAsync{TSource}(IQueryable{TSource}, Expression{Func{TSource, decimal?}}, CancellationToken)"/>
 public static Task <decimal?> SumAsyncEF <TSource>(
     this IQueryable <TSource> source,
     Expression <Func <TSource, decimal?> > selector,
     CancellationToken cancellationToken = default)
 => EntityFrameworkQueryableExtensions.SumAsync(source, selector, cancellationToken);
Exemple #7
0
 /// <inheritdoc cref="EntityFrameworkQueryableExtensions.SumAsync(IQueryable{decimal?}, CancellationToken)"/>
 public static Task <decimal?> SumAsyncEF(
     this IQueryable <decimal?> source,
     CancellationToken cancellationToken = default)
 => EntityFrameworkQueryableExtensions.SumAsync(source, cancellationToken);
Exemple #8
0
 /// <inheritdoc cref="EntityFrameworkQueryableExtensions.SumAsync{TSource}(IQueryable{TSource}, Expression{Func{TSource, decimal?}}, CancellationToken)"/>
 public Task <decimal?> SumAsync <TSource>(
     IQueryable <TSource> source,
     Expression <Func <TSource, decimal?> > selector,
     CancellationToken token)
 => EntityFrameworkQueryableExtensions.SumAsync(source, selector, token);
 /// <inheritdoc cref="EntityFrameworkQueryableExtensions.SumAsync(IQueryable{double?}, CancellationToken)"/>
 public Task <double?> SumAsync(
     IQueryable <double?> source,
     CancellationToken cancellationToken)
 => EntityFrameworkQueryableExtensions.SumAsync(source, cancellationToken);
 public Task <double> SumAsync <TSource>(IQueryable <TSource> source, Expression <Func <TSource, double> > selector)
 {
     return(EntityFrameworkQueryableExtensions.SumAsync(source, selector));
 }
        public async Task <IActionResult> Get(DateTime?fromDate, DateTime?toDate)
        {
            if (!fromDate.HasValue || !toDate.HasValue)
            {
                if (!fromDate.HasValue)
                {
                    ModelState.AddModelError("fromDate", "Required Date is null or empty.");
                }
                if (!toDate.HasValue)
                {
                    ModelState.AddModelError("toDate", "Required Date is null or empty.");
                }
                return(new BadRequestObjectResult(ModelState));
            }

            var dateSpanDays = (toDate.Value - fromDate.Value).TotalDays + 1;

            if (dateSpanDays > 31)
            {
                ModelState.AddModelError("", "Date timespan is more than 31 days.");
                return(new BadRequestObjectResult(ModelState));
            }

            var fromDateValue = fromDate.Value;
            var toDateValue   = toDate.Value;
            var partitionId   = CurrentPartitionId;

            var report = new ReportApi
            {
                FromDate    = fromDateValue,
                ToDate      = toDateValue,
                DaysInMonth = (int)dateSpanDays,
            };

            report.GroupTaskTotals = await EntityFrameworkQueryableExtensions.ToListAsync(DbContext.TaskItems.Where(ti => ti.Task.Group.PartitionId == partitionId && ti.Time > 0 && ti.Date >= fromDateValue && ti.Date <= toDateValue)
                                                                                          .OrderBy(ti => ti.Task.Group.Name).ThenBy(ti => ti.Task.Name).GroupBy(ti => new { Group = ti.Task.Group.Name, Task = ti.Task.Name })
                                                                                          .Select(gw => new ReportGroupTaskApi
            {
                Group = gw.Key.Group,
                Task = gw.Key.Task,
                MonthTotal = gw.Sum(ti => ti.Time),
            }).AsNoTracking());

            // Ny version der udnytter grupper opdelingen...
            //var groups = DbContext.Groups.Where(g => g.PartitionId == partitionId && g.Tasks.Any(t => t.Items.Where(i => i.Date >= fromDateValue && i.Date <= toDateValue).Count() > 0));
            //report.GroupTaskTotals = groups.Select(g => new ReportGroupTaskApi
            //{
            //    Group = g.Name,
            //    Task = g.Tasks.Select(t => t.Name).FirstOrDefault(),
            //});

            report.MonthTotal = await EntityFrameworkQueryableExtensions.SumAsync(DbContext.TaskItems.Where(ti => ti.Task.Group.PartitionId == partitionId && ti.Time > 0 && ti.Date >= fromDateValue && ti.Date <= toDateValue)
                                                                                  .Select(ti => (int)ti.Time));

            report.Users = DbContext.TaskItems.Where(ti => ti.Task.Group.PartitionId == partitionId && ti.Time > 0 && ti.Date >= fromDateValue && ti.Date <= toDateValue)
                           .OrderBy(ti => ti.User.FullName).GroupBy(ti => new { ti.User })
                           .Select(gw => new ReportUserApi
            {
                Id         = gw.Key.User.Id,
                FullName   = gw.Key.User.FullName,
                GroupTasks = gw.OrderBy(ti => ti.Task.Group.Name).ThenBy(ti => ti.Task.Name).GroupBy(ti => new { Group = ti.Task.Group.Name, Task = ti.Task.Name })
                             .Select(ggw => new ReportGroupTaskApi
                {
                    Group = ggw.Key.Group,
                    Task  = ggw.Key.Task,
                    Works = ggw.GroupBy(ti => ti.Date)
                            .Select(gdw => new ReportWorkApi
                    {
                        Day  = gdw.Key.Day,
                        Time = gdw.Sum(ti => ti.Time)
                    }).OrderBy(gdw => gdw.Day).ToList(),
                    MonthTotal = ggw.Sum(ti => (int)ti.Time)
                }).ToList(),
                MonthTotal = gw.Sum(ti => ti.Time)
            }).OrderBy(iu => iu.FullName).AsNoTracking().ToList();

            return(Json(report));
        }
        public async Task <IActionResult> Get(DateTime?date, string[] cuid)
        {
            if (!date.HasValue)
            {
                ModelState.AddModelError("date", "Required Date is null or empty.");
                return(new BadRequestObjectResult(ModelState));
            }
            // Only pass Date through
            date = date.Value.Date;

            var firstDayOfMonth     = date.Value.FirstDayOfMonth();
            var firstDayOfNextMonth = date.Value.FirstDayOfNextMonth();
            var firstDayOfWeek      = date.Value.FirstDayOfWeek();
            var firstDayOfNextWeek  = date.Value.FirstDayOfNextWeek();

            var timeGroups = DbContext.Groups.Where(g => g.PartitionId == CurrentPartitionId &&
                                                    (DbContext.DefaultGroups.Any(tdg => tdg.UserId == CurrentUserId && tdg.GroupId == g.Id) ||
                                                     g.Tasks.Where(t => t.Items.Where(i => i.UserId == CurrentUserId && i.Date == date.Value).Count() > 0 || DbContext.DefaultTasks.Any(dt => dt.UserId == CurrentUserId && dt.Task == t)).Count() > 0 ||            // GroupsWhereTasks, work around code copied in
                                                     g.Materials.Where(m => m.Items.Where(i => i.UserId == CurrentUserId && i.Date == date.Value).Count() > 0 || DbContext.DefaultMaterials.Any(dm => dm.UserId == CurrentUserId && dm.Material == m)).Count() > 0)) // GroupsWhereMaterials, work around code copied in
                             .Select(g => new TimeGroupApi
            {
                Id    = g.Id,
                Name  = g.Name,
                Tasks = g.Tasks.Where(t => t.Items.Where(i => i.UserId == CurrentUserId && i.Date == date.Value).Count() > 0 || DbContext.DefaultTasks.Any(dt => dt.UserId == CurrentUserId && dt.Task == t))      // GroupsWhereTasks, work around code copied in
                        .Select(t => new TimeTaskApi
                {
                    Id   = t.Id,
                    Name = t.Name,
                    Item = t.Items.Where(i => i.UserId == CurrentUserId && i.Date == date.Value).Select(w => new TimeTaskItemApi
                    {
                        Id   = w.Id,
                        Time = w.Time,
                    }).FirstOrDefault(),
                }).OrderBy(t => t.Name).ToList(),
                DayTaskTotal   = DbContext.TaskItems.Where(ti => ti.UserId == CurrentUserId && ti.Task.GroupId == g.Id && ti.Date == date.Value).Select(ti => (int)ti.Time).Sum(),
                WeekTaskTotal  = DbContext.TaskItems.Where(ti => ti.UserId == CurrentUserId && ti.Task.GroupId == g.Id && ti.Date >= firstDayOfWeek && ti.Date < firstDayOfNextWeek).Select(ti => (int)ti.Time).Sum(),
                MonthTaskTotal = DbContext.TaskItems.Where(ti => ti.UserId == CurrentUserId && ti.Task.GroupId == g.Id && ti.Date >= firstDayOfMonth && ti.Date < firstDayOfNextMonth).Select(ti => (int)ti.Time).Sum(),
            }).OrderBy(g => g.Name).ToList();     // do not currently work with Async...


            var uniqIdCount = 1;
            var pageItems   = new List <DashboardItemApi>();

            foreach (var group in timeGroups)
            {
                pageItems.Add(new DashboardItemApi
                {
                    Type           = DashboardItemTypes.Group,
                    UniqId         = GroupUniquId(date.Value, cuid, group),
                    Id             = group.Id,
                    Name           = group.Name,
                    DayTaskTotal   = group.DayTaskTotal,
                    WeekTaskTotal  = group.WeekTaskTotal,
                    MonthTaskTotal = group.MonthTaskTotal
                });

                foreach (var task in group.Tasks)
                {
                    pageItems.Add(new DashboardItemApi
                    {
                        Type           = DashboardItemTypes.Task,
                        UniqId         = GroupTaskUniquId(date.Value, cuid, group, task),
                        Id             = task.Id,
                        GroupId        = group.Id,
                        Name           = task.Name,
                        TaskItemId     = task.Item?.Id,
                        Time           = task.Item?.Time,
                        WeekTaskTotal  = await EntityFrameworkQueryableExtensions.SumAsync(DbContext.TaskItems.Where(ti => ti.UserId == CurrentUserId && ti.TaskId == task.Id && ti.Date >= firstDayOfWeek && ti.Date < firstDayOfNextWeek).Select(ti => (int)ti.Time)),
                        MonthTaskTotal = await EntityFrameworkQueryableExtensions.SumAsync(DbContext.TaskItems.Where(ti => ti.UserId == CurrentUserId && ti.TaskId == task.Id && ti.Date >= firstDayOfMonth && ti.Date < firstDayOfNextMonth).Select(ti => (int)ti.Time))
                    });
                }

                pageItems.Add(new DashboardItemApi {
                    State = DashboardItemStates.Button, Type = DashboardItemTypes.Task, GroupId = group.Id, UniqId = SpaceUniquId(date.Value, uniqIdCount++)
                });
            }
            pageItems.Add(new DashboardItemApi {
                State = DashboardItemStates.Button, Type = DashboardItemTypes.Group, UniqId = SpaceUniquId(date.Value, uniqIdCount++)
            });



            return(Json(pageItems));
        }
 /// <inheritdoc cref="EntityFrameworkQueryableExtensions.SumAsync{TSource}(IQueryable{TSource}, Expression{Func{TSource, long}}, CancellationToken)"/>
 public Task <long> SumAsync <TSource>(
     IQueryable <TSource> source,
     Expression <Func <TSource, long> > selector,
     CancellationToken cancellationToken)
 => EntityFrameworkQueryableExtensions.SumAsync(source, selector, cancellationToken);
Exemple #14
0
 public Task <float?> SumAsync(IQueryable <float?> source, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(EntityFrameworkQueryableExtensions.SumAsync(source, cancellationToken));
 }
Exemple #15
0
 /// <inheritdoc cref="EntityFrameworkQueryableExtensions.SumAsync(IQueryable{decimal?}, CancellationToken)"/>
 public Task <decimal?> SumAsync(
     IQueryable <decimal?> source,
     CancellationToken token)
 => EntityFrameworkQueryableExtensions.SumAsync(source, token);
Exemple #16
0
 public Task <decimal> SumAsync <TSource>(IQueryable <TSource> source, Expression <Func <TSource, decimal> > selector, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(EntityFrameworkQueryableExtensions.SumAsync(source, selector, cancellationToken));
 }
Exemple #17
0
 public static Task <float?> SumAsyncEF(
     this IQueryable <float?> source,
     CancellationToken token = default)
 => EntityFrameworkQueryableExtensions.SumAsync(source, token);