From 4cf54d36b9e12f77b1ac091beb21e8afb43abf7a Mon Sep 17 00:00:00 2001 From: TekH Date: Fri, 6 Mar 2026 11:35:49 +0100 Subject: [PATCH] Add EnforceSingleResult to config query and improve errors Added EnforceSingleResult to ReadDefaultConfigQuery to allow strict single-result enforcement. Updated handler logic to use SingleOrDefaultAsync when requested. Replaced InvalidOperationException with NotFoundException and improved error messaging when no configuration is found. --- .../Configuration/Queries/ReadDefaultConfigQuery.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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