public async Task <Library.Task> ReplaceTask(Library.Task item, string listId = null) { List list = null; Library.Task task = null; if (listId != null) { list = await GetListById(listId); task = list.Tasks.Where(x => x.Id == item.Id).FirstOrDefault(); } else { var listTaskTuple = await GetListAndTaskByTaskId(item.Id); if (listTaskTuple == null) { return(null); } list = listTaskTuple.Item1; task = listTaskTuple.Item2; } list.Tasks.Remove(task); list.Tasks.Add(item); //Update List and return Task await this.listRepository.UpdateItemAsync(list.Id, list); return(item); }
private static void UnpackTask(SqlDataReader reader, List <Library.Task> items) { string id = reader["TaskId"].ToString(); Library.Task task = items.FirstOrDefault(x => x.Id == id) as Library.Task; if (task == null) { DateTime completedDate; bool completedDateFlag = DateTime.TryParse(reader["TaskCompletedDate"].ToString(), out completedDate); DateTime dueDate; bool dueDateFlag = DateTime.TryParse(reader["TaskDueDate"].ToString(), out dueDate); task = new Library.Task { Id = reader["TaskId"].ToString(), Title = reader["TaskTitle"].ToString(), Notes = reader["TaskNotes"].ToString(), Important = (bool)reader["TaskImportant"], CompletedDate = completedDateFlag ? completedDate : default(DateTime?), DueDate = dueDateFlag ? dueDate : default(DateTime?), }; items.Add(task); } }
public async System.Threading.Tasks.Task DeleteTask(string taskId, string listId = null) { List list = null; Library.Task task = null; if (listId != null) { list = await GetListById(listId); task = list.Tasks.Where(x => x.Id == taskId).FirstOrDefault(); } else { var listTaskTuple = await GetListAndTaskByTaskId(taskId); if (listTaskTuple != null) { list = listTaskTuple.Item1; task = listTaskTuple.Item2; } } if (task != null) { list.Tasks.Remove(task); await this.listRepository.UpdateItemAsync(list.Id, list); } }
public async Task <IHttpActionResult> Get(string id) { IEnumerable <Library.Task> tasks = await this.listService.Get <Library.Task>(id); Library.Task task = tasks?.FirstOrDefault(); return(task != null?this.Ok(task) : this.NotFound() as IHttpActionResult); }
public async Task <Library.Task> AddTaskToList(string listId, Library.Task item) { var list = await GetListById(listId); list.Tasks.Add(item); await this.listRepository.UpdateItemAsync(listId, list); return(item); }
public async Task <Library.Task> AddTaskToList(string listId, Library.Task item) { var list = await GetListById(listId); list.Tasks.Add(item); var updatedList = await this.listRepository.CreateOrUpdateList(list); return(updatedList.Tasks.FirstOrDefault(x => x.Id == item.Id)); }
public async Task <IHttpActionResult> PostTask(string listId, Library.Task task) { if (task.Title.ToLower().Contains("aws")) { throw new System.Exception("Someone tried to join aws"); } Library.Task t = await this.listService.AddTaskToList(listId, task); return(this.Created($"api/lists/{listId}/tasks/{t.Id}", t)); }
/// <summary> /// We cannot get a task directly without loading the list unless we stored the tasks in a seperate table. /// The API however requires us to search tasks by due date or whether they are important. /// Even if we used a seperate table, this would require us to use a 'Partition Scan Query' which is not as performant as partition and rowkey queries /// Therefore we have not split tasks into a seperate table. This is also not very performant if a user has a lot of lists, so the right choice it really depends on how we think the lists will be used by the users. /// These demos are created to discus the pro and cons of different database in Azure, and therefore these performance considerations are a design choice for a specific use case /// </summary> private async Task <Tuple <Library.List, Library.Task> > GetListAndTaskByTaskId(string id) { var lists = await GetAllLists(); Library.Task task = null; Library.List list = null; foreach (var l in lists) { var match = l.Tasks.FirstOrDefault(x => x.Id == id); if (match != null) { task = match; list = l; break; } } if (task == null) { return(null); } return(Tuple.Create(list, task)); }
private Task InsertOrUpdate <T>(string id, T item, bool insert = true) where T : Library.IIdentifiable { return(Task.Run(() => { using (SqlConnection connection = new SqlConnection(this.connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand()) { List <SqlParameter> parameters = new List <SqlParameter>(); parameters.Add(new SqlParameter("id", SqlDbType.NVarChar, 50) { Value = id }); command.Connection = connection; if (typeof(T) == typeof(Library.List)) { Library.List list = item as Library.List; command.CommandText += this.BuildListQuery(list, parameters, insert); if (list.Tasks != null) { command.CommandText += this.BuildTaskQuery(parameters, insert, list.Tasks.ToArray()); } } else if (typeof(T) == typeof(Library.Task)) { Library.Task task = item as Library.Task; command.CommandText += this.BuildTaskQuery(parameters, insert, task); } command.Parameters.AddRange(parameters.ToArray()); command.CommandType = CommandType.Text; command.ExecuteNonQuery(); } } })); }
public async Task <IHttpActionResult> Put(string id, Library.Task task) { await this.listService.Replace <Library.Task>(id, task); return(this.Created($"api/tasks/{id}", task)); }
public async Task <IHttpActionResult> Put(string listId, string taskId, Library.Task task) { await this.listService.Replace <Library.Task>(taskId, task); return(this.Created($"api/lists/{listId}/tasks/{taskId}", task)); }
public async Task <IHttpActionResult> PostTask(string listId, Library.Task task) { Library.Task t = await this.listService.Create(task, listId); return(this.Created($"api/lists/{listId}/tasks/{t.Id}", t)); }