diff --git a/EnvelopeGenerator.Application/Extensions/TaskExtensions.cs b/EnvelopeGenerator.Application/Extensions/TaskExtensions.cs index 8f598e52..01ad829a 100644 --- a/EnvelopeGenerator.Application/Extensions/TaskExtensions.cs +++ b/EnvelopeGenerator.Application/Extensions/TaskExtensions.cs @@ -33,7 +33,7 @@ public static class TaskExtensions /// Exception provider /// The awaited collection if it is not null or empty. /// Thrown if the result is null or empty. - public static async Task> ThrowIfNull(this Task> task, Func factory) where TException : Exception + public static async Task> ThrowIfEmpty(this Task> task, Func factory) where TException : Exception { var result = await task; return result?.Any() ?? false ? result : throw factory(); diff --git a/EnvelopeGenerator.Application/Histories/Queries/ReadHistoryQuery.cs b/EnvelopeGenerator.Application/Histories/Queries/ReadHistoryQuery.cs index c773c8e9..27490d43 100644 --- a/EnvelopeGenerator.Application/Histories/Queries/ReadHistoryQuery.cs +++ b/EnvelopeGenerator.Application/Histories/Queries/ReadHistoryQuery.cs @@ -16,6 +16,4 @@ public record ReadHistoryQuery( [Required] int EnvelopeId, Constants.EnvelopeStatus? Status = null, - bool? OnlyLast = true) : IRequest> -{ -}; \ No newline at end of file + bool? OnlyLast = true) : IRequest>; \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Histories/Queries/ReadHistoryQueryHandler.cs b/EnvelopeGenerator.Application/Histories/Queries/ReadHistoryQueryHandler.cs index 5d7a3829..ba558303 100644 --- a/EnvelopeGenerator.Application/Histories/Queries/ReadHistoryQueryHandler.cs +++ b/EnvelopeGenerator.Application/Histories/Queries/ReadHistoryQueryHandler.cs @@ -42,10 +42,6 @@ public class ReadHistoryQueryHandler : IRequestHandler h.Status == request.Status); var hists = await query.ToListAsync(cancel); - - if (hists.Count == 0) - return _mapper.Map>(hists); - - throw new NotFoundException(); + return _mapper.Map>(hists); } } \ No newline at end of file diff --git a/EnvelopeGenerator.GeneratorAPI/Controllers/HistoryController.cs b/EnvelopeGenerator.GeneratorAPI/Controllers/HistoryController.cs index 7fa40ac3..cecbab75 100644 --- a/EnvelopeGenerator.GeneratorAPI/Controllers/HistoryController.cs +++ b/EnvelopeGenerator.GeneratorAPI/Controllers/HistoryController.cs @@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; using static EnvelopeGenerator.Domain.Constants; using EnvelopeGenerator.Application.Histories.Queries; +using DigitalData.Core.Client; +using EnvelopeGenerator.Application.Extensions; namespace EnvelopeGenerator.GeneratorAPI.Controllers; @@ -107,8 +109,7 @@ public class HistoryController : ControllerBase [Authorize] public async Task 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); } } diff --git a/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs b/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs index c6a93bec..92cf808c 100644 --- a/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs +++ b/EnvelopeGenerator.Web/Controllers/Test/TestEnvelopeReceiverController.cs @@ -27,7 +27,7 @@ public class TestEnvelopeReceiverController : ControllerBase [HttpGet] public async Task 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")] [HttpGet("verify-access-code/{envelope_receiver_id}")]