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 async Task <ActionResult <TodoList> > CreateListAsync([FromBody] TodoList list) { var lists = await _context.TodoLists.GetAsync(l => l.Id != DefaultList.id); PositionAdjuster.AdjustForCreate(list, lists.ToList <ISortable>(), list.Items.ToList <ISortable>()); await _context.TodoLists.AddAsync(list); await _context.SaveChangesAsync(); return(CreatedAtRoute("GetList", new { id = list.Id }, list)); }
public async Task <ActionResult <TodoListItem> > CreateItemAsync([FromBody] TodoListItem item) { // sort items based on requested position var items = await _context.TodoItems.GetAsync(t => t.TodoListId == item.TodoListId); PositionAdjuster.AdjustForCreate(item, items.ToList <ISortable>()); await _context.TodoItems.AddAsync(item); await _context.SaveChangesAsync(); return(CreatedAtRoute("GetItem", new { id = item.Id }, item)); }
public static async void CreateAsync(ITodoRepositoryContext context) { if (context.TodoLists.Count() == 0) { await context.TodoLists.AddAsync(new TodoList() { Name = "Default" }); await context.SaveChangesAsync(); } }
public async Task <ActionResult <TodoElement> > UpdateListElementAsync(int id, [FromBody] TodoElement element) { var current = await _context.TodoLists.GetAsync(id, s => s.Items); if (current == null) { return(NotFound()); } if (id != DefaultList.id) { // if default list is being updated, no need to adjust other lists positions var lists = await _context.TodoLists.GetAsync(l => l.Id != DefaultList.id); PositionAdjuster.AdjustForUpdate(element, lists.ToList <ISortable>(), current); } current.UpdateFrom(element); _context.TodoLists.Update(current); await _context.SaveChangesAsync(); return(current.ToElement()); }
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 <TodoItemReference> > MoveReferenceAsync(int id, [FromBody] TodoItemReference reference) { var current = await _context.TodoReferences.GetAsync(id); if (current == null) { return(NotFound()); } // re-arrange other references affected by move var references = await _context.TodoReferences.GetAsync(r => r.TodoQueryId == reference.TodoQueryId); PositionAdjuster.AdjustForUpdate(reference, references.ToList <ISortable>(), current); // move reference to desired location current.UpdateFrom(reference); _context.TodoReferences.Update(current); await _context.SaveChangesAsync(); return(current); }