Param binding refactor (#27)

pull/28/head
Rick Anderson 2022-08-31 15:14:32 -10:00 committed by GitHub
parent 17e4c47647
commit e5b2cac112
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 38 deletions

View File

@ -0,0 +1,9 @@
namespace TodoApi.Models;
public class Todo
{
public int Id { get; set; }
public string? Name { get; set; }
public bool IsComplete { get; set; }
public string? Secret { get; set; }
}

View File

@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;
namespace TodoApi.Models;
class TodoDb : DbContext
{
public TodoDb(DbContextOptions<TodoDb> options)
: base(options) { }
public DbSet<Todo> Todos => Set<Todo>();
}
record TodoItemRequest(int Id, TodoDb Db);
record CreateTodoItemRequest(TodoItemDTO Dto, TodoDb Db);
record EditTodoItemRequest(int Id, TodoItemDTO Dto, TodoDb Db);

View File

@ -0,0 +1,12 @@
namespace TodoApi.Models;
public class TodoItemDTO
{
public int Id { get; set; }
public string? Name { get; set; }
public bool IsComplete { get; set; }
public TodoItemDTO() { }
public TodoItemDTO(Todo todoItem) =>
(Id, Name, IsComplete) = (todoItem.Id, todoItem.Name, todoItem.IsComplete);
}

View File

@ -1,4 +1,6 @@
// <snippet_top>
using Microsoft.EntityFrameworkCore;
using TodoApi.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
@ -8,12 +10,17 @@ var app = builder.Build();
app.MapGet("/todoitems", async (TodoDb db) =>
await db.Todos.Select(x => new TodoItemDTO(x)).ToListAsync());
app.MapGet("/todoitems/{id}", async ([AsParameters] TodoItemRequest request) =>
await request.Db.Todos.FindAsync(request.Id)
// <snippet_id>
app.MapGet("/todoitems/{id}", async (int Id, TodoDb Db) =>
await Db.Todos.FindAsync(Id)
is Todo todo
? Results.Ok(new TodoItemDTO(todo))
: Results.NotFound());
// </snippet_id>
// Remaining code removed for brevity.
// </snippet_top>
// <snippet_post>
app.MapPost("/todoitems", async ([AsParameters] CreateTodoItemRequest request) =>
{
var todoItem = new Todo
@ -27,8 +34,63 @@ app.MapPost("/todoitems", async ([AsParameters] CreateTodoItemRequest request) =
return Results.Created($"/todoitems/{todoItem.Id}", new TodoItemDTO(todoItem));
});
// </snippet_post>
app.MapPut("/todoitems/{id}", async ([AsParameters] EditTodoItemRequest request) =>
// <snippet_put>
app.MapPut("/todoitems/{id}", async (int Id, TodoItemDTO Dto, TodoDb Db) =>
{
var todo = await Db.Todos.FindAsync(Id);
if (todo is null) return Results.NotFound();
todo.Name = Dto.Name;
todo.IsComplete = Dto.IsComplete;
await Db.SaveChangesAsync();
return Results.NoContent();
});
// </snippet_put>
// <snippet_delete>
app.MapDelete("/todoitems/{id}", async (int Id, TodoDb Db) =>
{
if (await Db.Todos.FindAsync(Id) is Todo todo)
{
Db.Todos.Remove(todo);
await Db.SaveChangesAsync();
return Results.Ok(new TodoItemDTO(todo));
}
return Results.NotFound();
});
// </snippet_delete>
// --- [AsParameters] go here ----------------------
// </snippet_ap_id>
app.MapGet("/ap/todoitems/{id}", async ([AsParameters] TodoItemRequest request) =>
await request.Db.Todos.FindAsync(request.Id)
is Todo todo
? Results.Ok(new TodoItemDTO(todo))
: Results.NotFound());
// </snippet_ap_id>
// <snippet_ap_post>
app.MapPost("/ap/todoitems", async ([AsParameters] CreateTodoItemRequest request) =>
{
var todoItem = new Todo
{
IsComplete = request.Dto.IsComplete,
Name = request.Dto.Name
};
request.Db.Todos.Add(todoItem);
await request.Db.SaveChangesAsync();
return Results.Created($"/todoitems/{todoItem.Id}", new TodoItemDTO(todoItem));
});
// <snippet_ap_put>
app.MapPut("/ap/todoitems/{id}", async ([AsParameters] EditTodoItemRequest request) =>
{
var todo = await request.Db.Todos.FindAsync(request.Id);
@ -41,8 +103,10 @@ app.MapPut("/todoitems/{id}", async ([AsParameters] EditTodoItemRequest request)
return Results.NoContent();
});
// </snippet_ap_put>
app.MapDelete("/todoitems/{id}", async ([AsParameters] TodoItemRequest request) =>
// <snippet_ap_delete>
app.MapDelete("/ap/todoitems/{id}", async ([AsParameters] TodoItemRequest request) =>
{
if (await request.Db.Todos.FindAsync(request.Id) is Todo todo)
{
@ -55,37 +119,3 @@ app.MapDelete("/todoitems/{id}", async ([AsParameters] TodoItemRequest request)
});
app.Run();
public class Todo
{
public int Id { get; set; }
public string? Name { get; set; }
public bool IsComplete { get; set; }
public string? Secret { get; set; }
}
public class TodoItemDTO
{
public int Id { get; set; }
public string? Name { get; set; }
public bool IsComplete { get; set; }
public TodoItemDTO() { }
public TodoItemDTO(Todo todoItem) =>
(Id, Name, IsComplete) = (todoItem.Id, todoItem.Name, todoItem.IsComplete);
}
class TodoDb : DbContext
{
public TodoDb(DbContextOptions<TodoDb> options)
: base(options) { }
public DbSet<Todo> Todos => Set<Todo>();
}
record TodoItemRequest(int Id, TodoDb Db);
record CreateTodoItemRequest(TodoItemDTO Dto, TodoDb Db);
record EditTodoItemRequest(int Id, TodoItemDTO Dto, TodoDb Db);