public async Task Post(HttpContext context) { var todo = await JsonSerializer.DeserializeAsync <Todo>(context.Request.Body, _options); _db.Todos.Add(todo); await _db.SaveChangesAsync(); }
public async Task PostAsync(HttpContext context) { var todo = await context.Request.ReadJsonAsync <Todo>(_options); await _db.Todos.AddAsync(todo); await _db.SaveChangesAsync(); }
public async Task Delete(HttpContext context) { var id = (string)context.Request.RouteValues["id"]; if (id == null || !long.TryParse(id, out var todoId)) { context.Response.StatusCode = StatusCodes.Status400BadRequest; return; } using var db = new TodoDbContext(); var todo = await db.Todos.FindAsync(todoId); if (todo == null) { context.Response.StatusCode = StatusCodes.Status404NotFound; await JsonSerializer.SerializeAsync(context.Response.Body, new { message = "Todo Not Found" }); return; } db.Todos.Remove(todo); await db.SaveChangesAsync(); await JsonSerializer.SerializeAsync(context.Response.Body, new { message = "Todo Not Found" }); }
static async Task UpdateCompleted(HttpContext context) { if (!context.Request.RouteValues.TryGet("id", out int id)) { context.Response.StatusCode = 400; return; } using var db = new TodoDbContext(); var todo = await db.Todos.FindAsync(id); if (todo == null) { context.Response.StatusCode = 404; return; } var inputTodo = await context.Request.ReadJsonAsync <Todo>(); todo.IsComplete = inputTodo.IsComplete; await db.SaveChangesAsync(); context.Response.StatusCode = 204; }
static async Task Post(HttpContext context) { var todo = await JsonSerializer.DeserializeAsync <Todo>(context.Request.Body, _options); using var db = new TodoDbContext(); db.Todos.Add(todo); await db.SaveChangesAsync(); }
public async Task PostAsync(HttpContext context) { var todo = await context.Request.ReadFromJsonAsync <Todo>(_options); using var db = new TodoDbContext(); await db.Todos.AddAsync(todo); await db.SaveChangesAsync(); }
static async Task PostAsync(HttpContext context) { var todo = await JsonSerializer.DeserializeAsync <Todo>(context.Request.Body, _options); using var db = new TodoDbContext(); await db.Todos.AddAsync(todo); await db.SaveChangesAsync(); context.Response.StatusCode = StatusCodes.Status204NoContent; }
static async Task CreateTodo(HttpContext context) { var todo = await context.Request.ReadJsonAsync <Todo>(); using var db = new TodoDbContext(); await db.Todos.AddAsync(todo); await db.SaveChangesAsync(); context.Response.StatusCode = 204; }
public async Task Post(HttpContext context) { var todo = await JsonSerializer.DeserializeAsync <Todo>(context.Request.Body, _options); using var db = new TodoDbContext(); db.Todos.Add(todo); await db.SaveChangesAsync(); await JsonSerializer.SerializeAsync(context.Response.Body, new { message = "Successfully created Todo" }); }
public async Task <IActionResult> Delete(long id, [FromServices] TodoDbContext db) { var todo = await db.Todos.FindAsync(id); if (todo == null) { return(NotFound()); } db.Todos.Remove(todo); await db.SaveChangesAsync(); return(Ok()); }
public async Task DeleteAsync(TodoDbContext db, HttpContext context) { var id = (string)context.Request.RouteValues["id"]; if (id == null || !long.TryParse(id, out var todoId)) { context.Response.StatusCode = StatusCodes.Status400BadRequest; return; } var todo = await db.Todos.FindAsync(todoId); if (todo == null) { context.Response.StatusCode = StatusCodes.Status404NotFound; return; } db.Todos.Remove(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.Todos.FindAsync(id); if (todo == null) { context.Response.StatusCode = 404; return; } db.Todos.Remove(todo); await db.SaveChangesAsync(); }
static async Task UpdateTodos(HttpContext context) { var todos = await context.Request.ReadJsonAsync <IEnumerable <Todo> >(); using var db = new TodoDbContext(); var others = await db.Todos.ToListAsync(); var toRemove = others.Except(todos); db.Todos.RemoveRange(toRemove); var toAdd = todos.Except(others); await db.Todos.AddRangeAsync(toAdd); var toUpdate = todos.Intersect(others); await db.SaveChangesAsync(); context.Response.StatusCode = 204; }
static async Task DeleteTodo(HttpContext context) { if (!context.Request.RouteValues.TryGet("id", out int id)) { context.Response.StatusCode = 400; return; } using var db = new TodoDbContext(); var todo = await db.Todos.FindAsync(id); if (todo == null) { context.Response.StatusCode = 404; return; } db.Todos.Remove(todo); await db.SaveChangesAsync(); context.Response.StatusCode = 204; }
static async Task UpdateAndAdd(HttpContext context) { if (!context.Request.RouteValues.TryGet("id", out int id)) { context.Response.StatusCode = 400; return; } using var db = new TodoDbContext(); var updateTodo = await db.Todos.FindAsync(id); updateTodo.Name = $"Update Task at {DateTime.Now}"; var newTodo = new Todo { Name = "New task" }; db.Add(newTodo); await db.SaveChangesAsync(); context.Response.StatusCode = 200; }
public async Task Post(Todo todo, [FromServices] TodoDbContext db) { db.Todos.Add(todo); await db.SaveChangesAsync(); }
public async Task Post(Todo todo) { _db.Todos.Add(todo); await _db.SaveChangesAsync(); }