public bool Update(int id, TaskGroupViewModel group) { if (!TryFindGroupInDb(id, out var groupFromDb)) { return(false); } var updatedGroup = _mapper.Map <TaskGroupEntity>(group); _dbContext.Entry(groupFromDb).CurrentValues.SetValues(updatedGroup); foreach (var userTaskFromDb in groupFromDb.UserTasks.ToList()) { var task = updatedGroup.UserTasks.SingleOrDefault(t => t.Id == userTaskFromDb.Id); if (task == null) { _dbContext.Remove(userTaskFromDb); } else { _dbContext.Entry(userTaskFromDb).CurrentValues.SetValues(task); } } foreach (var newUserTasks in updatedGroup.UserTasks .Where(newUserTasks => newUserTasks.Id == 0 || groupFromDb.UserTasks.All(t => t.Id != newUserTasks.Id))) { groupFromDb.UserTasks.Add(newUserTasks); } _dbContext.SaveChanges(); return(true); }
public async Task HandleAsync(ToDoTaskDeletedEvent @event) { var toDoTask = await context.ToDoTasks.FindAsync(@event.AggregateId); context.Remove(toDoTask); await context.SaveChangesAsync(); }
public bool Delete(int id) { try { context.Remove(GetById(id)); context.SaveChanges(); return(true); } catch { return(false); } }
Task <ActionResult> DeleteTask(int id) { var taskToDelete = await _context.Tasks .SingleOrDefaultAsync(x => x.Id == id); if (taskToDelete == null) { return(BadRequest("Task Not Found")); } _context.Remove(taskToDelete); await _context.SaveChangesAsync(); return(Ok("Successfully Deleted Task")); }