public void DeleteTask(int id) { try { using(Entities db = new Entities()) { TaskItem task = db.TaskItems.SingleOrDefault(q => q.Id == id); if (task != null) { db.TaskItems.Remove(task); db.SaveChanges(); } else { throw new ApplicationException("Couldn't find the task"); } } } catch (Exception ex) { throw ex; } }
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; } }