-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intro new mediator endpoint route builder extensions
- Loading branch information
Showing
2 changed files
with
249 additions
and
137 deletions.
There are no files selected for viewing
217 changes: 217 additions & 0 deletions
217
src/Shiny.Mediator.AspNet/EndpointRouteBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace Shiny.Mediator; | ||
|
||
|
||
public static class EndpointRouteBuilderExtensions | ||
{ | ||
// TODO: streaming to signalr | ||
|
||
#region Requests | ||
|
||
public static RouteHandlerBuilder MapMediatorGet<TRequest, TResult>(this IEndpointRouteBuilder builder, string pattern) | ||
where TRequest : IRequest<TResult> | ||
=> builder.MapGet( | ||
pattern, | ||
async ( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TRequest request, | ||
CancellationToken cancellationToken | ||
) => | ||
{ | ||
var result = await mediator | ||
.Request(request, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
return Results.Ok(result); | ||
} | ||
); | ||
|
||
|
||
public static RouteHandlerBuilder MapMediatorPost<TRequest, TResult>(this IEndpointRouteBuilder builder, string pattern) | ||
where TRequest : IRequest<TResult> | ||
=> builder.MapPost( | ||
pattern, | ||
async ( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TRequest request, | ||
CancellationToken cancellationToken | ||
) => | ||
{ | ||
var result = await mediator | ||
.Request(request, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
return Results.Ok(result); | ||
} | ||
); | ||
|
||
|
||
public static RouteHandlerBuilder MapMediatorPut<TRequest, TResult>(this IEndpointRouteBuilder builder, string pattern) | ||
where TRequest : IRequest<TResult> | ||
=> builder.MapPut( | ||
pattern, | ||
async ( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TRequest request, | ||
CancellationToken cancellationToken | ||
) => | ||
{ | ||
var result = await mediator | ||
.Request(request, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
return Results.Ok(result); | ||
} | ||
); | ||
|
||
|
||
public static RouteHandlerBuilder MapMediatorDelete<TRequest, TResult>(this IEndpointRouteBuilder builder, string pattern) | ||
where TRequest : IRequest<TResult> | ||
=> builder.MapDelete( | ||
pattern, | ||
async ( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TRequest request, | ||
CancellationToken cancellationToken | ||
) => | ||
{ | ||
var result = await mediator | ||
.Request(request, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
return Results.Ok(result); | ||
} | ||
); | ||
|
||
#endregion | ||
|
||
#region Commands | ||
|
||
public static RouteHandlerBuilder MapMediatorGet<TCommand>(this IEndpointRouteBuilder builder, string pattern) | ||
where TCommand : ICommand | ||
=> builder.MapGet( | ||
pattern, | ||
async ( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TCommand command, | ||
CancellationToken cancellationToken | ||
) => | ||
{ | ||
await mediator | ||
.Send(command, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
return Results.Ok(); | ||
} | ||
); | ||
|
||
|
||
public static RouteHandlerBuilder MapMediatorDelete<TCommand>(this IEndpointRouteBuilder builder, string pattern) | ||
where TCommand : ICommand | ||
=> builder.MapDelete( | ||
pattern, | ||
async ( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TCommand command, | ||
CancellationToken cancellationToken | ||
) => | ||
{ | ||
await mediator | ||
.Send(command, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
return Results.Ok(); | ||
} | ||
); | ||
|
||
|
||
public static RouteHandlerBuilder MapMediatorPut<TCommand>(this IEndpointRouteBuilder builder, string pattern) | ||
where TCommand : ICommand | ||
=> builder.MapPut( | ||
pattern, | ||
async ( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TCommand command, | ||
CancellationToken cancellationToken | ||
) => | ||
{ | ||
await mediator | ||
.Send(command, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
return Results.Ok(); | ||
} | ||
); | ||
|
||
|
||
public static RouteHandlerBuilder MapMediatorPost<TCommand>(this IEndpointRouteBuilder builder, string pattern) | ||
where TCommand : ICommand | ||
=> builder.MapPost( | ||
pattern, | ||
async ( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TCommand command, | ||
CancellationToken cancellationToken | ||
) => | ||
{ | ||
await mediator | ||
.Send(command, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
return Results.Ok(); | ||
} | ||
); | ||
|
||
#endregion | ||
|
||
#region Streams | ||
|
||
public static RouteHandlerBuilder MapMediatorStreamPost<TRequest, TResult>(this IEndpointRouteBuilder builder, string pattern) | ||
where TRequest : IStreamRequest<TResult> | ||
=> builder.MapPost( | ||
pattern, | ||
( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TRequest request, | ||
CancellationToken cancellationToken | ||
) => mediator.Request(request, cancellationToken) | ||
); | ||
|
||
public static RouteHandlerBuilder MapMediatorStreamGet<TRequest, TResult>(this IEndpointRouteBuilder builder, string pattern) | ||
where TRequest : IStreamRequest<TResult> | ||
=> builder.MapGet( | ||
pattern, | ||
( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TRequest request, | ||
CancellationToken cancellationToken | ||
) => mediator.Request(request, cancellationToken) | ||
); | ||
|
||
public static RouteHandlerBuilder MapMediatorStreamPut<TRequest, TResult>(this IEndpointRouteBuilder builder, string pattern) | ||
where TRequest : IStreamRequest<TResult> | ||
=> builder.MapPut( | ||
pattern, | ||
( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TRequest request, | ||
CancellationToken cancellationToken | ||
) => mediator.Request(request, cancellationToken) | ||
); | ||
|
||
public static RouteHandlerBuilder MapMediatorStreamDelete<TRequest, TResult>(this IEndpointRouteBuilder builder, string pattern) | ||
where TRequest : IStreamRequest<TResult> | ||
=> builder.MapDelete( | ||
pattern, | ||
( | ||
[FromServices] IMediator mediator, | ||
[FromBody] TRequest request, | ||
CancellationToken cancellationToken | ||
) => mediator.Request(request, cancellationToken) | ||
); | ||
#endregion | ||
} |
Oops, something went wrong.