72 lines
2.2 KiB
C#
72 lines
2.2 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using DigitalData.Core.Exceptions;
|
|
using EnvelopeGenerator.Application.Common.Dto.History;
|
|
using EnvelopeGenerator.Domain.Constants;
|
|
using MediatR;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace EnvelopeGenerator.Application.Histories.Queries;
|
|
|
|
//TODO: Add sender query
|
|
/// <summary>
|
|
/// Repräsentiert eine Abfrage für die Verlaufshistorie eines Umschlags.
|
|
/// </summary>
|
|
public record ReadHistoryQuery : IRequest<IEnumerable<HistoryDto>>
|
|
{
|
|
/// <summary>
|
|
/// Die eindeutige Kennung des Umschlags.
|
|
/// </summary>
|
|
[Required]
|
|
public int EnvelopeId { get; init; }
|
|
|
|
/// <summary>
|
|
/// Der Include des Umschlags, der abgefragt werden soll. Kann optional angegeben werden, um die Ergebnisse zu filtern.
|
|
/// </summary>
|
|
public EnvelopeStatus? Status { get; init; }
|
|
|
|
/// <summary>
|
|
/// Abfrage zur Steuerung, ob nur der aktuelle Include oder der gesamte Datensatz zurückgegeben wird.
|
|
/// </summary>
|
|
public bool? OnlyLast { get; init; } = true;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ReadHistoryQueryHandler : IRequestHandler<ReadHistoryQuery, IEnumerable<HistoryDto>>
|
|
{
|
|
private readonly IRepository<History> _repo;
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repo"></param>
|
|
/// <param name="mapper"></param>
|
|
public ReadHistoryQueryHandler(IRepository<History> repo, IMapper mapper)
|
|
{
|
|
_repo = repo;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotFoundException"></exception>
|
|
public async Task<IEnumerable<HistoryDto>> Handle(ReadHistoryQuery request, CancellationToken cancel = default)
|
|
{
|
|
var query = _repo.Where(h => h.EnvelopeId == request.EnvelopeId);
|
|
if (request.Status is not null)
|
|
query = query.Where(h => h.Status == request.Status);
|
|
|
|
var hists = await query.ToListAsync(cancel);
|
|
return _mapper.Map<List<HistoryDto>>(hists);
|
|
}
|
|
} |