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:
parent
9b945ce232
commit
af280ee64e
@ -31,6 +31,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\EnvelopeGenerator.Common\EnvelopeGenerator.Common.vbproj" />
|
||||
<ProjectReference Include="..\EnvelopeGenerator.Domain\EnvelopeGenerator.Domain.csproj" />
|
||||
<ProjectReference Include="..\EnvelopeGenerator.Extensions\EnvelopeGenerator.Extensions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -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>();
|
||||
}
|
||||
}
|
||||
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
using EnvelopeGenerator.Application.Contracts.Services;
|
||||
using EnvelopeGenerator.Application.Histories.Queries.Read;
|
||||
using EnvelopeGenerator.Extensions;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
@ -20,15 +21,19 @@ public class HistoryController : ControllerBase
|
||||
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
/// <summary>
|
||||
/// Konstruktor für den HistoryController.
|
||||
/// </summary>
|
||||
/// <param name="service">Der Dienst, der für die Verarbeitung der Umschlaghistorie verantwortlich ist.</param>
|
||||
/// <param name="memoryCache"></param>
|
||||
public HistoryController(IEnvelopeHistoryService service, IMemoryCache memoryCache)
|
||||
/// param name="mediator"
|
||||
public HistoryController(IEnvelopeHistoryService service, IMemoryCache memoryCache, IMediator mediator)
|
||||
{
|
||||
_service = service;
|
||||
_memoryCache = memoryCache;
|
||||
_mediator = mediator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -96,7 +101,7 @@ public class HistoryController : ControllerBase
|
||||
/// <summary>
|
||||
/// Ruft die gesamte Umschlaghistorie basierend auf den angegebenen Abfrageparametern ab.
|
||||
/// </summary>
|
||||
/// <param name="history">Die Abfrageparameter, die die Filterkriterien für die Umschlaghistorie definieren.</param>
|
||||
/// <param name="historyQuery">Die Abfrageparameter, die die Filterkriterien für die Umschlaghistorie definieren.</param>
|
||||
/// <returns>Eine Liste von Historieneinträgen, die den angegebenen Kriterien entsprechen, oder nur der letzte Eintrag.</returns>
|
||||
/// <response code="200">Die Anfrage war erfolgreich, und die Umschlaghistorie wird zurückgegeben.</response>
|
||||
/// <response code="400">Die Anfrage war ungültig oder unvollständig.</response>
|
||||
@ -105,33 +110,10 @@ public class HistoryController : ControllerBase
|
||||
/// <response code="500">Ein unerwarteter Fehler ist aufgetreten.</response>
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetAllAsync([FromQuery] ReadHistoryQuery history)
|
||||
public async Task<IActionResult> GetAllAsync([FromQuery] ReadHistoryQuery historyQuery)
|
||||
{
|
||||
bool withReceiver = false;
|
||||
bool withSender = false;
|
||||
|
||||
switch (history.Related)
|
||||
{
|
||||
case ReferenceType.Receiver:
|
||||
withReceiver = true;
|
||||
break;
|
||||
case ReferenceType.Sender:
|
||||
withSender = true;
|
||||
break;
|
||||
}
|
||||
var history = await _mediator.Send(historyQuery);
|
||||
|
||||
var histories = await _service.ReadAsync(
|
||||
envelopeId: history.EnvelopeId,
|
||||
referenceType: history.Related,
|
||||
withSender: withSender,
|
||||
withReceiver: withReceiver);
|
||||
|
||||
if(history.Status is not null)
|
||||
histories = histories.Where(h => h.Status == (int)history.Status);
|
||||
|
||||
if (!histories.Any())
|
||||
return NotFound();
|
||||
|
||||
return Ok((history.OnlyLast ?? false) ? histories.MaxBy(h => h.AddedWhen) : histories);
|
||||
return Ok((historyQuery.OnlyLast ?? false) ? history.MaxBy(h => h.AddedWhen) : history);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user