Skip to content

Commit

Permalink
Intro new mediator endpoint route builder extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jan 19, 2025
1 parent 84b7a05 commit 9009f37
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 137 deletions.
217 changes: 217 additions & 0 deletions src/Shiny.Mediator.AspNet/EndpointRouteBuilderExtensions.cs
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
}
Loading

0 comments on commit 9009f37

Please sign in to comment.