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.
This commit is contained in:
2026-03-06 11:08:03 +01:00
parent 40c899e47e
commit 020cecabf3

View File

@@ -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;
/// <summary>
///
/// </summary>
public record ReadDefaultConfigQuery : IRequest<ConfigDto>
{
}
/// <summary>
///
/// </summary>
public class ReadDefaultConfigQueryHandler : IRequestHandler<ReadDefaultConfigQuery, ConfigDto>
{
private readonly IRepository<Config> _repo;
private readonly IMapper _mapper;
/// <summary>
///
/// </summary>
/// <param name="repo"></param>
/// <param name="mapper"></param>
public ReadDefaultConfigQueryHandler(IRepository<Config> repo, IMapper mapper)
{
_repo = repo;
_mapper = mapper;
}
/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancel"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public async Task<ConfigDto> Handle(ReadDefaultConfigQuery request, CancellationToken cancel)
{
var config = await _repo.Query.FirstOrDefaultAsync(cancel) ?? throw new InvalidOperationException("No configuration found.");
return _mapper.Map<ConfigDto>(config);
}
}