Skip to content

refactor!: use enum to present map nullable route parameter #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
</Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.0.0"/>
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0"/>
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cnblogs.Architecture.Ddd.Cqrs.Abstractions\Cnblogs.Architecture.Ddd.Cqrs.Abstractions.csproj"/>
<ProjectReference Include="..\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.csproj"/>
<ProjectReference Include="..\Cnblogs.Architecture.Ddd.Cqrs.Abstractions\Cnblogs.Architecture.Ddd.Cqrs.Abstractions.csproj" />
<ProjectReference Include="..\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static class CqrsRouteMapper
public static IEndpointConventionBuilder MapQuery<T>(
this IEndpointRouteBuilder app,
[StringSyntax("Route")] string route,
bool mapNullableRouteParameters = false,
MapNullableRouteParameter mapNullableRouteParameters = MapNullableRouteParameter.Disable,
string nullRouteParameterPattern = "-")
{
return app.MapQuery(
Expand Down Expand Up @@ -79,7 +79,7 @@ public static IEndpointConventionBuilder MapQuery(
this IEndpointRouteBuilder app,
[StringSyntax("Route")] string route,
Delegate handler,
bool mapNullableRouteParameters = false,
MapNullableRouteParameter mapNullableRouteParameters = MapNullableRouteParameter.Disable,
string nullRouteParameterPattern = "-")
{
var isQuery = handler.Method.ReturnType.GetInterfaces().Where(x => x.IsGenericType)
Expand All @@ -90,7 +90,7 @@ public static IEndpointConventionBuilder MapQuery(
"delegate does not return a query, please make sure it returns object that implement IQuery<> or IListQuery<> or interface that inherit from them");
}

if (mapNullableRouteParameters == false)
if (mapNullableRouteParameters is MapNullableRouteParameter.Disable)
{
return app.MapGet(route, handler).AddEndpointFilter<QueryEndpointHandler>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Cnblogs.Architecture.Ddd.Cqrs.AspNetCore;

/// <summary>
/// Defines behavior for nullable route parameters.
/// </summary>
public enum MapNullableRouteParameter
{
/// <summary>
/// Map different routes to present <c>null</c> for nullable route parameters.
/// </summary>
Enable = 1,

/// <summary>
/// Do not map extra route for nullable route parameters.
/// </summary>
Disable = 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

var apis = app.NewVersionedApi();
var v1 = apis.MapGroup("/api/v{version:apiVersion}").HasApiVersion(1);
v1.MapQuery<GetStringQuery>("apps/{appId}/strings/{stringId:int}/value", true);
v1.MapQuery<GetStringQuery>("apps/{appId}/strings/{stringId:int}/value", MapNullableRouteParameter.Enable);
v1.MapQuery<GetStringQuery>("strings/{id:int}");
v1.MapQuery<ListStringsQuery>("strings");
v1.MapCommand("strings", (CreatePayload payload) => new CreateCommand(payload.NeedError));
Expand Down