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

            context.Response.ContentType = "application/json";
            await JsonSerializer.SerializeAsync(context.Response.Body, todo, _options);
        }