-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example of MapCommand for extreme endpoints handling
- Loading branch information
1 parent
5216a64
commit a1cea48
Showing
9 changed files
with
100 additions
and
33 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...le/Warehouse/Warehouse.Api.Tests/Products/GettingProductDetails/GetProductDetailsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
Sample/Warehouse/Warehouse.Api.Tests/Products/GettingProducts/GetProductsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
Sample/Warehouse/Warehouse.Api.Tests/Products/RegisteringProduct/RegisterProductRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Warehouse.Api.Tests.Products.RegisteringProduct | ||
{ | ||
public record RegisterProductRequest( | ||
string? SKU, | ||
string? Name, | ||
string? Description | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Sample/Warehouse/Warehouse/Core/Extensions/EndpointsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Net; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http; | ||
using Warehouse.Core.Commands; | ||
|
||
namespace Warehouse.Core.Extensions | ||
{ | ||
internal static class EndpointsExtensions | ||
{ | ||
internal static IEndpointRouteBuilder MapCommand<TRequest>( | ||
this IEndpointRouteBuilder endpoints, | ||
HttpMethod httpMethod, | ||
string url, | ||
HttpStatusCode statusCode = HttpStatusCode.OK) | ||
{ | ||
endpoints.MapMethods(url, new []{httpMethod.ToString()} , async context => | ||
{ | ||
var command = await context.FromBody<TRequest>(); | ||
|
||
var commandResult = await context.SendCommand(command); | ||
|
||
if (commandResult == CommandResult.None) | ||
{ | ||
context.Response.StatusCode = (int)statusCode; | ||
return; | ||
} | ||
|
||
await context.ReturnJSON(commandResult.Result, statusCode); | ||
}); | ||
|
||
return endpoints; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters