- Replace nullable `OnlyLast` with non-nullable default `true` - Support filtering by Envelope object (Id or Uuid) in addition to deprecated EnvelopeId - Throw `BadRequestException` if no valid Envelope reference is provided - Preserve status filtering and ordering for latest history entries
79 lines
2.4 KiB
C#
79 lines
2.4 KiB
C#
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using DigitalData.Core.Exceptions;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using MediatR;
|
|
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 CountHistoryQuery : HistoryQueryBase, IRequest<int>;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class CountHistoryQueryExtensions
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="queryOptions"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public static async Task<bool> AnyHistoryAsync(this ISender sender, Action<CountHistoryQuery> queryOptions, CancellationToken cancel = default)
|
|
{
|
|
var query = new CountHistoryQuery();
|
|
queryOptions(query);
|
|
var count = await sender.Send(query, cancel);
|
|
return count > 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class CountHistoryQueryHandler : IRequestHandler<CountHistoryQuery, int>
|
|
{
|
|
private readonly IRepository<History> _repo;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repo"></param>
|
|
public CountHistoryQueryHandler(IRepository<History> repo)
|
|
{
|
|
_repo = repo;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotFoundException"></exception>
|
|
public Task<int> Handle(CountHistoryQuery request, CancellationToken cancel = default)
|
|
{
|
|
var query = _repo.Query;
|
|
|
|
if (request.Envelope.Id is int envId)
|
|
query = query.Where(e => e.Id == envId);
|
|
else if (request.Envelope.Uuid is string uuid)
|
|
query = query.Where(e => e.Envelope!.Uuid == uuid);
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
|
else if (request.EnvelopeId is not null)
|
|
query = query.Where(h => h.EnvelopeId == request.EnvelopeId);
|
|
#pragma warning restore CS0618 // Type or member is obsolete
|
|
else
|
|
throw new BadRequestException("Invalid request: An Envelope object or a valid EnvelopeId/UUID must be supplied.");
|
|
|
|
if (request.Status is not null)
|
|
query = query.Where(h => h.Status == request.Status);
|
|
|
|
return query.CountAsync(cancel);
|
|
}
|
|
} |