Skip to content

Commit

Permalink
implement a DTO - Data Transfer Object; it makes sense to me to call …
Browse files Browse the repository at this point in the history
…it the user-facing version of the object. Purpose: prevent over-posting, hide properties that clients shouldn't see, omit properties to reduce size, flatten object graphs (can be more convenient for clients)
  • Loading branch information
pb413Lw committed Sep 16, 2022
1 parent ed2a549 commit 0dec25e
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions TodoApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,42 @@
app.MapGet("/", () => "Hello World!");

app.MapGet("/todoitems", async (TodoDb db) =>
await db.Todos.ToListAsync());
await db.Todos.Select(x => new TodoItemDTO(x)).ToListAsync());

app.MapGet("/todoitems/complete", async (TodoDb db) =>
await db.Todos.Where(t => t.IsComplete).ToListAsync());
// These lines not included in step Prevent over-posting
// TODO Can I make them right?

//app.MapGet("/todoitems/complete", async (TodoDb db) =>
// await db.Todos.Where(t => t.IsComplete).ToListAsync());

app.MapGet("/todoitems/{id}", async (int id, TodoDb db) =>
await db.Todos.FindAsync(id)
is Todo todo
? Results.Ok(todo)
? Results.Ok(new TodoItemDTO(todo))
: Results.NotFound());

app.MapPost("/todoitems", async (Todo todo, TodoDb db) =>
app.MapPost("/todoitems", async (TodoItemDTO todoItemDTO, TodoDb db) =>
{
db.Todos.Add(todo);
var todoItem = new Todo
{
IsComplete = todoItemDTO.IsComplete,
Name = todoItemDTO.Name
};

db.Todos.Add(todoItem);
await db.SaveChangesAsync();

return Results.Created($"/todoitems/{todo.Id}", todo);
return Results.Created($"/todoitems/{todoItem.Id}", new TodoItemDTO(todoItem));
});

app.MapPut("/todoitems/{id}", async (int id, Todo inputTodo, TodoDb db) =>
app.MapPut("/todoitems/{id}", async (int id, TodoItemDTO todoItemDTO, TodoDb db) =>
{
var todo = await db.Todos.FindAsync(id);

if (todo is null) return Results.NotFound();

todo.Name = inputTodo.Name;
todo.IsComplete = inputTodo.IsComplete;
todo.Name = todoItemDTO.Name;
todo.IsComplete = todoItemDTO.IsComplete;

await db.SaveChangesAsync();

Expand All @@ -47,22 +56,33 @@ is Todo todo
{
db.Todos.Remove(todo);
await db.SaveChangesAsync();
return Results.Ok(todo);
return Results.Ok(new TodoItemDTO(todo));
}

return Results.NotFound();
});

app.Run();

class Todo
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)
Expand Down

0 comments on commit 0dec25e

Please sign in to comment.