/// <summary> /// Update a task. /// </summary> /// <param name="taskToUpdate"></param> /// <returns></returns> public async Task <int> Update(int taskId, TaskDto taskToUpdate) { try { using (var context = new WorkFlowContext()) { Entities.Task taskToUpdateFromDb = await context.Task.FindAsync(taskId); if (taskToUpdateFromDb != null) { taskToUpdateFromDb.Description = taskToUpdate.Description; taskToUpdateFromDb.Assignee = taskToUpdate.Assignee; taskToUpdateFromDb.AssignedBy = taskToUpdate.AssignedBy; taskToUpdateFromDb.Status = taskToUpdate.Status; taskToUpdateFromDb.AgreedCompletionDate = taskToUpdate.AgreedCompletionDate; return(await context.SaveChangesAsync()); } else { return(0); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Update the status of a task to In Progress or Done. /// </summary> /// <param name="taskId"></param> /// <param name="taskToUpdate"></param> /// <returns></returns> public async Task <int> UpdateStatus(int taskId, TaskDto taskToUpdate) { try { if (taskToUpdate.Status != "Done") { Entities.Task taskToDelete = new Entities.Task() { TaskId = taskId, Status = taskToUpdate.Status }; using (var context = new WorkFlowContext()) { context.Task.Attach(taskToDelete); context.Entry(taskToDelete).Property(t => t.Status).IsModified = true; return(await context.SaveChangesAsync()); } } else { using (var context = new WorkFlowContext()) { Entities.Task taskToUpdateFromDb = await context.Task.FindAsync(taskId); if (taskToUpdateFromDb != null) { taskToUpdateFromDb.Status = taskToUpdate.Status; taskToUpdateFromDb.ActualCompletionDate = taskToUpdate.ActualCompletionDate; return(await context.SaveChangesAsync()); } else { return(0); } } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Create a new task. /// </summary> /// <param name="taskToCreate"></param> /// <returns></returns> public async Task <int> Create(TaskDto taskToCreate) { try { if (taskToCreate != null) { Entities.Task taskEntityToCreate = Mapper.Map <Entities.Task>(taskToCreate); using (var context = new WorkFlowContext()) { context.Task.Add(taskEntityToCreate); return(await context.SaveChangesAsync()); } } return(0); } catch (Exception ex) { throw ex; } }
/// <summary> /// Delete a task. /// </summary> /// <param name="taskId"></param> /// <returns></returns> public async Task <int> Delete(int taskId) { try { Entities.Task taskToDelete = new Entities.Task() { TaskId = taskId, IsDeleted = true }; using (var context = new WorkFlowContext()) { context.Task.Attach(taskToDelete); context.Entry(taskToDelete).Property(t => t.IsDeleted).IsModified = true; return(await context.SaveChangesAsync()); } } catch (Exception ex) { throw ex; } }