Add MediatR support for envelope history queries

Updated project references and introduced MediatR for handling
envelope history queries. Added new mapping profile and response
class, and refactored the HistoryController to utilize the
mediator pattern for improved query handling.
This commit is contained in:
Developer 02
2025-05-12 10:55:19 +02:00
parent 9b945ce232
commit af280ee64e
6 changed files with 140 additions and 47 deletions

View File

@@ -0,0 +1,18 @@
using AutoMapper;
using EnvelopeGenerator.Domain.Entities;
namespace EnvelopeGenerator.Application.Histories.Queries.Read;
/// <summary>
///
/// </summary>
public class ReadHistoryMappingProfile: Profile
{
/// <summary>
///
/// </summary>
public ReadHistoryMappingProfile()
{
CreateMap<EnvelopeHistory, ReadHistoryMappingProfile>();
}
}

View File

@@ -1,4 +1,5 @@
using EnvelopeGenerator.Common;
using MediatR;
namespace EnvelopeGenerator.Application.Histories.Queries.Read;
@@ -12,24 +13,6 @@ namespace EnvelopeGenerator.Application.Histories.Queries.Read;
public record ReadHistoryQuery(
int EnvelopeId,
Constants.EnvelopeStatus? Status = null,
bool? OnlyLast = true)
bool? OnlyLast = true) : IRequest<IEnumerable<ReadHistoryResponse>>
{
/// <summary>
/// Abfrage, die angibt, worauf sich der Datensatz bezieht. Ob er sich auf den Empfänger, den Sender oder das System bezieht, wird durch 0, 1 bzw. 2 dargestellt.
/// </summary>
public Constants.ReferenceType? Related
{
get
{
if (Status is null)
return null;
var statusCode = ((int)Status).ToString();
return statusCode.FirstOrDefault() switch
{
'1' => (Constants.ReferenceType?)Constants.ReferenceType.Sender,
'2' => (Constants.ReferenceType?)Constants.ReferenceType.Receiver,
_ => (Constants.ReferenceType?)Constants.ReferenceType.System,
};
}
}
};

View File

@@ -0,0 +1,44 @@
using AutoMapper;
using EnvelopeGenerator.Application.Contracts.Repositories;
using EnvelopeGenerator.Application.Exceptions;
using MediatR;
namespace EnvelopeGenerator.Application.Histories.Queries.Read;
/// <summary>
///
/// </summary>
public class ReadHistoryQueryHandler : IRequestHandler<ReadHistoryQuery, IEnumerable<ReadHistoryResponse>>
{
private readonly IEnvelopeHistoryRepository _repository;
private readonly IMapper _mapper;
/// <summary>
///
/// </summary>
/// <param name="repository"></param>
/// <param name="mapper"></param>
public ReadHistoryQueryHandler(IEnvelopeHistoryRepository repository, IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="NotFoundException"></exception>
public async Task<IEnumerable<ReadHistoryResponse>> Handle(ReadHistoryQuery request, CancellationToken cancellationToken)
{
var hists = await _repository.ReadAsync(request.EnvelopeId, status: request.Status is null ? null : (int) request.Status);
if (!hists.Any())
throw new NotFoundException();
return _mapper.Map<IEnumerable<ReadHistoryResponse>>(hists);
}
}

View File

@@ -0,0 +1,65 @@
using EnvelopeGenerator.Common;
namespace EnvelopeGenerator.Application.Histories.Queries.Read;
/// <summary>
/// Represents the history of an envelope, including its status, user actions, and references.
/// </summary>
public class ReadHistoryResponse
{
/// <summary>
/// Gets or sets the unique identifier of the envelope history record.
/// </summary>
public long Id { get; set; }
/// <summary>
/// Gets or sets the identifier of the associated envelope.
/// </summary>
public int EnvelopeId { get; set; }
/// <summary>
/// Gets or sets the reference identifier of the user who performed the action.
/// </summary>
public string UserReference { get; set; }
/// <summary>
/// Gets or sets the status code of the envelope.
/// </summary>
public int Status { get; set; }
/// <summary>
/// Gets or sets the name of the status.
/// </summary>
public string? StatusName => Status.ToString();
/// <summary>
///
/// </summary>
public Common.Constants.ReferenceType ReferenceType => Status.ToString().FirstOrDefault() switch
{
'1' => Constants.ReferenceType.Sender,
'2' => Constants.ReferenceType.Receiver,
_ => Constants.ReferenceType.System,
};
/// <summary>
/// Gets or sets the date and time when the record was added.
/// </summary>
public DateTime AddedWhen { get; set; }
/// <summary>
/// Gets or sets the date and time when the action occurred.
/// </summary>
public DateTime? ActionDate { get; set; }
/// <summary>
/// Gets or sets the optional comment about the envelope history record.
/// </summary>
public string? Comment { get; set; }
/// <inheritdoc/>
public override int GetHashCode()
{
return Id.GetHashCode();
}
}