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 @@ -25,6 +25,7 @@

// split the reference string into an array of strings
String[] referenceData = command.Reference?.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
// TODO: Validate the reference data has the correct number of elements

Check warning on line 28 in CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs#L28

Complete the task associated to this 'TODO' comment.
if (referenceData.Length == 0) {
return Result.Failure("Reference cannot be empty.");
}
Expand Down
31 changes: 26 additions & 5 deletions CallbackHandler/Bootstrapper/MiddlewareRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

namespace CallbackHandler.Bootstrapper;

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using Common;
using EventStore.Client;
using Lamar;
Expand All @@ -20,6 +15,13 @@ namespace CallbackHandler.Bootstrapper;
using Shared.General;
using Shared.Middleware;
using Swashbuckle.AspNetCore.Filters;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;

[ExcludeFromCodeCoverage]
public class MiddlewareRegistry :ServiceRegistry
Expand Down Expand Up @@ -85,5 +87,24 @@ public MiddlewareRegistry()
new(middlewareLogLevel, logRequests, logResponses);

this.AddSingleton(config);

this.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.PropertyNamingPolicy = new SnakeCaseNamingPolicy();
options.SerializerOptions.PropertyNameCaseInsensitive = true; // optional, but safer
});
}

public class SnakeCaseNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name)
{
// simple PascalCase to snake_case
return string.Concat(
name.Select((c, i) =>
i > 0 && char.IsUpper(c) ? "_" + char.ToLower(c) : char.ToLower(c).ToString()
)
);
}
}
}
103 changes: 0 additions & 103 deletions CallbackHandler/Controllers/CallbackController.cs

This file was deleted.

31 changes: 31 additions & 0 deletions CallbackHandler/Endpoints/CallbackEndpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using SimpleResults;
using System;

namespace CallbackHandler.Endpoints
{
public static class CallbackEndpoints
{
private const string BaseRoute = "/api/callbacks";

public static IEndpointRouteBuilder MapCallbackEndpoints(this IEndpointRouteBuilder endpoints)
{
RouteGroupBuilder group = endpoints.MapGroup(BaseRoute)
.WithTags("Callbacks");

group.MapPost("/", Handlers.CallbackHandlers.RecordCallback)
.WithName("RecordCallback")
.WithSummary("Records a deposit callback")
.Produces<Result<Guid>>(StatusCodes.Status200OK);

group.MapGet("/{callbackId:guid}", Handlers.CallbackHandlers.GetCallback)
.WithName("GetCallback")
.WithSummary("Gets a callback by ID")
.Produces<Result<DataTransferObjects.CallbackMessage>>(StatusCodes.Status200OK);

return endpoints;
}
}
}
59 changes: 59 additions & 0 deletions CallbackHandler/Handlers/CallbackHandlers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using CallbackHandler.BusinessLogic.Requests;
using CallbackHandler.DataTransferObjects;
using CallbackHandlers.Models;
using MediatR;
using Newtonsoft.Json;
using Shared.Results;
using SimpleResults;

namespace CallbackHandler.Handlers;

public static class CallbackHandlers
{
public static async Task<Result<Guid>> RecordCallback(Deposit depositCallback,
IMediator mediator,
CancellationToken cancellationToken)
{
Guid callbackId = Guid.NewGuid();

CallbackCommands.RecordCallbackCommand request = new(
callbackId,
JsonConvert.SerializeObject(depositCallback),
new[] { "TransactionProcessor" },
MessageFormat.JSON,
depositCallback.GetType().ToString(),
depositCallback.Reference);

Result result = await mediator.Send(request, cancellationToken);

if (result.IsFailed) {
return ResultHelpers.CreateFailure(result);
}

return Result.Success(callbackId);
}

public static async Task<Result<DataTransferObjects.CallbackMessage>> GetCallback(Guid callbackId,
IMediator mediator,
CancellationToken cancellationToken)
{
CallbackQueries.GetCallbackQuery query = new(callbackId);
Result<global::CallbackHandlers.Models.CallbackMessage> getResult = await mediator.Send(query, cancellationToken);

if (getResult.IsFailed) {
return ResultHelpers.CreateFailure(getResult);
}

Result<DataTransferObjects.CallbackMessage> result = Result.Success(new DataTransferObjects.CallbackMessage
{
Reference = getResult.Data.Reference,
TypeString = getResult.Data.TypeString,
Message = getResult.Data.Message
});

return result;
}
}
4 changes: 3 additions & 1 deletion CallbackHandler/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CallbackHandler.Endpoints;

namespace CallbackHandler
{
Expand Down Expand Up @@ -112,7 +113,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapCallbackEndpoints();

endpoints.MapHealthChecks("health",
new HealthCheckOptions
{
Expand Down
Loading