Skip to content

Commit

Permalink
♻️ Cleanup endpoints & OpenAPI mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Hona committed Mar 14, 2024
1 parent 3d1147f commit 57647e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,15 @@ public static RouteHandlerBuilder MapGetWithOpenApi<T>(
public static RouteHandlerBuilder ProducesPost(this RouteHandlerBuilder builder) =>
builder
.ProducesValidationProblem()
.ProducesProblem(StatusCodes.Status500InternalServerError);

public static RouteHandlerBuilder MapPostWithCreatedOpenApi(
this IEndpointRouteBuilder endpoints,
string pattern,
Delegate handler) =>
endpoints.MapPost(pattern, handler)
.ProducesPost()
.ProducesProblem(StatusCodes.Status500InternalServerError)
.Produces(StatusCodes.Status201Created);

public static RouteHandlerBuilder MapPostWithUpdatedOpenApi(
public static RouteHandlerBuilder MapPostWithOpenApi(
this IEndpointRouteBuilder endpoints,
string pattern,
Delegate handler) =>
endpoints.MapPost(pattern, handler)
.ProducesPost()
.Produces(StatusCodes.Status404NotFound)
.Produces(StatusCodes.Status204NoContent);
.ProducesPost();

/// <summary>
/// Used for PUT endpoints that updates a single item.
Expand Down Expand Up @@ -75,5 +66,5 @@ public static RouteHandlerBuilder MapDeleteWithOpenApi(
string pattern,
Delegate handler) =>
endpoints.MapDelete(pattern, handler)
.Produces(StatusCodes.Status204NoContent);
.ProducesDelete();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,42 @@ public static void MapEndpoints(IEndpointRouteBuilder endpoints)
var group = endpoints.MapGroup("/todos")
.WithTags(nameof(Todo));

group.MapPostWithCreatedOpenApi(string.Empty,
group.MapPostWithOpenApi(string.Empty,
async (CreateTodoCommand command, ISender sender, CancellationToken cancellationToken) =>
{
var id = await sender.Send(command, cancellationToken);
return Results.Created($"/todos/{id}", id);
});

group.MapPostWithUpdatedOpenApi("/{id:guid}",
async (Guid id, UpdateTodoCommand command, ISender sender, CancellationToken cancellationToken)
=>
{
await sender.Send(command with { Id = id }, cancellationToken);
return Results.NoContent();
})

group.MapPutWithOpenApi("/{id:guid}",
async (Guid id, UpdateTodoCommand command, ISender sender, CancellationToken cancellationToken) =>
{
await sender.Send(command with { Id = id }, cancellationToken);
return Results.NoContent();
})
.WithTags(nameof(Todo));

group.MapPostWithUpdatedOpenApi("/{id:guid}/complete",
async (Guid id, ISender sender, CancellationToken cancellationToken)
=>

group.MapPutWithOpenApi("/{id:guid}/complete",
async (Guid id, ISender sender, CancellationToken cancellationToken) =>
{
await sender.Send(new CompleteTodoCommand(id), cancellationToken);
return Results.NoContent();
})
.WithTags(nameof(Todo));

group.MapDeleteWithOpenApi("/{id:guid}",
async (Guid id, ISender sender, CancellationToken cancellationToken)
=>

group.MapDeleteWithOpenApi("/{id:guid}",
async (Guid id, ISender sender, CancellationToken cancellationToken) =>
{
await sender.Send(new DeleteTodoCommand(id), cancellationToken);
return Results.NoContent();
});
group.MapGetWithOpenApi<Todo>("/{id:guid}",
(Guid id, ISender sender, CancellationToken cancellationToken)

group.MapGetWithOpenApi<Todo>("/{id:guid}",
(Guid id, ISender sender, CancellationToken cancellationToken)
=> sender.Send(new GetTodoQuery(id), cancellationToken));

group.MapGetWithOpenApi<IImmutableList<Todo>>(string.Empty,
(bool? isCompleted, ISender sender, CancellationToken cancellationToken)
(bool? isCompleted, ISender sender, CancellationToken cancellationToken)
=> sender.Send(new GetAllTodosQuery(isCompleted), cancellationToken));
}
}

0 comments on commit 57647e0

Please sign in to comment.