Replaced EmailTemplateQueryBase record with IEmailTemplateQuery interface for email template queries. Updated all relevant commands, queries, and controller methods to use the new interface. Removed EmailTemplateQueryBase and migrated properties to implementing classes. Improved documentation to clarify query structure and Type property usage.
92 lines
3.0 KiB
C#
92 lines
3.0 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// Befehl zum Aktualisieren einer E-Mail-Vorlage.
|
|
/// </summary>
|
|
/// <param name="Body">
|
|
/// (Optional)Der neue Inhalt des E-Mail-Textkörpers. Wenn null, bleibt der vorhandene Inhalt unverändert.
|
|
/// </param>
|
|
/// <param name="Subject">
|
|
/// (Optional) Der neue Betreff der E-Mail. Wenn null, bleibt der vorhandene Betreff unverändert.
|
|
/// </param>
|
|
public record UpdateEmailTemplateCommand(string? Body = null, string? Subject = null) : IRequest
|
|
{
|
|
/// <param>
|
|
/// Die Abfrage, die die E-Mail-Vorlage darstellt, die aktualisiert werden soll.
|
|
/// </param>
|
|
[JsonIgnore]
|
|
public IEmailTemplateQuery? EmailTemplateQuery { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public DateTime ChangedWhen { get; init; } = DateTime.Now;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class UpdateEmailTemplateCommandHandler : IRequestHandler<UpdateEmailTemplateCommand>
|
|
{
|
|
private readonly IRepository<EmailTemplate> _repository;
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repository"></param>
|
|
/// <param name="mapper"></param>
|
|
public UpdateEmailTemplateCommandHandler(IRepository<EmailTemplate> repository, IMapper mapper)
|
|
{
|
|
_repository = repository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
/// <exception cref="NotFoundException"></exception>
|
|
[Obsolete("Use Read-method returning IReadQuery<TEntity> 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<EmailTemplateDto>(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<EmailTemplateDto>(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);
|
|
}
|
|
} |