From 10341fd3ccf9d625a170b47414735fedfb363bb2 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 6 May 2025 20:58:52 +0200 Subject: [PATCH] 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`, 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`. --- .../DTOs/EmailTemplateDto.cs | 30 +++++++++++++--- .../ResetEnvelopeTemplateCommandHandler.cs | 35 ++++++++++++++----- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/EnvelopeGenerator.Application/DTOs/EmailTemplateDto.cs b/EnvelopeGenerator.Application/DTOs/EmailTemplateDto.cs index e22a4844..fbad5c4e 100644 --- a/EnvelopeGenerator.Application/DTOs/EmailTemplateDto.cs +++ b/EnvelopeGenerator.Application/DTOs/EmailTemplateDto.cs @@ -3,10 +3,30 @@ using Microsoft.AspNetCore.Mvc; namespace EnvelopeGenerator.Application.DTOs { + /// + /// + /// [ApiExplorerSettings(IgnoreApi = true)] - public record EmailTemplateDto( - int Id, - string Name, - string Body, - string Subject) : IUnique; + public record EmailTemplateDto : IUnique + { + /// + /// + /// + public int Id{ get; init; } + + /// + /// + /// + public required string Name { get; init; } + + /// + /// + /// + public required string Body { get; init; } + + /// + /// + /// + public required string Subject { get; init; } + }; } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/EmailTemplates/Commands/Reset/ResetEnvelopeTemplateCommandHandler.cs b/EnvelopeGenerator.Application/EmailTemplates/Commands/Reset/ResetEnvelopeTemplateCommandHandler.cs index 7e5b36e3..323b751c 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/Commands/Reset/ResetEnvelopeTemplateCommandHandler.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/Commands/Reset/ResetEnvelopeTemplateCommandHandler.cs @@ -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; /// public class ResetEnvelopeTemplateCommandHandler : IRequestHandler { + private readonly IRepository _repository; + /// /// /// - /// - /// - /// - /// - public Task Handle(ResetEnvelopeTemplateCommand request, CancellationToken cancellationToken) + /// + public ResetEnvelopeTemplateCommandHandler(IRepository repository) { - throw new NotImplementedException(); + _repository = repository; + } + + public async Task Handle(ResetEnvelopeTemplateCommand request, CancellationToken cancel) + { + var temps = request.Id is not null + ? await _repository.ReadAllAsync(t => t.Id == request.Id, cancel) + : request.Type is not null + ? await _repository.ReadAllAsync(t => t.Name == request.Type.ToString(), cancel) + : await _repository.ReadAllAsync(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); + } } /// /// /// - public static readonly IEnumerable Default = new List() + public static readonly IEnumerable Defaults = new List() { new(){ Id = 1, @@ -86,4 +104,5 @@ public class ResetEnvelopeTemplateCommandHandler : IRequestHandler