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."); } }
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())); }
public UnCompleteTodo() : base("/todos") { Put("/{TodoId:Guid}/uncomplete", args => { TodoDataModel todo = TodoQuery.GetTodo(args.TodoId); todo.Complete = false; return(HttpStatusCode.OK); }); }
public RemoveTodo() : base("/todos") { Delete("/{TodoId:Guid}", args => { TodoDataModel todo = TodoQuery.GetTodo(args.TodoId); TodoQuery.RemoveTodo(todo); return(HttpStatusCode.OK); }); }
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); }
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)); }
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); }); }
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); }
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); }
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); }
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); }
public GetTodo() : base("/todos") { Get("/{TodoId:int}", args => TodoQuery.GetTodo(args.TodoId)); }
public async Task <TodoItem> GetByIdAndUserID(Guid id, string userId) => await _todoDbContext.TodoItems .AsNoTracking() .Where(TodoQuery.GetByIdAndUserId(id, userId)) .FirstOrDefaultAsync();
public GetTodos() : base("/todos") { Get("/", args => TodoQuery.GetTodos()); }
public TodoSchema() { Query = new TodoQuery(); Mutation = new TodoMutation(); }
private async Task <IList <TodoListItem> > InternalExecuteQueryAsync(TodoQuery query) { return(await _context.TodoItems.GetAsync(ToRetrievalOptions(query))); }
public async Task <ICollection <TodoItem> > GetAll(string userId) => await _todoDbContext.TodoItems.AsNoTracking() .Where(TodoQuery.GetAll(userId)) .OrderBy(x => x.CreateDate) .ToListAsync();