From e96820a969e143acf9bf76a5d3addd29cbbf51a2 Mon Sep 17 00:00:00 2001 From: mehyaa Date: Tue, 1 Nov 2022 16:12:53 +0300 Subject: [PATCH] HTTP Head support --- .../Builders/DispatcherEndpointsBuilder.cs | 36 +++++++++++++++++++ .../IDispatcherEndpointsBuilder.cs | 10 ++++++ .../src/Convey.WebApi/EndpointsBuilder.cs | 25 +++++++++++++ .../src/Convey.WebApi/IEndpointsBuilder.cs | 9 +++++ 4 files changed, 80 insertions(+) diff --git a/src/Convey.WebApi.CQRS/src/Convey.WebApi.CQRS/Builders/DispatcherEndpointsBuilder.cs b/src/Convey.WebApi.CQRS/src/Convey.WebApi.CQRS/Builders/DispatcherEndpointsBuilder.cs index af9fa250..230cfbcf 100644 --- a/src/Convey.WebApi.CQRS/src/Convey.WebApi.CQRS/Builders/DispatcherEndpointsBuilder.cs +++ b/src/Convey.WebApi.CQRS/src/Convey.WebApi.CQRS/Builders/DispatcherEndpointsBuilder.cs @@ -122,6 +122,42 @@ public IDispatcherEndpointsBuilder Delete(string path, Func context = null, + Action endpoint = null, bool auth = false, string roles = null, + params string[] policies) + { + _builder.Head(path, context, endpoint, auth, roles, policies); + + return this; + } + + public IDispatcherEndpointsBuilder Head(string path, + Func beforeDispatch = null, + Func afterDispatch = null, + Action endpoint = null, bool auth = false, string roles = null, + params string[] policies) where T : class, IQuery + { + _builder.Head(path, async (query, ctx) => + { + if (beforeDispatch is not null) + { + await beforeDispatch(query, ctx); + } + + var dispatcher = ctx.RequestServices.GetRequiredService(); + var result = await dispatcher.QueryAsync(query); + if (afterDispatch is null) + { + ctx.Response.StatusCode = result ? 200 : 404; + return; + } + + await afterDispatch(query, result, ctx); + }, endpoint, auth, roles, policies); + + return this; + } + private static async Task BuildCommandContext(T command, HttpContext context, Func beforeDispatch = null, Func afterDispatch = null) where T : class, ICommand diff --git a/src/Convey.WebApi.CQRS/src/Convey.WebApi.CQRS/IDispatcherEndpointsBuilder.cs b/src/Convey.WebApi.CQRS/src/Convey.WebApi.CQRS/IDispatcherEndpointsBuilder.cs index d552557e..1a107390 100644 --- a/src/Convey.WebApi.CQRS/src/Convey.WebApi.CQRS/IDispatcherEndpointsBuilder.cs +++ b/src/Convey.WebApi.CQRS/src/Convey.WebApi.CQRS/IDispatcherEndpointsBuilder.cs @@ -48,4 +48,14 @@ IDispatcherEndpointsBuilder Delete(string path, Func be bool auth = false, string roles = null, params string[] policies) where T : class, ICommand; + + IDispatcherEndpointsBuilder Head(string path, Func context = null, + Action endpoint = null, bool auth = false, string roles = null, + params string[] policies); + + IDispatcherEndpointsBuilder Head(string path, + Func beforeDispatch = null, + Func afterDispatch = null, + Action endpoint = null, bool auth = false, string roles = null, + params string[] policies) where T : class, IQuery; } \ No newline at end of file diff --git a/src/Convey.WebApi/src/Convey.WebApi/EndpointsBuilder.cs b/src/Convey.WebApi/src/Convey.WebApi/EndpointsBuilder.cs index 543220d9..e44ff71a 100644 --- a/src/Convey.WebApi/src/Convey.WebApi/EndpointsBuilder.cs +++ b/src/Convey.WebApi/src/Convey.WebApi/EndpointsBuilder.cs @@ -134,6 +134,31 @@ public IEndpointsBuilder Delete(string path, Func conte return this; } + public IEndpointsBuilder Head(string path, Func context = null, + Action endpoint = null, bool auth = false, string roles = null, + params string[] policies) + { + var builder = _routeBuilder.MapMethods(path, new[] { "HEAD" }, ctx => context?.Invoke(ctx) ?? Task.CompletedTask); + endpoint?.Invoke(builder); + ApplyAuthRolesAndPolicies(builder, auth, roles, policies); + AddEndpointDefinition(HttpMethods.Head, path); + + return this; + } + + public IEndpointsBuilder Head(string path, Func context = null, + Action endpoint = null, bool auth = false, string roles = null, + params string[] policies) + where T : class + { + var builder = _routeBuilder.MapMethods(path, new[] { "HEAD" }, ctx => BuildQueryContext(ctx, context)); + endpoint?.Invoke(builder); + ApplyAuthRolesAndPolicies(builder, auth, roles, policies); + AddEndpointDefinition(HttpMethods.Head, path); + + return this; + } + private static void ApplyAuthRolesAndPolicies(IEndpointConventionBuilder builder, bool auth, string roles, params string[] policies) { diff --git a/src/Convey.WebApi/src/Convey.WebApi/IEndpointsBuilder.cs b/src/Convey.WebApi/src/Convey.WebApi/IEndpointsBuilder.cs index 728eade9..8bda1dfa 100644 --- a/src/Convey.WebApi/src/Convey.WebApi/IEndpointsBuilder.cs +++ b/src/Convey.WebApi/src/Convey.WebApi/IEndpointsBuilder.cs @@ -47,4 +47,13 @@ IEndpointsBuilder Delete(string path, Func context = nu Action endpoint = null, bool auth = false, string roles = null, params string[] policies) where T : class; + + IEndpointsBuilder Head(string path, Func context = null, + Action endpoint = null, bool auth = false, string roles = null, + params string[] policies); + + IEndpointsBuilder Head(string path, Func context = null, + Action endpoint = null, bool auth = false, string roles = null, + params string[] policies) + where T : class; } \ No newline at end of file