refactor(SaveDocStatusCommand): update to return DocumentStatusDto.
This commit is contained in:
@@ -3,6 +3,8 @@ using EnvelopeGenerator.Domain.Entities;
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using EnvelopeGenerator.Application.Extensions;
|
using EnvelopeGenerator.Application.Extensions;
|
||||||
|
using EnvelopeGenerator.Application.Dto;
|
||||||
|
using AutoMapper;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Application.DocStatus.Commands;
|
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.
|
/// 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.
|
/// It returns the identifier of the saved document status.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SaveDocStatusCommand : ModifyDocStatusCommandBase, IRequest<int?>;
|
public record SaveDocStatusCommand : ModifyDocStatusCommandBase, IRequest<DocumentStatusDto?>;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
@@ -26,28 +28,37 @@ public static class Extensions
|
|||||||
/// <param name="value"></param>
|
/// <param name="value"></param>
|
||||||
/// <param name="cancel"></param>
|
/// <param name="cancel"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static Task<int?> SignDocAsync(this IMediator mediator, string uuid, string signature, string value, CancellationToken cancel = default)
|
public static async Task<int?> SignDocAsync(this IMediator mediator, string uuid, string signature, string value, CancellationToken cancel = default)
|
||||||
=> mediator.Send(new SaveDocStatusCommand()
|
{
|
||||||
{
|
|
||||||
Envelope = new() { Uuid = uuid },
|
var docStatus = await mediator.Send(new SaveDocStatusCommand()
|
||||||
Receiver = new() { Signature = signature },
|
{
|
||||||
Value = value
|
Envelope = new() { Uuid = uuid },
|
||||||
}, cancel);
|
Receiver = new() { Signature = signature },
|
||||||
|
Value = value
|
||||||
|
}, cancel);
|
||||||
|
|
||||||
|
return docStatus?.Id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SaveDocStatusCommandHandler : IRequestHandler<SaveDocStatusCommand, int?>
|
public class SaveDocStatusCommandHandler : IRequestHandler<SaveDocStatusCommand, DocumentStatusDto?>
|
||||||
{
|
{
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
private readonly IRepository<DocumentStatus> _repo;
|
private readonly IRepository<DocumentStatus> _repo;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="mapper"></param>
|
||||||
/// <param name="repo"></param>
|
/// <param name="repo"></param>
|
||||||
public SaveDocStatusCommandHandler(IRepository<DocumentStatus> repo)
|
public SaveDocStatusCommandHandler(IMapper mapper, IRepository<DocumentStatus> repo)
|
||||||
{
|
{
|
||||||
|
_mapper = mapper;
|
||||||
_repo = repo;
|
_repo = repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +68,7 @@ public class SaveDocStatusCommandHandler : IRequestHandler<SaveDocStatusCommand,
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="cancel"></param>
|
/// <param name="cancel"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<int?> Handle(SaveDocStatusCommand request, CancellationToken cancel)
|
public async Task<DocumentStatusDto?> Handle(SaveDocStatusCommand request, CancellationToken cancel)
|
||||||
{
|
{
|
||||||
// ceck if exists
|
// ceck if exists
|
||||||
bool isExists = await _repo.ReadOnly().Where(request).AnyAsync(cancel);
|
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);
|
var docStatus = await _repo.ReadOnly().Where(request).SingleOrDefaultAsync(cancel);
|
||||||
return docStatus?.Id;
|
|
||||||
|
return _mapper.Map<DocumentStatusDto>(docStatus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user