Skip to content

Commit

Permalink
Fix registration on groups
Browse files Browse the repository at this point in the history
  • Loading branch information
prom3theu5 committed Nov 9, 2022
1 parent 2a84f9e commit 34489ac
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 16 deletions.
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sdk": {
"version": "7.0.0",
"version": "7.0.100",
"rollForward": "latestMajor",
"allowPrerelease": false
}
}
}
30 changes: 30 additions & 0 deletions src/SimCube.Spartan.ExampleConsole/.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@
<member name="M:SimCube.Spartan.ExampleConsole.Handlers.GetExampleRequestHandler.Handle(SimCube.Spartan.ExampleConsole.Requests.GetExampleRequest,System.Threading.CancellationToken)">
<inheritdoc />
</member>
<member name="T:SimCube.Spartan.ExampleConsole.Handlers.GetExampleThreeGroupRequestHandler">
<summary>
Th example request handler.
</summary>
</member>
<member name="M:SimCube.Spartan.ExampleConsole.Handlers.GetExampleThreeGroupRequestHandler.Handle(SimCube.Spartan.ExampleConsole.Requests.GetExampleThreeGroupRequest,System.Threading.CancellationToken)">
<inheritdoc />
</member>
<member name="T:SimCube.Spartan.ExampleConsole.Handlers.GetExampleTwoGroupRequestHandler">
<summary>
Th example request handler.
Expand Down Expand Up @@ -243,6 +251,28 @@
<member name="P:SimCube.Spartan.ExampleConsole.Requests.GetExampleRequest.Name">
<summary>The name.</summary>
</member>
<member name="T:SimCube.Spartan.ExampleConsole.Requests.GetExampleThreeGroupRequest">
<summary>
The example get request.
</summary>
</member>
<member name="M:SimCube.Spartan.ExampleConsole.Requests.GetExampleThreeGroupRequest.#ctor(System.Int32,System.String)">
<summary>
Initializes a new instance of the <see cref="T:SimCube.Spartan.ExampleConsole.Requests.GetExampleThreeGroupRequest"/> class.
</summary>
<param name="age">The age.</param>
<param name="name">The name.</param>
</member>
<member name="P:SimCube.Spartan.ExampleConsole.Requests.GetExampleThreeGroupRequest.Age">
<summary>
Gets the age.
</summary>
</member>
<member name="P:SimCube.Spartan.ExampleConsole.Requests.GetExampleThreeGroupRequest.Name">
<summary>
Gets the name.
</summary>
</member>
<member name="T:SimCube.Spartan.ExampleConsole.Requests.GetExampleTwoGroupRequest">
<summary>
The example get request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetExampleTwoGroupRequest>("/example-two/{name}/{age}");
group.MediatedGet<GetExampleThreeGroupRequest, Results<Ok<string>, NotFound>>("/example-three/{name}/{age}");

return app;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace SimCube.Spartan.ExampleConsole.Handlers;

/// <summary>
/// Th example request handler.
/// </summary>
public class GetExampleThreeGroupRequestHandler : IRequestHandler<GetExampleThreeGroupRequest, Results<Ok<string>, NotFound>>
{
/// <inheritdoc />
public async Task<Results<Ok<string>, 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}"),
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace SimCube.Spartan.ExampleConsole.Requests;

/// <summary>
/// The example get request.
/// </summary>
public class GetExampleThreeGroupRequest : IMediatedRequest<Results<Ok<string>, NotFound>>
{
/// <summary>
/// Initializes a new instance of the <see cref="GetExampleThreeGroupRequest"/> class.
/// </summary>
/// <param name="age">The age.</param>
/// <param name="name">The name.</param>
public GetExampleThreeGroupRequest(int age, string name)
{
Age = age;
Name = name;
}

/// <summary>
/// Gets the age.
/// </summary>
public int Age { get; }

/// <summary>
/// Gets the name.
/// </summary>
public string Name { get; }
}
7 changes: 0 additions & 7 deletions src/SimCube.Spartan/Extensions/AttributeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions src/SimCube.Spartan/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 34489ac

Please sign in to comment.