示例#1
0
        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);
        }
示例#2
0
 public Task Create(Task task)
 {
     task.DateClosed = null;
     task.DateAdded  = DateTime.Now;
     context.Tasks.Add(task);
     context.SaveChanges();
     return(task);
 }
示例#3
0
 public void Post([FromBody] Task task)
 {
     taskService.Create(task);
 }
示例#4
0
        public IActionResult Put(int id, [FromBody] Task task)
        {
            var result = taskService.Upsert(id, task);

            return(Ok(result));
        }