- Updated `ReadEmailTemplateQuery` to implement `IRequest<ReadEmailTemplateResponse?>`. - Changed `ReadEmailTemplateResponse` from a record to a class with updated properties. - Enhanced `EmailTemplateController` to inject `IEmailTemplateRepository` and `IMediator`, and made the `Get` method asynchronous. - Introduced `ReadEmailTemplateMappingProfile` for AutoMapper mappings. - Added `ReadEmailTemplateQueryHandler` to manage query logic and response mapping. These changes improve the structure and maintainability of the email template querying process.
51 lines
1.8 KiB
C#
51 lines
1.8 KiB
C#
using AutoMapper;
|
|
using EnvelopeGenerator.Application.Contracts.Repositories;
|
|
using EnvelopeGenerator.Application.DTOs;
|
|
using EnvelopeGenerator.Common;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace EnvelopeGenerator.Application.EmailTemplates.Queries.Read;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ReadEmailTemplateQueryHandler : IRequestHandler<ReadEmailTemplateQuery, ReadEmailTemplateResponse?>
|
|
{
|
|
private readonly IMapper _mapper;
|
|
|
|
private readonly IEmailTemplateRepository _repository;
|
|
|
|
/// <summary>
|
|
/// Initialisiert eine neue Instanz der <see cref="EmailTemplateController"/>-Klasse.
|
|
/// </summary>
|
|
/// <param name="mapper">
|
|
/// <param name="repository">
|
|
/// Die AutoMapper-Instanz, die zum Zuordnen von Objekten verwendet wird.
|
|
/// </param>
|
|
public ReadEmailTemplateQueryHandler(IMapper mapper, IEmailTemplateRepository repository)
|
|
{
|
|
_mapper = mapper;
|
|
_repository = repository;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
public async Task<ReadEmailTemplateResponse?> Handle(ReadEmailTemplateQuery request, CancellationToken cancellationToken)
|
|
{
|
|
var temp = request.Id is int id
|
|
? await _repository.ReadByIdAsync(id)
|
|
: request.Type is Constants.EmailTemplateType type
|
|
? await _repository.ReadByNameAsync(type)
|
|
: throw new InvalidOperationException("Either a valid integer ID or a valid EmailTemplateType must be provided in the request.");
|
|
|
|
return temp is null ? null : _mapper.Map<ReadEmailTemplateResponse>(temp);
|
|
}
|
|
}
|