Skip to content

Commit 293a8b8

Browse files
Merge pull request #198 from TransactionProcessing/task/#194_move_to_minimal_api
moved controller to miminal api pattern
2 parents 60eec58 + ef38b10 commit 293a8b8

6 files changed

Lines changed: 120 additions & 109 deletions

File tree

CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public async Task<Result> RecordCallback(CallbackCommands.RecordCallbackCommand
2525

2626
// split the reference string into an array of strings
2727
String[] referenceData = command.Reference?.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
28+
// TODO: Validate the reference data has the correct number of elements
2829
if (referenceData.Length == 0) {
2930
return Result.Failure("Reference cannot be empty.");
3031
}

CallbackHandler/Bootstrapper/MiddlewareRegistry.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
namespace CallbackHandler.Bootstrapper;
44

5-
using System;
6-
using System.Collections.Generic;
7-
using System.Diagnostics.CodeAnalysis;
8-
using System.IO;
9-
using System.Reflection;
105
using Common;
116
using EventStore.Client;
127
using Lamar;
@@ -20,6 +15,13 @@ namespace CallbackHandler.Bootstrapper;
2015
using Shared.General;
2116
using Shared.Middleware;
2217
using Swashbuckle.AspNetCore.Filters;
18+
using System;
19+
using System.Collections.Generic;
20+
using System.Diagnostics.CodeAnalysis;
21+
using System.IO;
22+
using System.Linq;
23+
using System.Reflection;
24+
using System.Text.Json;
2325

2426
[ExcludeFromCodeCoverage]
2527
public class MiddlewareRegistry :ServiceRegistry
@@ -85,5 +87,24 @@ public MiddlewareRegistry()
8587
new(middlewareLogLevel, logRequests, logResponses);
8688

8789
this.AddSingleton(config);
90+
91+
this.ConfigureHttpJsonOptions(options =>
92+
{
93+
options.SerializerOptions.PropertyNamingPolicy = new SnakeCaseNamingPolicy();
94+
options.SerializerOptions.PropertyNameCaseInsensitive = true; // optional, but safer
95+
});
96+
}
97+
98+
public class SnakeCaseNamingPolicy : JsonNamingPolicy
99+
{
100+
public override string ConvertName(string name)
101+
{
102+
// simple PascalCase to snake_case
103+
return string.Concat(
104+
name.Select((c, i) =>
105+
i > 0 && char.IsUpper(c) ? "_" + char.ToLower(c) : char.ToLower(c).ToString()
106+
)
107+
);
88108
}
109+
}
89110
}

CallbackHandler/Controllers/CallbackController.cs

Lines changed: 0 additions & 103 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Routing;
4+
using SimpleResults;
5+
using System;
6+
7+
namespace CallbackHandler.Endpoints
8+
{
9+
public static class CallbackEndpoints
10+
{
11+
private const string BaseRoute = "/api/callbacks";
12+
13+
public static IEndpointRouteBuilder MapCallbackEndpoints(this IEndpointRouteBuilder endpoints)
14+
{
15+
RouteGroupBuilder group = endpoints.MapGroup(BaseRoute)
16+
.WithTags("Callbacks");
17+
18+
group.MapPost("/", Handlers.CallbackHandlers.RecordCallback)
19+
.WithName("RecordCallback")
20+
.WithSummary("Records a deposit callback")
21+
.Produces<Result<Guid>>(StatusCodes.Status200OK);
22+
23+
group.MapGet("/{callbackId:guid}", Handlers.CallbackHandlers.GetCallback)
24+
.WithName("GetCallback")
25+
.WithSummary("Gets a callback by ID")
26+
.Produces<Result<DataTransferObjects.CallbackMessage>>(StatusCodes.Status200OK);
27+
28+
return endpoints;
29+
}
30+
}
31+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using CallbackHandler.BusinessLogic.Requests;
5+
using CallbackHandler.DataTransferObjects;
6+
using CallbackHandlers.Models;
7+
using MediatR;
8+
using Newtonsoft.Json;
9+
using Shared.Results;
10+
using SimpleResults;
11+
12+
namespace CallbackHandler.Handlers;
13+
14+
public static class CallbackHandlers
15+
{
16+
public static async Task<Result<Guid>> RecordCallback(Deposit depositCallback,
17+
IMediator mediator,
18+
CancellationToken cancellationToken)
19+
{
20+
Guid callbackId = Guid.NewGuid();
21+
22+
CallbackCommands.RecordCallbackCommand request = new(
23+
callbackId,
24+
JsonConvert.SerializeObject(depositCallback),
25+
new[] { "TransactionProcessor" },
26+
MessageFormat.JSON,
27+
depositCallback.GetType().ToString(),
28+
depositCallback.Reference);
29+
30+
Result result = await mediator.Send(request, cancellationToken);
31+
32+
if (result.IsFailed) {
33+
return ResultHelpers.CreateFailure(result);
34+
}
35+
36+
return Result.Success(callbackId);
37+
}
38+
39+
public static async Task<Result<DataTransferObjects.CallbackMessage>> GetCallback(Guid callbackId,
40+
IMediator mediator,
41+
CancellationToken cancellationToken)
42+
{
43+
CallbackQueries.GetCallbackQuery query = new(callbackId);
44+
Result<global::CallbackHandlers.Models.CallbackMessage> getResult = await mediator.Send(query, cancellationToken);
45+
46+
if (getResult.IsFailed) {
47+
return ResultHelpers.CreateFailure(getResult);
48+
}
49+
50+
Result<DataTransferObjects.CallbackMessage> result = Result.Success(new DataTransferObjects.CallbackMessage
51+
{
52+
Reference = getResult.Data.Reference,
53+
TypeString = getResult.Data.TypeString,
54+
Message = getResult.Data.Message
55+
});
56+
57+
return result;
58+
}
59+
}

CallbackHandler/Startup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Generic;
88
using System.Linq;
99
using System.Threading.Tasks;
10+
using CallbackHandler.Endpoints;
1011

1112
namespace CallbackHandler
1213
{
@@ -112,7 +113,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
112113

113114
app.UseEndpoints(endpoints =>
114115
{
115-
endpoints.MapControllers();
116+
endpoints.MapCallbackEndpoints();
117+
116118
endpoints.MapHealthChecks("health",
117119
new HealthCheckOptions
118120
{

0 commit comments

Comments
 (0)