refactor(ReadHistoryQuery): update to not throw exception.

- update controller to throw NotFound if the list is empty
This commit is contained in:
Developer 02 2025-08-28 18:30:27 +02:00
parent f7c988be9b
commit dad43de8b1
5 changed files with 7 additions and 12 deletions

View File

@ -33,7 +33,7 @@ public static class TaskExtensions
/// <param name="factory">Exception provider</param> /// <param name="factory">Exception provider</param>
/// <returns>The awaited collection if it is not <c>null</c> or empty.</returns> /// <returns>The awaited collection if it is not <c>null</c> or empty.</returns>
/// <exception cref="NotFoundException">Thrown if the result is <c>null</c> or empty.</exception> /// <exception cref="NotFoundException">Thrown if the result is <c>null</c> or empty.</exception>
public static async Task<IEnumerable<T>> ThrowIfNull<T, TException>(this Task<IEnumerable<T>> task, Func<TException> factory) where TException : Exception public static async Task<IEnumerable<T>> ThrowIfEmpty<T, TException>(this Task<IEnumerable<T>> task, Func<TException> factory) where TException : Exception
{ {
var result = await task; var result = await task;
return result?.Any() ?? false ? result : throw factory(); return result?.Any() ?? false ? result : throw factory();

View File

@ -16,6 +16,4 @@ public record ReadHistoryQuery(
[Required] [Required]
int EnvelopeId, int EnvelopeId,
Constants.EnvelopeStatus? Status = null, Constants.EnvelopeStatus? Status = null,
bool? OnlyLast = true) : IRequest<IEnumerable<EnvelopeHistoryDto>> bool? OnlyLast = true) : IRequest<IEnumerable<EnvelopeHistoryDto>>;
{
};

View File

@ -42,10 +42,6 @@ public class ReadHistoryQueryHandler : IRequestHandler<ReadHistoryQuery, IEnumer
query = query.Where(h => h.Status == request.Status); query = query.Where(h => h.Status == request.Status);
var hists = await query.ToListAsync(cancel); var hists = await query.ToListAsync(cancel);
return _mapper.Map<IEnumerable<EnvelopeHistoryDto>>(hists);
if (hists.Count == 0)
return _mapper.Map<IEnumerable<EnvelopeHistoryDto>>(hists);
throw new NotFoundException();
} }
} }

View File

@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using static EnvelopeGenerator.Domain.Constants; using static EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.Application.Histories.Queries; using EnvelopeGenerator.Application.Histories.Queries;
using DigitalData.Core.Client;
using EnvelopeGenerator.Application.Extensions;
namespace EnvelopeGenerator.GeneratorAPI.Controllers; namespace EnvelopeGenerator.GeneratorAPI.Controllers;
@ -107,8 +109,7 @@ public class HistoryController : ControllerBase
[Authorize] [Authorize]
public async Task<IActionResult> GetAllAsync([FromQuery] ReadHistoryQuery historyQuery) public async Task<IActionResult> GetAllAsync([FromQuery] ReadHistoryQuery historyQuery)
{ {
var history = await _mediator.Send(historyQuery); var history = await _mediator.Send(historyQuery).ThrowIfEmpty(Exceptions.NotFound);
return Ok((historyQuery.OnlyLast ?? false) ? history.MaxBy(h => h.AddedWhen) : history); return Ok((historyQuery.OnlyLast ?? false) ? history.MaxBy(h => h.AddedWhen) : history);
} }
} }

View File

@ -27,7 +27,7 @@ public class TestEnvelopeReceiverController : ControllerBase
[HttpGet] [HttpGet]
public async Task<IActionResult> Get([FromQuery] ReadEnvelopeReceiverQuery q, CancellationToken cancel) public async Task<IActionResult> Get([FromQuery] ReadEnvelopeReceiverQuery q, CancellationToken cancel)
=> Ok(await _mediator.Send(q, cancel).ThrowIfNull(Exceptions.NotFound)); => Ok(await _mediator.Send(q, cancel).ThrowIfEmpty(Exceptions.NotFound));
[Obsolete("Use MediatR")] [Obsolete("Use MediatR")]
[HttpGet("verify-access-code/{envelope_receiver_id}")] [HttpGet("verify-access-code/{envelope_receiver_id}")]