refactor(application): centralize API envelope creation and command mapping
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using DigitalData.TaskFlow.Domain.Common;
|
||||
using DigitalData.TaskFlow.Domain.Enums;
|
||||
|
||||
namespace DigitalData.TaskFlow.Application.Common.Factories;
|
||||
|
||||
/// <summary>
|
||||
/// Creates standard API envelope instances with centralized meta code/message handling.
|
||||
/// </summary>
|
||||
public static class ApiEnvelopeFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a successful or business-result response envelope.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Payload type.</typeparam>
|
||||
/// <param name="data">Payload data.</param>
|
||||
/// <param name="code">Business result code.</param>
|
||||
/// <param name="message">Optional custom message. If not supplied, a default message is resolved from <paramref name="code"/>.</param>
|
||||
/// <returns>Response envelope containing meta and payload.</returns>
|
||||
public static ApiEnvelope<T> Create<T>(T data, MetaCode code = MetaCode.Ok, string? message = null)
|
||||
{
|
||||
return new ApiEnvelope<T>
|
||||
{
|
||||
Meta =
|
||||
[
|
||||
new MetaEntry
|
||||
{
|
||||
Code = (int)code,
|
||||
Message = message ?? ResolveDefaultMessage(code)
|
||||
}
|
||||
],
|
||||
Data = data
|
||||
};
|
||||
}
|
||||
|
||||
private static string ResolveDefaultMessage(MetaCode code)
|
||||
{
|
||||
return code switch
|
||||
{
|
||||
MetaCode.Ok => "OK",
|
||||
MetaCode.NoOpenWorkflows => "Keine offenen Workflows",
|
||||
MetaCode.NoAccess => "Kein Zugriff",
|
||||
MetaCode.InvalidRequest => "Ungultiger Request",
|
||||
MetaCode.InternalError => "Interner Fehler",
|
||||
MetaCode.NotFound => "Vorgang nicht gefunden",
|
||||
MetaCode.AlreadyClosed => "Vorgang bereits abgeschlossen",
|
||||
_ => "Interner Fehler"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using AutoMapper;
|
||||
using DigitalData.TaskFlow.Application.Features.Workflows.Commands.CloseWorkflow;
|
||||
using DigitalData.TaskFlow.Domain.Closing;
|
||||
|
||||
namespace DigitalData.TaskFlow.Application.Common.Mappings;
|
||||
|
||||
@@ -12,5 +14,6 @@ public sealed class ApplicationMappingProfile : Profile
|
||||
/// </summary>
|
||||
public ApplicationMappingProfile()
|
||||
{
|
||||
CreateMap<CloseWorkflowCommand, CloseWorkflowDecision>();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user