Refactor EmailTemplateDto and update command handler
The `EmailTemplateDto` class has been changed from a record with positional parameters to a class with explicit properties, including XML documentation and required modifiers for `Name`, `Body`, and `Subject`. In the `ResetEnvelopeTemplateCommandHandler`, added necessary using directives, modified the constructor to accept an `IRepository<EmailTemplate>`, and updated the `Handle` method to read and update email templates based on the request's ID or type. The static `Default` collection has been renamed to `Defaults` and now uses `EmailTemplateDto`.
This commit is contained in:
parent
05de44bc13
commit
10341fd3cc
@ -3,10 +3,30 @@ using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace EnvelopeGenerator.Application.DTOs
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[ApiExplorerSettings(IgnoreApi = true)]
|
||||
public record EmailTemplateDto(
|
||||
int Id,
|
||||
string Name,
|
||||
string Body,
|
||||
string Subject) : IUnique<int>;
|
||||
public record EmailTemplateDto : IUnique<int>
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public int Id{ get; init; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string Name { get; init; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string Body { get; init; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string Subject { get; init; }
|
||||
};
|
||||
}
|
||||
@ -1,4 +1,7 @@
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using DigitalData.Core.Abstractions.Infrastructure;
|
||||
using EnvelopeGenerator.Application.DTOs;
|
||||
using EnvelopeGenerator.Application.EmailTemplates.Queries.Read;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using MediatR;
|
||||
|
||||
namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Reset;
|
||||
@ -8,22 +11,37 @@ namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Reset;
|
||||
/// </summary>
|
||||
public class ResetEnvelopeTemplateCommandHandler : IRequestHandler<ResetEnvelopeTemplateCommand>
|
||||
{
|
||||
private readonly IRepository<EmailTemplate> _repository;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public Task Handle(ResetEnvelopeTemplateCommand request, CancellationToken cancellationToken)
|
||||
/// <param name="repository"></param>
|
||||
public ResetEnvelopeTemplateCommandHandler(IRepository<EmailTemplate> repository)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public async Task Handle(ResetEnvelopeTemplateCommand request, CancellationToken cancel)
|
||||
{
|
||||
var temps = request.Id is not null
|
||||
? await _repository.ReadAllAsync<EmailTemplateDto>(t => t.Id == request.Id, cancel)
|
||||
: request.Type is not null
|
||||
? await _repository.ReadAllAsync<EmailTemplateDto>(t => t.Name == request.Type.ToString(), cancel)
|
||||
: await _repository.ReadAllAsync<EmailTemplateDto>(ct: cancel);
|
||||
|
||||
foreach (var temp in temps)
|
||||
{
|
||||
var def = Defaults.Where(t => t.Name == temp.Name).FirstOrDefault();
|
||||
if(def is not null)
|
||||
await _repository.UpdateAsync(def, t => t.Id == temp.Id, cancel);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static readonly IEnumerable<EmailTemplate> Default = new List<EmailTemplate>()
|
||||
public static readonly IEnumerable<EmailTemplateDto> Defaults = new List<EmailTemplateDto>()
|
||||
{
|
||||
new(){
|
||||
Id = 1,
|
||||
@ -86,4 +104,5 @@ public class ResetEnvelopeTemplateCommandHandler : IRequestHandler<ResetEnvelope
|
||||
Subject = "2-Faktor-Verifizierung QR-Code"
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user