Renamed ReadEmailTemplateById and ReadEmailTemplateByType to ReadEmailTemplateAsync in ReadEmailTemplateQueryExtensions. This improves naming consistency and better reflects their asynchronous behavior. No changes to method signatures or functionality.
125 lines
4.7 KiB
C#
125 lines
4.7 KiB
C#
using AutoMapper;
|
|
using MediatR;
|
|
using EnvelopeGenerator.Application.Common.Dto;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using EnvelopeGenerator.Domain.Constants;
|
|
using DigitalData.Core.Exceptions;
|
|
using EnvelopeGenerator.Application.Common.Extensions;
|
|
|
|
namespace EnvelopeGenerator.Application.EmailTemplates.Queries;
|
|
|
|
/// <summary>
|
|
/// Stellt eine Abfrage dar, um eine E-Mail-Vorlage zu lesen.
|
|
/// Diese Klasse erbt von <see cref="IEmailTemplateQuery"/>.
|
|
/// </summary>
|
|
public record ReadEmailTemplateQuery : IEmailTemplateQuery, IRequest<IEnumerable<EmailTemplateDto>>
|
|
{
|
|
/// <summary>
|
|
/// Die eindeutige Kennung der E-Mail-Vorlage (optional).
|
|
/// </summary>
|
|
public int? Id { get; set; }
|
|
|
|
/// <summary>
|
|
/// Der Typ der E-Mail-Vorlage, z. B. <see cref="EmailTemplateType"/> (optional). Beispiele:<br/>
|
|
/// 0 - DocumentReceived: Benachrichtigung über den Empfang eines Dokuments.<br/>
|
|
/// 1 - DocumentSigned: Benachrichtigung über die Unterzeichnung eines Dokuments.<br/>
|
|
/// 2 - DocumentDeleted: Benachrichtigung über das Löschen eines Dokuments.<br/>
|
|
/// 3 - DocumentCompleted: Benachrichtigung über den Abschluss eines Dokuments.<br/>
|
|
/// 4 - DocumentAccessCodeReceived: Benachrichtigung über den Erhalt eines Zugangscodes.<br/>
|
|
/// 5 - DocumentShared: Benachrichtigung über das Teilen eines Dokuments.<br/>
|
|
/// 6 - TotpSecret: Benachrichtigung über ein TOTP-Geheimnis.<br/>
|
|
/// 7 - DocumentRejected_ADM (für den Absender): Mail an den Absender, wenn das Dokument abgelehnt wird.<br/>
|
|
/// 8 - DocumentRejected_REC (für den ablehnenden Empfänger): Mail an den ablehnenden Empfänger, wenn das Dokument abgelehnt wird.<br/>
|
|
/// 9 - DocumentRejected_REC_2 (für sonstige Empfänger): Mail an andere Empfänger (Brief), wenn das Dokument abgelehnt wird.
|
|
/// </summary>
|
|
public EmailTemplateType? Type { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? LangCode { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class ReadEmailTemplateQueryExtensions
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="langCode"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public static async Task<EmailTemplateDto?> ReadEmailTemplateAsync(this ISender sender, int id, string langCode, CancellationToken cancel = default)
|
|
{
|
|
var result = await sender.Send(new ReadEmailTemplateQuery { Id = id, LangCode = langCode }, cancel);
|
|
return result.FirstOrDefault();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="type"></param>
|
|
/// <param name="langCode"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public static async Task<EmailTemplateDto?> ReadEmailTemplateAsync(this ISender sender, EmailTemplateType type, string langCode, CancellationToken cancel = default)
|
|
{
|
|
var result = await sender.Send(new ReadEmailTemplateQuery { Type = type, LangCode = langCode }, cancel);
|
|
return result.FirstOrDefault();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ReadEmailTemplateQueryHandler : IRequestHandler<ReadEmailTemplateQuery, IEnumerable<EmailTemplateDto>>
|
|
{
|
|
private readonly IMapper _mapper;
|
|
|
|
private readonly IRepository<EmailTemplate> _repo;
|
|
|
|
/// <summary>
|
|
/// Initialisiert eine neue Instanz der <see cref="EmailTemplateController"/>-Klasse.
|
|
/// </summary>
|
|
/// <param name="mapper">
|
|
/// <param name="repo">
|
|
/// Die AutoMapper-Instanz, die zum Zuordnen von Objekten verwendet wird.
|
|
/// </param>
|
|
public ReadEmailTemplateQueryHandler(IMapper mapper, IRepository<EmailTemplate> repo)
|
|
{
|
|
_mapper = mapper;
|
|
_repo = repo;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
public async Task<IEnumerable<EmailTemplateDto>> Handle(ReadEmailTemplateQuery request, CancellationToken cancel)
|
|
{
|
|
var query = _repo.Query;
|
|
|
|
if (request.Id is int id)
|
|
query = query.Where(temp => temp.Id == id);
|
|
|
|
if (request.Type is EmailTemplateType type)
|
|
query = query.Where(temp => temp.Name == type.ToString());
|
|
|
|
if (request.LangCode is string langCode)
|
|
query = query.Where(temp => temp.LangCode == langCode);
|
|
|
|
var entity = await query.ToListAsync(cancel);
|
|
|
|
return _mapper.Map<IEnumerable<EmailTemplateDto>>(entity);
|
|
}
|
|
} |