From 020cecabf3f111766f14ffb94325b3f1b9d2d96f Mon Sep 17 00:00:00 2001 From: TekH Date: Fri, 6 Mar 2026 11:08:03 +0100 Subject: [PATCH] Add query/handler to fetch default config via MediatR Introduced ReadDefaultConfigQuery and its handler to retrieve the application's default configuration using MediatR. The handler fetches the first Config entity from the repository, maps it to ConfigDto, and throws an exception if no configuration is found. --- .../Queries/ReadDefaultConfigQuery.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 EnvelopeGenerator.Application/Configuration/Queries/ReadDefaultConfigQuery.cs diff --git a/EnvelopeGenerator.Application/Configuration/Queries/ReadDefaultConfigQuery.cs b/EnvelopeGenerator.Application/Configuration/Queries/ReadDefaultConfigQuery.cs new file mode 100644 index 00000000..808ba713 --- /dev/null +++ b/EnvelopeGenerator.Application/Configuration/Queries/ReadDefaultConfigQuery.cs @@ -0,0 +1,49 @@ +using AutoMapper; +using DigitalData.Core.Abstraction.Application.Repository; +using EnvelopeGenerator.Application.Common.Dto; +using EnvelopeGenerator.Domain.Entities; +using MediatR; +using Microsoft.EntityFrameworkCore; + +namespace EnvelopeGenerator.Application.Configuration.Queries; + +/// +/// +/// +public record ReadDefaultConfigQuery : IRequest +{ +} + +/// +/// +/// +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 = await _repo.Query.FirstOrDefaultAsync(cancel) ?? throw new InvalidOperationException("No configuration found."); + return _mapper.Map(config); + } +} \ No newline at end of file