Esempio n. 1
0
        public 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();
        }
Esempio n. 2
0
        public async Task DeleteAsync(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;
                return;
            }

            db.Todos.Remove(todo);
            await db.SaveChangesAsync();
        }