public Task Upsert(int id, Task task) { var existing = context.Tasks.AsNoTracking().FirstOrDefault(t => t.Id == id); if (existing == null) { task.DateClosed = null; task.DateAdded = DateTime.Now; context.Tasks.Add(task); context.SaveChanges(); return(task); } task.Id = id; if (task.TaskState == TaskState.Closed && existing.TaskState != TaskState.Closed) { task.DateClosed = DateTime.Now; } else if (existing.TaskState == TaskState.Closed && task.TaskState != TaskState.Closed) { task.DateClosed = null; } context.Tasks.Update(task); context.SaveChanges(); return(task); }
public Task Create(Task task) { task.DateClosed = null; task.DateAdded = DateTime.Now; context.Tasks.Add(task); context.SaveChanges(); return(task); }
public void Post([FromBody] Task task) { taskService.Create(task); }
public IActionResult Put(int id, [FromBody] Task task) { var result = taskService.Upsert(id, task); return(Ok(result)); }