Exemple #1
0
            public async Task <TodoListEnvelope> Handle(Query q, CancellationToken ct)
            {
                if (q is null)
                {
                    throw new ArgumentNullException(nameof(q));
                }

                var strategy = _context.Database.CreateExecutionStrategy();

                var todo = await strategy.ExecuteAsync(async (ct) => {
                    var dbTodos = FilterTodo.ApplyFilters(_context.Todos, q.Filters);

                    var todos = await dbTodos
                                .Select(x => new Todo {
                        Done       = x.Done,
                        DueDateUtc = x.DueDateUtc,
                        Id         = x.Id,
                        Name       = x.Name,
                        Priority   = x.Priority
                    })
                                .ToListAsync(ct)
                                .ConfigureAwait(false);

                    var todoGroups = OrderTodo.GroupAndOrder(todos, q.GroupOrder, q.TodoOrder);

                    return(new TodoListEnvelope {
                        TodoList = todoGroups
                    });
                }, ct).ConfigureAwait(false);

                return(todo);
            }
Exemple #2
0
            public async Task <TodoPagedListEnvelope> Handle(Query q, CancellationToken ct)
            {
                if (q is null)
                {
                    throw new ArgumentNullException(nameof(q));
                }

                var strategy = _context.Database.CreateExecutionStrategy();

                var todo = await strategy.ExecuteAsync(async (ct) => {
                    using var t = await _context.Database.BeginTransactionAsync(IsolationLevel.ReadCommitted, ct).ConfigureAwait(false);

                    var dbTodos    = FilterTodo.ApplyFilters(_context.Todos, q.Filters);
                    var totalCount = await dbTodos.CountAsync(ct).ConfigureAwait(false);

                    var pagedTodos = dbTodos.Skip(q.Page.Value *q.ItemsPerPage.Value).Take(q.ItemsPerPage.Value);

                    var todos = await pagedTodos
                                .Select(x => new Todo {
                        Done       = x.Done,
                        DueDateUtc = x.DueDateUtc,
                        Id         = x.Id,
                        Name       = x.Name,
                        Priority   = x.Priority
                    })
                                .ToListAsync(ct)
                                .ConfigureAwait(false);

                    await t.CommitAsync(ct).ConfigureAwait(false);

                    var todoGroups = OrderTodo.GroupAndOrder(todos, q.GroupOrder, q.TodoOrder);

                    return(new TodoPagedListEnvelope {
                        TodoPaged = new Paged <System.Collections.Generic.SortedDictionary <DateTime, System.Collections.Generic.IReadOnlyCollection <Todo> > > {
                            Items = todoGroups,
                            ItemsPerPage = q.ItemsPerPage,
                            PageNum = q.Page,
                            TotalItems = totalCount
                        }
                    });
                }, ct).ConfigureAwait(false);

                return(todo);
            }