From bb75c6ae2ab96dd9769e6abf3ace2ccb906dbad1 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Sun, 2 Nov 2025 15:47:20 +0000 Subject: [PATCH 1/3] move to minimal api --- ...CallbackHandler.BusinessLogic.Tests.csproj | 2 +- .../CallbackHandler.BusinessLogic.csproj | 6 +-- .../Services/CallbackDomainService.cs | 30 +---------- ...andler.CallbackMessage.DomainEvents.csproj | 4 +- ...dler.CallbackMessageAggregate.Tests.csproj | 2 +- ...ackHandler.CallbackMessageAggregate.csproj | 6 +-- .../CallbackMessage.cs | 8 +++ .../CallbackHandler.IntegrationTests.csproj | 4 +- CallbackHandler/CallbackHandler.csproj | 3 +- .../Endpoints/CallbackEndpoints.cs | 9 ++-- CallbackHandler/Handlers/CallbackHandlers.cs | 51 +++++++++---------- 11 files changed, 55 insertions(+), 70 deletions(-) diff --git a/CallbackHandler.BusinessLogic.Tests/CallbackHandler.BusinessLogic.Tests.csproj b/CallbackHandler.BusinessLogic.Tests/CallbackHandler.BusinessLogic.Tests.csproj index 1bb6e13..a52859e 100644 --- a/CallbackHandler.BusinessLogic.Tests/CallbackHandler.BusinessLogic.Tests.csproj +++ b/CallbackHandler.BusinessLogic.Tests/CallbackHandler.BusinessLogic.Tests.csproj @@ -9,7 +9,7 @@ - + diff --git a/CallbackHandler.BusinessLogic/CallbackHandler.BusinessLogic.csproj b/CallbackHandler.BusinessLogic/CallbackHandler.BusinessLogic.csproj index e95100d..ca3a661 100644 --- a/CallbackHandler.BusinessLogic/CallbackHandler.BusinessLogic.csproj +++ b/CallbackHandler.BusinessLogic/CallbackHandler.BusinessLogic.csproj @@ -5,9 +5,9 @@ - - - + + + diff --git a/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs b/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs index 333b0b1..b4084b7 100644 --- a/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs +++ b/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs @@ -1,10 +1,9 @@ -using Shared.Results; +using Shared.EventStore.Helpers; using SimpleResults; namespace CallbackHandler.BusinessLogic.Services; using CallbackHandler.BusinessLogic.Requests; -using CallbackHandlers.Models; using CallbackMessageAggregate; using Shared.DomainDrivenDesign.EventSourcing; using Shared.EventStore.Aggregate; @@ -27,7 +26,7 @@ public async Task RecordCallback(CallbackCommands.RecordCallbackCommand String[] referenceData = command.Reference?.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty(); // TODO: Validate the reference data has the correct number of elements if (referenceData.Length == 0) { - return Result.Failure("Reference cannot be empty."); + return Result.Invalid("Reference cannot be empty."); } // Element 0 is estate reference, Element 1 is merchant reference @@ -47,29 +46,4 @@ public async Task RecordCallback(CallbackCommands.RecordCallbackCommand return stateResult; return await this.AggregateRepository.SaveChanges(aggregate, cancellationToken); } -} - -public static class DomainServiceHelper -{ - public static Result HandleGetAggregateResult(Result result, Guid aggregateId, bool isNotFoundError = true) - where T : Aggregate, new() // Constraint: T is a subclass of Aggregate and has a parameterless constructor - { - if (result.IsFailed && result.Status != ResultStatus.NotFound) - { - return ResultHelpers.CreateFailure(result); - } - - if (result.Status == ResultStatus.NotFound && isNotFoundError) - { - return ResultHelpers.CreateFailure(result); - } - - T aggregate = result.Status switch - { - ResultStatus.NotFound => new T { AggregateId = aggregateId }, // Set AggregateId when creating a new instance - _ => result.Data - }; - - return Result.Success(aggregate); - } } \ No newline at end of file diff --git a/CallbackHandler.CallbackMessage.DomainEvents/CallbackHandler.CallbackMessage.DomainEvents.csproj b/CallbackHandler.CallbackMessage.DomainEvents/CallbackHandler.CallbackMessage.DomainEvents.csproj index 3f9999b..0054bbd 100644 --- a/CallbackHandler.CallbackMessage.DomainEvents/CallbackHandler.CallbackMessage.DomainEvents.csproj +++ b/CallbackHandler.CallbackMessage.DomainEvents/CallbackHandler.CallbackMessage.DomainEvents.csproj @@ -5,7 +5,7 @@ None - - + + \ No newline at end of file diff --git a/CallbackHandler.CallbackMessageAggregate.Tests/CallbackHandler.CallbackMessageAggregate.Tests.csproj b/CallbackHandler.CallbackMessageAggregate.Tests/CallbackHandler.CallbackMessageAggregate.Tests.csproj index 1da0705..614ff9e 100644 --- a/CallbackHandler.CallbackMessageAggregate.Tests/CallbackHandler.CallbackMessageAggregate.Tests.csproj +++ b/CallbackHandler.CallbackMessageAggregate.Tests/CallbackHandler.CallbackMessageAggregate.Tests.csproj @@ -8,7 +8,7 @@ - + diff --git a/CallbackHandler.CallbackMessageAggregate/CallbackHandler.CallbackMessageAggregate.csproj b/CallbackHandler.CallbackMessageAggregate/CallbackHandler.CallbackMessageAggregate.csproj index 1b5477e..22bd279 100644 --- a/CallbackHandler.CallbackMessageAggregate/CallbackHandler.CallbackMessageAggregate.csproj +++ b/CallbackHandler.CallbackMessageAggregate/CallbackHandler.CallbackMessageAggregate.csproj @@ -10,8 +10,8 @@ - - - + + + \ No newline at end of file diff --git a/CallbackHandler.DataTransferObjects/CallbackMessage.cs b/CallbackHandler.DataTransferObjects/CallbackMessage.cs index caa5714..0fd56c5 100644 --- a/CallbackHandler.DataTransferObjects/CallbackMessage.cs +++ b/CallbackHandler.DataTransferObjects/CallbackMessage.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Newtonsoft.Json; namespace CallbackHandler.DataTransferObjects { @@ -16,4 +17,11 @@ public class CallbackMessage public String Reference { get; set; } } + + public class CallbackResponse { + [JsonProperty("callback_id")] + public Guid CallbackId { get; set; } + } + + } diff --git a/CallbackHandler.IntegrationTests/CallbackHandler.IntegrationTests.csproj b/CallbackHandler.IntegrationTests/CallbackHandler.IntegrationTests.csproj index 7e7ee90..209fb16 100644 --- a/CallbackHandler.IntegrationTests/CallbackHandler.IntegrationTests.csproj +++ b/CallbackHandler.IntegrationTests/CallbackHandler.IntegrationTests.csproj @@ -15,8 +15,8 @@ - - + + diff --git a/CallbackHandler/CallbackHandler.csproj b/CallbackHandler/CallbackHandler.csproj index 8bf24ea..0cb5b23 100644 --- a/CallbackHandler/CallbackHandler.csproj +++ b/CallbackHandler/CallbackHandler.csproj @@ -12,7 +12,8 @@ - + + diff --git a/CallbackHandler/Endpoints/CallbackEndpoints.cs b/CallbackHandler/Endpoints/CallbackEndpoints.cs index 16b979a..619c0a0 100644 --- a/CallbackHandler/Endpoints/CallbackEndpoints.cs +++ b/CallbackHandler/Endpoints/CallbackEndpoints.cs @@ -1,8 +1,11 @@ -using Microsoft.AspNetCore.Builder; +using CallbackHandler.DataTransferObjects; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using SimpleResults; using System; +using Shared.Extensions; +using Shared.Middleware; namespace CallbackHandler.Endpoints { @@ -18,12 +21,12 @@ public static IEndpointRouteBuilder MapCallbackEndpoints(this IEndpointRouteBuil group.MapPost("/", Handlers.CallbackHandlers.RecordCallback) .WithName("RecordCallback") .WithSummary("Records a deposit callback") - .Produces>(StatusCodes.Status200OK); + .WithStandardProduces(); ; group.MapGet("/{callbackId:guid}", Handlers.CallbackHandlers.GetCallback) .WithName("GetCallback") .WithSummary("Gets a callback by ID") - .Produces>(StatusCodes.Status200OK); + .WithStandardProduces(); return endpoints; } diff --git a/CallbackHandler/Handlers/CallbackHandlers.cs b/CallbackHandler/Handlers/CallbackHandlers.cs index e23435c..c7951ab 100644 --- a/CallbackHandler/Handlers/CallbackHandlers.cs +++ b/CallbackHandler/Handlers/CallbackHandlers.cs @@ -1,21 +1,28 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using CallbackHandler.BusinessLogic.Requests; +using CallbackHandler.BusinessLogic.Requests; using CallbackHandler.DataTransferObjects; using CallbackHandlers.Models; using MediatR; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Shared.Results; using SimpleResults; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http.HttpResults; +using Shared.Results.Web; namespace CallbackHandler.Handlers; public static class CallbackHandlers { - public static async Task> RecordCallback(Deposit depositCallback, - IMediator mediator, - CancellationToken cancellationToken) + public static async Task RecordCallback(Deposit depositCallback, + IMediator mediator, + CancellationToken cancellationToken) { Guid callbackId = Guid.NewGuid(); @@ -29,31 +36,23 @@ public static async Task> RecordCallback(Deposit depositCallback, Result result = await mediator.Send(request, cancellationToken); - if (result.IsFailed) { - return ResultHelpers.CreateFailure(result); - } - - return Result.Success(callbackId); + return ResponseFactory.FromResult(result, (r) => new CallbackResponse { + CallbackId = callbackId + }); } - public static async Task> GetCallback(Guid callbackId, - IMediator mediator, - CancellationToken cancellationToken) + public static async Task GetCallback(Guid callbackId, + IMediator mediator, + CancellationToken cancellationToken) { CallbackQueries.GetCallbackQuery query = new(callbackId); - Result getResult = await mediator.Send(query, cancellationToken); + Result result = await mediator.Send(query, cancellationToken); - if (getResult.IsFailed) { - return ResultHelpers.CreateFailure(getResult); - } - - Result result = Result.Success(new DataTransferObjects.CallbackMessage + return ResponseFactory.FromResult(result, (r) => new DataTransferObjects.CallbackMessage { - Reference = getResult.Data.Reference, - TypeString = getResult.Data.TypeString, - Message = getResult.Data.Message + Reference = r.Reference, + TypeString = r.TypeString, + Message = r.Message }); - - return result; } } \ No newline at end of file From e40d087a320e6e084b398eaee24d5fc47f9d69b3 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Sun, 2 Nov 2025 15:53:37 +0000 Subject: [PATCH 2/3] fix failed tests --- CallbackHandler.IntegrationTests/Shared/SharedSteps.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs b/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs index c123a03..bf4b5c7 100644 --- a/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs +++ b/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs @@ -53,10 +53,10 @@ public async Task WhenISendTheRequestsToTheCallbackHandlerForDeposits() var response = await client.SendAsync(msg); response.StatusCode.ShouldBe(HttpStatusCode.OK); var content = await response.Content.ReadAsStringAsync(); - ResponseData responseData = - JsonConvert.DeserializeObject>(content); + CallbackResponse responseData = + JsonConvert.DeserializeObject(content); - this.TestingContext.SentCallbacks.Add(responseData.Data, payload); + this.TestingContext.SentCallbacks.Add(responseData.CallbackId, payload); } } From b2ae7091e60b0a9cd20778232d33ecfa4a0b7a3d Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Sun, 2 Nov 2025 16:07:29 +0000 Subject: [PATCH 3/3] :| --- CallbackHandler.IntegrationTests/Shared/SharedSteps.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs b/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs index bf4b5c7..3920777 100644 --- a/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs +++ b/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs @@ -75,12 +75,12 @@ public async Task ThenTheDepositRecordsAreRecorded() var originalDeposit = JsonConvert.DeserializeObject(sentCallback.Value); var content = await response.Content.ReadAsStringAsync(); - ResponseData responseData = - JsonConvert.DeserializeObject>(content); + CallbackMessage responseData = + JsonConvert.DeserializeObject(content); - responseData.Data.Reference.ShouldBe(originalDeposit.Reference); - var callbackDeposit = JsonConvert.DeserializeObject(responseData.Data.Message); + responseData.Reference.ShouldBe(originalDeposit.Reference); + var callbackDeposit = JsonConvert.DeserializeObject(responseData.Message); callbackDeposit.AccountNumber.ShouldBe(originalDeposit.AccountNumber); } }