From 34489ac47634f2fb6c6f30cc7f8e17849db3a544 Mon Sep 17 00:00:00 2001 From: Prom3theu5 Date: Wed, 9 Nov 2022 14:05:46 +0000 Subject: [PATCH] Fix registration on groups --- global.json | 4 +-- src/SimCube.Spartan.ExampleConsole/.xml | 30 +++++++++++++++++++ .../DemoRegistrationMethods.cs | 1 + .../GetExampleThreeGroupRequestHandler.cs | 19 ++++++++++++ .../Requests/GetExampleThreeGroupRequest.cs | 28 +++++++++++++++++ .../Extensions/AttributeExtensions.cs | 7 ----- src/SimCube.Spartan/Startup.cs | 9 ++---- 7 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 src/SimCube.Spartan.ExampleConsole/Handlers/GetExampleThreeGroupRequestHandler.cs create mode 100644 src/SimCube.Spartan.ExampleConsole/Requests/GetExampleThreeGroupRequest.cs diff --git a/global.json b/global.json index 36e1a9e..9c9b66d 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,7 @@ { "sdk": { - "version": "7.0.0", + "version": "7.0.100", "rollForward": "latestMajor", "allowPrerelease": false } -} \ No newline at end of file +} diff --git a/src/SimCube.Spartan.ExampleConsole/.xml b/src/SimCube.Spartan.ExampleConsole/.xml index ae38e58..01182d8 100644 --- a/src/SimCube.Spartan.ExampleConsole/.xml +++ b/src/SimCube.Spartan.ExampleConsole/.xml @@ -82,6 +82,14 @@ + + + Th example request handler. + + + + + Th example request handler. @@ -243,6 +251,28 @@ The name. + + + The example get request. + + + + + Initializes a new instance of the class. + + The age. + The name. + + + + Gets the age. + + + + + Gets the name. + + The example get request. diff --git a/src/SimCube.Spartan.ExampleConsole/DemoRegistrationMethods.cs b/src/SimCube.Spartan.ExampleConsole/DemoRegistrationMethods.cs index f8e6294..195d6b7 100644 --- a/src/SimCube.Spartan.ExampleConsole/DemoRegistrationMethods.cs +++ b/src/SimCube.Spartan.ExampleConsole/DemoRegistrationMethods.cs @@ -68,6 +68,7 @@ public static WebApplication AddExampleGroupedMaps(this WebApplication app) // Or manually register the requests on the group if they do not have the attribute. group.MediatedGet("/example-two/{name}/{age}"); + group.MediatedGet, NotFound>>("/example-three/{name}/{age}"); return app; } diff --git a/src/SimCube.Spartan.ExampleConsole/Handlers/GetExampleThreeGroupRequestHandler.cs b/src/SimCube.Spartan.ExampleConsole/Handlers/GetExampleThreeGroupRequestHandler.cs new file mode 100644 index 0000000..65a25e2 --- /dev/null +++ b/src/SimCube.Spartan.ExampleConsole/Handlers/GetExampleThreeGroupRequestHandler.cs @@ -0,0 +1,19 @@ +namespace SimCube.Spartan.ExampleConsole.Handlers; + +/// +/// Th example request handler. +/// +public class GetExampleThreeGroupRequestHandler : IRequestHandler, NotFound>> +{ + /// + public async Task, NotFound>> Handle(GetExampleThreeGroupRequest request, CancellationToken cancellationToken) + { + await Task.Delay(1, cancellationToken); + + return request.Age switch + { + < 18 => TypedResults.NotFound(), + >= 18 => TypedResults.Ok($"The age was {request.Age} and the name was {request.Name}"), + }; + } +} diff --git a/src/SimCube.Spartan.ExampleConsole/Requests/GetExampleThreeGroupRequest.cs b/src/SimCube.Spartan.ExampleConsole/Requests/GetExampleThreeGroupRequest.cs new file mode 100644 index 0000000..26b53d9 --- /dev/null +++ b/src/SimCube.Spartan.ExampleConsole/Requests/GetExampleThreeGroupRequest.cs @@ -0,0 +1,28 @@ +namespace SimCube.Spartan.ExampleConsole.Requests; + +/// +/// The example get request. +/// +public class GetExampleThreeGroupRequest : IMediatedRequest, NotFound>> +{ + /// + /// Initializes a new instance of the class. + /// + /// The age. + /// The name. + public GetExampleThreeGroupRequest(int age, string name) + { + Age = age; + Name = name; + } + + /// + /// Gets the age. + /// + public int Age { get; } + + /// + /// Gets the name. + /// + public string Name { get; } +} diff --git a/src/SimCube.Spartan/Extensions/AttributeExtensions.cs b/src/SimCube.Spartan/Extensions/AttributeExtensions.cs index a3b15bb..61e4f3b 100644 --- a/src/SimCube.Spartan/Extensions/AttributeExtensions.cs +++ b/src/SimCube.Spartan/Extensions/AttributeExtensions.cs @@ -108,13 +108,6 @@ private static (Type? Type, ImplementedType ImplementedType) GetResultType(Type return (null, ImplementedType.Unknown); } - private static Type? GetGenericInterface(Type[] interfacesOnType) => - Array.Find( - interfacesOnType, - x => x.IsGenericType && - (x.GetGenericTypeDefinition() == typeof(IMediatedRequest<>) || - x.GetGenericTypeDefinition() == typeof(IMediatedRequest<>))); - private static string? GetCachedPolicyNameIfHasAttribute(Attribute[] attributes) => attributes?.FirstOrDefault(x => x is CachePolicyAttribute) is not CachePolicyAttribute cachePolicyAttribute ? null diff --git a/src/SimCube.Spartan/Startup.cs b/src/SimCube.Spartan/Startup.cs index 9a42d7f..7aac983 100644 --- a/src/SimCube.Spartan/Startup.cs +++ b/src/SimCube.Spartan/Startup.cs @@ -117,15 +117,10 @@ public static RouteGroupBuilder AddMediatedEndpointsToGroup(this RouteGroupBuild } var endpointsToDefine = groupRequests - .Where(type => type.BaseType == typeof(BaseMediatedRequest) && - type.GetCustomAttributes(typeof(MediatedEndpointAttribute), true).Length > 0) + .Where(type => type.GetCustomAttributes(typeof(MediatedEndpointAttribute), true).Length > 0) .ToArray(); - var requests = endpointsToDefine - .Where(type => type.GetInterfaces().Contains(typeof(IMediatedRequest)) - || (type.IsGenericType && type.GetInterfaces().Contains(typeof(IMediatedStream<>)))).ToArray(); - - foreach (var request in requests) + foreach (var request in endpointsToDefine) { Attribute .GetCustomAttributes(request)