Skip to content

Commit 6381288

Browse files
authored
Merge pull request #156 from cnblogs/support-generic-map-methods
feat: support generic map methods
2 parents 528aac9 + 1ba9334 commit 6381288

File tree

8 files changed

+98
-5
lines changed

8 files changed

+98
-5
lines changed

src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</Description>
88
</PropertyGroup>
99
<ItemGroup>
10-
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.0.1" />
10+
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.1.0" />
1111
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
1212
</ItemGroup>
1313
<ItemGroup>

src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsRouteMapper.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Cnblogs.Architecture.Ddd.Cqrs.Abstractions;
55
using Microsoft.AspNetCore.Builder;
66
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Mvc;
78
using Microsoft.AspNetCore.Routing;
89
using Microsoft.AspNetCore.Routing.Patterns;
910

@@ -206,6 +207,20 @@ public static IEndpointConventionBuilder MapCommand(
206207
return app.MapPutCommand(route, handler);
207208
}
208209

210+
/// <summary>
211+
/// Map a command API, using POST method and get command data from request body.
212+
/// </summary>
213+
/// <param name="app"><see cref="ApplicationBuilder"/></param>
214+
/// <param name="route">The route template.</param>
215+
/// <typeparam name="TCommand">The type of command.</typeparam>
216+
/// <returns></returns>
217+
public static IEndpointConventionBuilder MapPostCommand<TCommand>(
218+
this IEndpointRouteBuilder app,
219+
[StringSyntax("Route")] string route)
220+
{
221+
return app.MapPostCommand(route, ([FromBody] TCommand command) => command);
222+
}
223+
209224
/// <summary>
210225
/// Map a command API, using POST method.
211226
/// </summary>
@@ -222,6 +237,20 @@ public static IEndpointConventionBuilder MapPostCommand(
222237
return app.MapPost(route, handler).AddEndpointFilter<CommandEndpointHandler>();
223238
}
224239

240+
/// <summary>
241+
/// Map a command API, using PUT method and get command data from request body.
242+
/// </summary>
243+
/// <param name="app"><see cref="IEndpointRouteBuilder"/></param>
244+
/// <param name="route">The route template.</param>
245+
/// <typeparam name="TCommand">The type of command.</typeparam>
246+
/// <returns></returns>
247+
public static IEndpointConventionBuilder MapPutCommand<TCommand>(
248+
this IEndpointRouteBuilder app,
249+
[StringSyntax("Route")] string route)
250+
{
251+
return app.MapPutCommand(route, ([FromBody] TCommand command) => command);
252+
}
253+
225254
/// <summary>
226255
/// Map a command API, using PUT method.
227256
/// </summary>
@@ -238,6 +267,20 @@ public static IEndpointConventionBuilder MapPutCommand(
238267
return app.MapPut(route, handler).AddEndpointFilter<CommandEndpointHandler>();
239268
}
240269

270+
/// <summary>
271+
/// Map a command API, using DELETE method and get command from route/query parameters.
272+
/// </summary>
273+
/// <param name="app"><see cref="IEndpointRouteBuilder"/></param>
274+
/// <param name="route">The route template.</param>
275+
/// <typeparam name="TCommand">The type of command.</typeparam>
276+
/// <returns></returns>
277+
public static IEndpointConventionBuilder MapDeleteCommand<TCommand>(
278+
this IEndpointRouteBuilder app,
279+
[StringSyntax("Route")] string route)
280+
{
281+
return app.MapDeleteCommand(route, ([AsParameters] TCommand command) => command);
282+
}
283+
241284
/// <summary>
242285
/// Map a command API, using DELETE method.
243286
/// </summary>

src/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

src/Cnblogs.Architecture.Ddd.Infrastructure.MongoDb/Cnblogs.Architecture.Ddd.Infrastructure.MongoDb.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
13+
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

test/Cnblogs.Architecture.IntegrationTestProject/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
(int id, UpdatePayload payload) => new UpdateCommand(id, payload.NeedValidationError, payload.NeedExecutionError));
4646
v1.MapCommand<DeleteCommand>("strings/{id:int}");
4747

48+
// generic command map
49+
v1.MapPostCommand<CreateCommand>("generic-map/strings");
50+
v1.MapPutCommand<UpdateCommand>("generic-map/strings");
51+
v1.MapDeleteCommand<DeleteCommand>("generic-map/strings/{id:int}");
52+
4853
app.Run();
4954

5055
namespace Cnblogs.Architecture.IntegrationTestProject

test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
33
<PackageReference Include="Cnblogs.Serilog.Extensions" Version="1.1.0" />
4-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.11" />
4+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.12" />
55
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
66
<PackageReference Include="xunit" Version="2.5.1" />
77
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">

test/Cnblogs.Architecture.IntegrationTests/CqrsRouteMapperTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Net.Http.Json;
33
using Cnblogs.Architecture.Ddd.Infrastructure.Abstractions;
44
using Cnblogs.Architecture.IntegrationTestProject;
5+
using Cnblogs.Architecture.IntegrationTestProject.Application.Commands;
56
using FluentAssertions;
67
using Microsoft.AspNetCore.Mvc.Testing;
78

@@ -132,4 +133,48 @@ public async Task GetItem_MapHeadAndGet_SuccessAsync()
132133
// Assert
133134
responses.Should().Match(x => x.All(y => y.IsSuccessStatusCode));
134135
}
136+
137+
[Fact]
138+
public async Task PostItem_GenericMap_SuccessAsync()
139+
{
140+
// Arrange
141+
var builder = new WebApplicationFactory<Program>();
142+
143+
// Act
144+
var response = await builder.CreateClient().PostAsJsonAsync(
145+
"/api/v1/generic-map/strings",
146+
new CreateCommand(false, "data"));
147+
148+
// Assert
149+
response.Should().BeSuccessful();
150+
}
151+
152+
[Fact]
153+
public async Task PutItem_GenericMap_SuccessAsync()
154+
{
155+
// Arrange
156+
var builder = new WebApplicationFactory<Program>();
157+
158+
// Act
159+
var response = await builder.CreateClient().PutAsJsonAsync(
160+
"/api/v1/generic-map/strings",
161+
new UpdateCommand(1, false, false));
162+
163+
// Assert
164+
response.Should().BeSuccessful();
165+
}
166+
167+
[Fact]
168+
public async Task DeleteCommand_GenericMap_SuccessAsync()
169+
{
170+
// Arrange
171+
var builder = new WebApplicationFactory<Program>();
172+
173+
// Act
174+
var queryBuilder = new QueryStringBuilder().Add("needError", false);
175+
var response = await builder.CreateClient().DeleteAsync("/api/v1/generic-map/strings/1" + queryBuilder.Build());
176+
177+
// Assert
178+
response.Should().BeSuccessful();
179+
}
135180
}

test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<ItemGroup>
4-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.11" />
4+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.12" />
55
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
66
<PackageReference Include="xunit" Version="2.5.1" />
77
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">

0 commit comments

Comments
 (0)