public async Task PostAsync(TodoDbContext db, HttpContext context) { var todo = await context.Request.ReadJsonAsync <TodoItem>(_options); if (await db.TodoItems.ContainsAsync(todo)) { db.TodoItems.Update(todo); } else { await db.TodoItems.AddAsync(todo); } await db.SaveChangesAsync(); }
public async Task PutAsync(TodoDbContext db, HttpContext context) { if (!context.Request.RouteValues.TryGet("id", out int id)) { context.Response.StatusCode = 400; return; } var todo = await context.Request.ReadJsonAsync <TodoItem>(_options); todo.Id = id; db.TodoItems.Update(todo); await db.SaveChangesAsync(); }
public async Task DeleteAsync(TodoDbContext db, HttpContext context) { if (!context.Request.RouteValues.TryGet("id", out int id)) { context.Response.StatusCode = 400; return; } var todo = await db.TodoItems.FindAsync(id); if (todo == null) { context.Response.StatusCode = 404; return; } db.TodoItems.Remove(todo); await db.SaveChangesAsync(); }