From 2c43fdbaed7b47b7fe412424d6a6483fd63f97af Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 9 Feb 2026 09:16:13 +0100 Subject: [PATCH] Move handler into UpdateEmailTemplateCommand.cs Refactored by relocating UpdateEmailTemplateCommandHandler from its own file into UpdateEmailTemplateCommand.cs. Updated using statements accordingly. No logic changes; improves cohesion and maintainability. Removed the now-unnecessary UpdateEmailTemplateCommandHandler.cs file. --- .../Update/UpdateEmailTemplateCommand.cs | 67 ++++++++++++++++++- .../UpdateEmailTemplateCommandHandler.cs | 67 ------------------- 2 files changed, 65 insertions(+), 69 deletions(-) delete mode 100644 EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommandHandler.cs diff --git a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs index c507ff82..559c67ca 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs @@ -1,9 +1,15 @@ -using MediatR; +using AutoMapper; +using DigitalData.Core.Abstraction.Application.Repository; +using DigitalData.Core.Exceptions; +using MediatR; using System.Text.Json.Serialization; +using EnvelopeGenerator.Domain.Entities; +using Microsoft.EntityFrameworkCore; +using EnvelopeGenerator.Domain.Constants; +using EnvelopeGenerator.Application.Common.Dto; namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update; - /// /// Befehl zum Aktualisieren einer E-Mail-Vorlage. /// @@ -27,3 +33,60 @@ public record UpdateEmailTemplateCommand(string? Body = null, string? Subject = [JsonIgnore] public DateTime ChangedWhen { get; init; } = DateTime.Now; } + +/// +/// +/// +public class UpdateEmailTemplateCommandHandler : IRequestHandler +{ + private readonly IRepository _repository; + + private readonly IMapper _mapper; + + /// + /// + /// + /// + /// + public UpdateEmailTemplateCommandHandler(IRepository repository, IMapper mapper) + { + _repository = repository; + _mapper = mapper; + } + + /// + /// + /// + /// + /// + /// + /// + /// + [Obsolete("Use Read-method returning IReadQuery instead.")] + public async Task Handle(UpdateEmailTemplateCommand request, CancellationToken cancel) + { + EmailTemplateDto? tempDto; + + if (request.EmailTemplateQuery?.Id is int id) + { + var temp = await _repository.ReadOnly().Where(t => t.Id == id).FirstOrDefaultAsync(cancel); + tempDto = _mapper.Map(temp); + } + else if (request!.EmailTemplateQuery!.Type is EmailTemplateType type) + { + var temp = await _repository.ReadOnly().Where(t => t.Name == type.ToString()).FirstOrDefaultAsync(cancel); + tempDto = _mapper.Map(temp); + } + else + { + throw new InvalidOperationException("Both id and type is null. Id: " + request.EmailTemplateQuery.Id + ". Type: " + request.EmailTemplateQuery.Type.ToString()); + } + + if (tempDto == null) + { + throw new NotFoundException(); + } + + await _repository.UpdateAsync(tempDto, t => t.Id == tempDto.Id, cancel); + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommandHandler.cs b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommandHandler.cs deleted file mode 100644 index b4ac207a..00000000 --- a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommandHandler.cs +++ /dev/null @@ -1,67 +0,0 @@ -using AutoMapper; -using DigitalData.Core.Abstraction.Application.Repository; -using DigitalData.Core.Exceptions; -using EnvelopeGenerator.Domain.Entities; -using MediatR; -using Microsoft.EntityFrameworkCore; -using EnvelopeGenerator.Domain.Constants; -using EnvelopeGenerator.Application.Common.Dto; - -namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update; - -/// -/// -/// -public class UpdateEmailTemplateCommandHandler : IRequestHandler -{ - private readonly IRepository _repository; - - private readonly IMapper _mapper; - - /// - /// - /// - /// - /// - public UpdateEmailTemplateCommandHandler(IRepository repository, IMapper mapper) - { - _repository = repository; - _mapper = mapper; - } - - /// - /// - /// - /// - /// - /// - /// - /// - [Obsolete("Use Read-method returning IReadQuery instead.")] - public async Task Handle(UpdateEmailTemplateCommand request, CancellationToken cancel) - { - EmailTemplateDto? tempDto; - - if (request.EmailTemplateQuery?.Id is int id) - { - var temp = await _repository.ReadOnly().Where(t => t.Id == id).FirstOrDefaultAsync(cancel); - tempDto = _mapper.Map(temp); - } - else if (request!.EmailTemplateQuery!.Type is EmailTemplateType type) - { - var temp = await _repository.ReadOnly().Where(t => t.Name == type.ToString()).FirstOrDefaultAsync(cancel); - tempDto = _mapper.Map(temp); - } - else - { - throw new InvalidOperationException("Both id and type is null. Id: " + request.EmailTemplateQuery.Id +". Type: " + request.EmailTemplateQuery.Type.ToString()); - } - - if (tempDto == null) - { - throw new NotFoundException(); - } - - await _repository.UpdateAsync(tempDto, t => t.Id == tempDto.Id, cancel); - } -}