using AutoMapper; using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.Core.Exceptions; using EnvelopeGenerator.Application.Common.Dto; using EnvelopeGenerator.Domain.Entities; using MediatR; using Microsoft.EntityFrameworkCore; namespace EnvelopeGenerator.Application.Configuration.Queries; /// /// /// public record ReadDefaultConfigQuery : IRequest { /// /// /// public bool EnforceSingleResult { get; init; } } /// /// /// public class ReadDefaultConfigQueryHandler : IRequestHandler { private readonly IRepository _repo; private readonly IMapper _mapper; /// /// /// /// /// public ReadDefaultConfigQueryHandler(IRepository repo, IMapper mapper) { _repo = repo; _mapper = mapper; } /// /// /// /// /// /// /// public async Task Handle(ReadDefaultConfigQuery request, CancellationToken cancel) { 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); } }