diff --git a/EnvelopeGenerator.Application/Configuration/Queries/ReadDefaultConfigQuery.cs b/EnvelopeGenerator.Application/Configuration/Queries/ReadDefaultConfigQuery.cs index 808ba713..0edf6f56 100644 --- a/EnvelopeGenerator.Application/Configuration/Queries/ReadDefaultConfigQuery.cs +++ b/EnvelopeGenerator.Application/Configuration/Queries/ReadDefaultConfigQuery.cs @@ -1,5 +1,6 @@ using AutoMapper; using DigitalData.Core.Abstraction.Application.Repository; +using DigitalData.Core.Exceptions; using EnvelopeGenerator.Application.Common.Dto; using EnvelopeGenerator.Domain.Entities; using MediatR; @@ -12,6 +13,10 @@ namespace EnvelopeGenerator.Application.Configuration.Queries; /// public record ReadDefaultConfigQuery : IRequest { + /// + /// + /// + public bool EnforceSingleResult { get; init; } } /// @@ -43,7 +48,10 @@ public class ReadDefaultConfigQueryHandler : IRequestHandler public async Task Handle(ReadDefaultConfigQuery request, CancellationToken cancel) { - var config = await _repo.Query.FirstOrDefaultAsync(cancel) ?? throw new InvalidOperationException("No configuration found."); + var config = request.EnforceSingleResult + ? await _repo.Query.SingleOrDefaultAsync(cancel) + : await _repo.Query.FirstOrDefaultAsync(cancel) + ?? throw new NotFoundException("Default configuration could not be found. Ensure at least one configuration record exists in the database."); return _mapper.Map(config); } } \ No newline at end of file