예제 #1
0
        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();
        }
예제 #2
0
        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();
        }
예제 #3
0
        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();
        }