Refactor UpdateEmailTemplateCommand to use base classes
Refactored UpdateEmailTemplateCommand and its handler to inherit from generic UpdateCommand and UpdateCommandHandler base classes. Replaced QueryExpression with BuildQueryExpression(), removed redundant Update property, and cleaned up unused usings. This improves code reuse, modularity, and consistency across update commands.
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
using AutoMapper;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Exceptions;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using EnvelopeGenerator.Application.Common.Commands;
|
||||
using EnvelopeGenerator.Domain.Constants;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using MediatR;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace EnvelopeGenerator.Application.EmailTemplates.Commands;
|
||||
@@ -18,7 +16,7 @@ public record EmailTemplateUpdateDto(string Body, string Subject);
|
||||
/// <summary>
|
||||
/// Befehl zum Aktualisieren einer E-Mail-Vorlage.
|
||||
/// </summary>
|
||||
public record UpdateEmailTemplateCommand : IEmailTemplateQuery, IRequest
|
||||
public record UpdateEmailTemplateCommand : UpdateCommand<EmailTemplateUpdateDto, EmailTemplate>, IEmailTemplateQuery
|
||||
{
|
||||
/// <summary>
|
||||
/// Die eindeutige Kennung der E-Mail-Vorlage (optional).
|
||||
@@ -43,40 +41,23 @@ public record UpdateEmailTemplateCommand : IEmailTemplateQuery, IRequest
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Expression<Func<EmailTemplate, bool>> QueryExpression => Id is int id
|
||||
/// <returns></returns>
|
||||
public override Expression<Func<EmailTemplate, bool>> BuildQueryExpression()
|
||||
=> Id is int id
|
||||
? temp => temp.Id == id
|
||||
: temp => temp!.Name == Type.ToString();
|
||||
|
||||
/// <summary>
|
||||
/// Die aktualisierte E-Mail-Vorlage.
|
||||
/// </summary>
|
||||
public EmailTemplateUpdateDto Update { get; init; } = null!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class UpdateEmailTemplateCommandHandler : IRequestHandler<UpdateEmailTemplateCommand>
|
||||
public class UpdateEmailTemplateCommandHandler : UpdateCommandHandler<UpdateEmailTemplateCommand, EmailTemplateUpdateDto, EmailTemplate>
|
||||
{
|
||||
private readonly IRepository<EmailTemplate> _repository;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="repository"></param>
|
||||
public UpdateEmailTemplateCommandHandler(IRepository<EmailTemplate> repository)
|
||||
public UpdateEmailTemplateCommandHandler(IRepository<EmailTemplate> repository) : base(repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancel"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
/// <exception cref="NotFoundException"></exception>
|
||||
public Task Handle(UpdateEmailTemplateCommand request, CancellationToken cancel)
|
||||
=> _repository.UpdateAsync(request.Update, request.QueryExpression, cancel);
|
||||
}
|
||||
Reference in New Issue
Block a user