Ejemplo n.º 1
0
        private Expression <Func <TodoListItem, object> > GetSortByExpression(TodoQuery query)
        {
            var operandString = GetOperandString(query.OrderBy.Value);

            if (query.OrderBy.Value == QueryOperand.Important || query.OrderBy.Value == QueryOperand.Done)
            {
                return((Expression <Func <TodoListItem, object> >)DynamicExpressionParser.ParseLambda(
                           new[] { _itemParameter },
                           typeof(object),
                           $"{operandString}"));
            }
            else if (query.OrderBy.Value == QueryOperand.DueDate)
            {
                // bounds tells sorting what to do with null dates
                var direction = query.OrderByDirection ?? QueryDirection.Ascending;
                var bounds    = direction == QueryDirection.Ascending ? DateTime.MaxValue : DateTime.MinValue;
                return((Expression <Func <TodoListItem, object> >)DynamicExpressionParser.ParseLambda(
                           new[] { _itemParameter },
                           typeof(object),
                           $"{operandString} ?? @0", bounds));
            }
            else
            {
                throw new InvalidOperationException("Invalid operand.");
            }
        }
Ejemplo n.º 2
0
        private Expression <Func <TodoListItem, bool> > GetWhereExpression(TodoQuery query)
        {
            var           expression = new StringBuilder();
            List <object> values     = new List <object>(10);

            foreach (var predicate in query.Predicates)
            {
                if (predicate.Group.HasValue && predicate.Group.Value == QueryPredicateGroup.Begin)
                {
                    expression.Append("(");
                }
                object value = GetPredicateValue(predicate);
                expression.Append($"{GetOperandComparisonString(predicate.Operand, value)}");
                expression.Append($" {GetOperatorString(predicate.Operator)}");
                expression.Append($" @{values.Count}");
                values.Add(value);
                if (predicate.Group.HasValue && predicate.Group.Value == QueryPredicateGroup.End)
                {
                    expression.Append(")");
                }
                if (predicate.Keyword.HasValue)
                {
                    expression.Append($" {GetKeywordString(predicate.Keyword.Value)} ");
                }
            }

            return((Expression <Func <TodoListItem, bool> >)DynamicExpressionParser.ParseLambda(
                       new[] { _itemParameter },
                       typeof(bool),
                       expression.ToString(),
                       values.ToArray()));
        }
Ejemplo n.º 3
0
 public UnCompleteTodo() : base("/todos")
 {
     Put("/{TodoId:Guid}/uncomplete", args =>
     {
         TodoDataModel todo = TodoQuery.GetTodo(args.TodoId);
         todo.Complete      = false;
         return(HttpStatusCode.OK);
     });
 }
Ejemplo n.º 4
0
 public RemoveTodo() : base("/todos")
 {
     Delete("/{TodoId:Guid}", args =>
     {
         TodoDataModel todo = TodoQuery.GetTodo(args.TodoId);
         TodoQuery.RemoveTodo(todo);
         return(HttpStatusCode.OK);
     });
 }
Ejemplo n.º 5
0
        private async Task DeleteQuery(TodoQuery query)
        {
            // delete last results
            var results = await _context.TodoReferences.GetAsync(r => r.TodoQueryId == query.Id);

            results.ForEach(r => _context.TodoReferences.Delete(r));

            // and query
            _context.TodoQueries.Delete(query);
        }
Ejemplo n.º 6
0
        public async Task <ActionResult <TodoQuery> > CreateQueryAsync([FromBody] TodoQuery query)
        {
            // adjust predicate positions to match their positions in collection
            PositionAdjuster.AdjustChildren(query.Predicates.ToList <ISortable>());

            await _context.TodoQueries.AddAsync(query);

            await _context.SaveChangesAsync();

            return(CreatedAtRoute("GetQuery", new { id = query.Id }, query));
        }
Ejemplo n.º 7
0
 public AddTodo() : base("/todos")
 {
     Post("/create", args =>
     {
         var post      = this.Bind <CreateModel>();
         var nextOrder = TodoQuery.GetNextOrder();
         var todo      = new TodoDataModel(post.Title, nextOrder);
         TodoQuery.AddTodo(todo);
         return(HttpStatusCode.OK);
     });
 }
Ejemplo n.º 8
0
        public async Task <List <TodoItemReference> > ExecuteQueryAsync(TodoQuery query)
        {
            // get last set of results
            var lastResults = await _context.TodoReferences.GetAsync(r => r.TodoQueryId == query.Id, r => r.Item);

            // re-execute query (get new list items)
            var matchedItems = await InternalExecuteQueryAsync(query);

            // re-arrange new results based on existing result positions
            var mergedResults = await MergeResultsAsync(query.Id, lastResults, matchedItems);

            await _context.SaveChangesAsync();

            return(mergedResults);
        }
Ejemplo n.º 9
0
        public async Task <ActionResult <TodoQuery> > UpdateQueryAsync(int id, [FromBody] TodoQuery query)
        {
            var current = await FetchQueryAsync(id);

            if (current == null)
            {
                return(NotFound());
            }

            // adjust predicate positions to match their positions in collection
            PositionAdjuster.AdjustChildren(query.Predicates.ToList <ISortable>());

            current.UpdateFrom(query);

            _context.TodoQueries.Update(current);
            await _context.SaveChangesAsync();

            return(current);
        }
Ejemplo n.º 10
0
        private RetrievalOptions <TodoListItem> ToRetrievalOptions(TodoQuery query)
        {
            var options = new RetrievalOptions <TodoListItem>();

            if (query.OrderBy.HasValue)
            {
                options.OrderBy.Predicate = GetSortByExpression(query);
                if (!query.OrderByDirection.HasValue)
                {
                    options.OrderBy.Ascending = true;
                }
                else
                {
                    options.OrderBy.Ascending = query.OrderByDirection.Value == QueryDirection.Ascending;
                }
            }

            options.Where = GetWhereExpression(query);
            return(options);
        }
Ejemplo n.º 11
0
        public async Task <Result <TodoItem> > GetTodos(TodoQuery query, User user)
        {
            var result = new Result <TodoItem>();

            var q = context.TodoItems.Where(i => i.User == user);

            if (query.Limit <= 0)
            {
                query.Limit = 10;
            }
            if (query.Offset <= 0)
            {
                query.Offset = 0;
            }
            q = q.Skip(query.Offset).Take(query.Limit);

            result.data = await q.ToListAsync();

            result.TotalItems = await q.CountAsync();

            return(result);
        }
Ejemplo n.º 12
0
 public GetTodo() : base("/todos")
 {
     Get("/{TodoId:int}", args => TodoQuery.GetTodo(args.TodoId));
 }
Ejemplo n.º 13
0
 public async Task <TodoItem> GetByIdAndUserID(Guid id, string userId) =>
 await _todoDbContext.TodoItems
 .AsNoTracking()
 .Where(TodoQuery.GetByIdAndUserId(id, userId))
 .FirstOrDefaultAsync();
Ejemplo n.º 14
0
 public GetTodos() : base("/todos")
 {
     Get("/", args => TodoQuery.GetTodos());
 }
Ejemplo n.º 15
0
 public TodoSchema()
 {
     Query    = new TodoQuery();
     Mutation = new TodoMutation();
 }
Ejemplo n.º 16
0
 private async Task <IList <TodoListItem> > InternalExecuteQueryAsync(TodoQuery query)
 {
     return(await _context.TodoItems.GetAsync(ToRetrievalOptions(query)));
 }
Ejemplo n.º 17
0
 public async Task <ICollection <TodoItem> > GetAll(string userId) =>
 await _todoDbContext.TodoItems.AsNoTracking()
 .Where(TodoQuery.GetAll(userId))
 .OrderBy(x => x.CreateDate)
 .ToListAsync();