Skip to content
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,7 +7,7 @@
</Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.0.1" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.1.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
Expand Down
43 changes: 43 additions & 0 deletions src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsRouteMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Cnblogs.Architecture.Ddd.Cqrs.Abstractions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Patterns;

Expand Down Expand Up @@ -206,6 +207,20 @@ public static IEndpointConventionBuilder MapCommand(
return app.MapPutCommand(route, handler);
}

/// <summary>
/// Map a command API, using POST method and get command data from request body.
/// </summary>
/// <param name="app"><see cref="ApplicationBuilder"/></param>
/// <param name="route">The route template.</param>
/// <typeparam name="TCommand">The type of command.</typeparam>
/// <returns></returns>
public static IEndpointConventionBuilder MapPostCommand<TCommand>(
this IEndpointRouteBuilder app,
[StringSyntax("Route")] string route)
{
return app.MapPostCommand(route, ([FromBody] TCommand command) => command);
}

/// <summary>
/// Map a command API, using POST method.
/// </summary>
Expand All @@ -222,6 +237,20 @@ public static IEndpointConventionBuilder MapPostCommand(
return app.MapPost(route, handler).AddEndpointFilter<CommandEndpointHandler>();
}

/// <summary>
/// Map a command API, using PUT method and get command data from request body.
/// </summary>
/// <param name="app"><see cref="IEndpointRouteBuilder"/></param>
/// <param name="route">The route template.</param>
/// <typeparam name="TCommand">The type of command.</typeparam>
/// <returns></returns>
public static IEndpointConventionBuilder MapPutCommand<TCommand>(
this IEndpointRouteBuilder app,
[StringSyntax("Route")] string route)
{
return app.MapPutCommand(route, ([FromBody] TCommand command) => command);
}

/// <summary>
/// Map a command API, using PUT method.
/// </summary>
Expand All @@ -238,6 +267,20 @@ public static IEndpointConventionBuilder MapPutCommand(
return app.MapPut(route, handler).AddEndpointFilter<CommandEndpointHandler>();
}

/// <summary>
/// Map a command API, using DELETE method and get command from route/query parameters.
/// </summary>
/// <param name="app"><see cref="IEndpointRouteBuilder"/></param>
/// <param name="route">The route template.</param>
/// <typeparam name="TCommand">The type of command.</typeparam>
/// <returns></returns>
public static IEndpointConventionBuilder MapDeleteCommand<TCommand>(
this IEndpointRouteBuilder app,
[StringSyntax("Route")] string route)
{
return app.MapDeleteCommand(route, ([AsParameters] TCommand command) => command);
}

/// <summary>
/// Map a command API, using DELETE method.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions test/Cnblogs.Architecture.IntegrationTestProject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
(int id, UpdatePayload payload) => new UpdateCommand(id, payload.NeedValidationError, payload.NeedExecutionError));
v1.MapCommand<DeleteCommand>("strings/{id:int}");

// generic command map
v1.MapPostCommand<CreateCommand>("generic-map/strings");
v1.MapPutCommand<UpdateCommand>("generic-map/strings");
v1.MapDeleteCommand<DeleteCommand>("generic-map/strings/{id:int}");

app.Run();

namespace Cnblogs.Architecture.IntegrationTestProject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Cnblogs.Serilog.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.12" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
Expand Down
45 changes: 45 additions & 0 deletions test/Cnblogs.Architecture.IntegrationTests/CqrsRouteMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net.Http.Json;
using Cnblogs.Architecture.Ddd.Infrastructure.Abstractions;
using Cnblogs.Architecture.IntegrationTestProject;
using Cnblogs.Architecture.IntegrationTestProject.Application.Commands;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;

Expand Down Expand Up @@ -132,4 +133,48 @@ public async Task GetItem_MapHeadAndGet_SuccessAsync()
// Assert
responses.Should().Match(x => x.All(y => y.IsSuccessStatusCode));
}

[Fact]
public async Task PostItem_GenericMap_SuccessAsync()
{
// Arrange
var builder = new WebApplicationFactory<Program>();

// Act
var response = await builder.CreateClient().PostAsJsonAsync(
"/api/v1/generic-map/strings",
new CreateCommand(false, "data"));

// Assert
response.Should().BeSuccessful();
}

[Fact]
public async Task PutItem_GenericMap_SuccessAsync()
{
// Arrange
var builder = new WebApplicationFactory<Program>();

// Act
var response = await builder.CreateClient().PutAsJsonAsync(
"/api/v1/generic-map/strings",
new UpdateCommand(1, false, false));

// Assert
response.Should().BeSuccessful();
}

[Fact]
public async Task DeleteCommand_GenericMap_SuccessAsync()
{
// Arrange
var builder = new WebApplicationFactory<Program>();

// Act
var queryBuilder = new QueryStringBuilder().Add("needError", false);
var response = await builder.CreateClient().DeleteAsync("/api/v1/generic-map/strings/1" + queryBuilder.Build());

// Assert
response.Should().BeSuccessful();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.12" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
Expand Down