public async Task<ToDo> Get(Guid todoId, Guid userId) { using (var dbContext = new Context.ToDoContext()) { var todo = await dbContext.ToDo.FirstOrDefaultAsync(n => n.ToDoId.Equals(todoId) && !n.Completed && n.UserId.Equals(userId) && n.Active) ?? new ToDo(); var todoReturn = new ToDo { ToDoId = todo.ToDoId, Description = todo.Description, Completed = todo.Completed, UserId = todo.UserId }; return await Task.Run(() => todoReturn); } }
public async Task<ToDo> Post(Guid userId, ToDo todo) { using (var dbContext = new Context.ToDoContext()) { dbContext.ToDo.Add(new Entities.ToDo { ToDoId = todo.ToDoId = Guid.NewGuid(), Description = todo.Description, Completed = false, Active = true, UserId = todo.UserId = userId, CreatedAt = DateTime.Now, UpDatedAt = DateTime.Now }); await dbContext.SaveChangesAsync(); return await Task.Run(() => todo); } }
public async Task<ToDo> Put(Guid todoId, ToDo todo) { using (var dbContext = new Context.ToDoContext()) { var todoModel = await dbContext.ToDo.FirstOrDefaultAsync(n => n.ToDoId.Equals(todoId)); todoModel.Description = todo.Description; todoModel.Completed = todo.Completed; todoModel.UpDatedAt = DateTime.Now; await dbContext.SaveChangesAsync(); todo.Completed = todoModel.Completed; todo.ToDoId = todoModel.ToDoId; todo.UserId = todoModel.UserId; return await Task.Run(() => todo); } }