refactor(SaveDocStatusCommand): update to return DocumentStatusDto.

This commit is contained in:
2025-09-03 13:03:09 +02:00
parent 6ab85f25eb
commit 6b00ab6a45

View File

@@ -3,6 +3,8 @@ using EnvelopeGenerator.Domain.Entities;
using MediatR;
using Microsoft.EntityFrameworkCore;
using EnvelopeGenerator.Application.Extensions;
using EnvelopeGenerator.Application.Dto;
using AutoMapper;
namespace EnvelopeGenerator.Application.DocStatus.Commands;
@@ -10,7 +12,7 @@ namespace EnvelopeGenerator.Application.DocStatus.Commands;
/// Represents a command to save the status of a document, either by creating a new status or updating an existing one based on the provided envelope and receiver identifiers.
/// It returns the identifier of the saved document status.
/// </summary>
public record SaveDocStatusCommand : ModifyDocStatusCommandBase, IRequest<int?>;
public record SaveDocStatusCommand : ModifyDocStatusCommandBase, IRequest<DocumentStatusDto?>;
/// <summary>
///
@@ -26,28 +28,37 @@ public static class Extensions
/// <param name="value"></param>
/// <param name="cancel"></param>
/// <returns></returns>
public static Task<int?> SignDocAsync(this IMediator mediator, string uuid, string signature, string value, CancellationToken cancel = default)
=> mediator.Send(new SaveDocStatusCommand()
{
Envelope = new() { Uuid = uuid },
Receiver = new() { Signature = signature },
Value = value
}, cancel);
public static async Task<int?> SignDocAsync(this IMediator mediator, string uuid, string signature, string value, CancellationToken cancel = default)
{
var docStatus = await mediator.Send(new SaveDocStatusCommand()
{
Envelope = new() { Uuid = uuid },
Receiver = new() { Signature = signature },
Value = value
}, cancel);
return docStatus?.Id;
}
}
/// <summary>
///
/// </summary>
public class SaveDocStatusCommandHandler : IRequestHandler<SaveDocStatusCommand, int?>
public class SaveDocStatusCommandHandler : IRequestHandler<SaveDocStatusCommand, DocumentStatusDto?>
{
private readonly IMapper _mapper;
private readonly IRepository<DocumentStatus> _repo;
/// <summary>
///
/// </summary>
/// <param name="mapper"></param>
/// <param name="repo"></param>
public SaveDocStatusCommandHandler(IRepository<DocumentStatus> repo)
public SaveDocStatusCommandHandler(IMapper mapper, IRepository<DocumentStatus> repo)
{
_mapper = mapper;
_repo = repo;
}
@@ -57,7 +68,7 @@ public class SaveDocStatusCommandHandler : IRequestHandler<SaveDocStatusCommand,
/// <param name="request"></param>
/// <param name="cancel"></param>
/// <returns></returns>
public async Task<int?> Handle(SaveDocStatusCommand request, CancellationToken cancel)
public async Task<DocumentStatusDto?> Handle(SaveDocStatusCommand request, CancellationToken cancel)
{
// ceck if exists
bool isExists = await _repo.ReadOnly().Where(request).AnyAsync(cancel);
@@ -74,6 +85,7 @@ public class SaveDocStatusCommandHandler : IRequestHandler<SaveDocStatusCommand,
}
var docStatus = await _repo.ReadOnly().Where(request).SingleOrDefaultAsync(cancel);
return docStatus?.Id;
return _mapper.Map<DocumentStatusDto>(docStatus);
}
}