public TaskDto GetTask(int id) { TaskDto taskDto = null; try { using (Entities db = new Entities()) { var task = db.TaskItems.SingleOrDefault(q => q.Id == id); if (task != null) { taskDto = new TaskDto(); taskDto.Id = task.Id; taskDto.Title = task.Title; taskDto.Details = task.Details; taskDto.DueDate = task.DueDate; taskDto.IsComplete = task.CompletedDate != null; taskDto.CreatedBy = task.CreatedBy; taskDto.CreatedDate = task.CreatedDate; taskDto.ModifiedBy = task.ModifiedBy; taskDto.ModifiedDate = task.ModifiedDate; } else { throw new ApplicationException("Couldn't find the task"); } } } catch (Exception ex) { throw ex; } return taskDto; }
public void SaveTask(TaskDto dto) { TaskItem task = null; try { using (Entities db = new Entities()) { if (dto.Id == 0) // Insert { task = new TaskItem(); } else // Updating an existing record { task = db.TaskItems.SingleOrDefault(q => q.Id == dto.Id); if (task == null) { throw new ApplicationException("Couldn't find the task to update."); } } task.Id = dto.Id; task.Title = dto.Title; task.Details = dto.Details; task.DueDate = dto.DueDate; if (dto.Id == 0) { task.CreatedBy = dto.CreatedBy; task.CreatedDate = DateTime.Now; db.TaskItems.Add(task); db.SaveChanges(); } else { if (dto.IsComplete) { task.CompletedDate = DateTime.Now; } task.ModifiedBy = dto.ModifiedBy; task.ModifiedDate = DateTime.Now; db.TaskItems.Attach(task); db.Entry(task).State = EntityState.Modified; db.SaveChanges(); } } } catch (Exception ex) { throw ex; } }