Developer 02 629c0d51b2 Enhance email template handling and documentation
- Added XML documentation to the `Handle` method in `UpdateEmailTemplateCommandHandler`.
- Improved readability in `ReadEmailTemplateQueryHandler` by storing the mapped response in a variable.
- Updated properties in `ReadEmailTemplateResponse` to be mutable and renamed `Type` to `Name` with a type change from `int` to `string`.
- Added data annotations in `EmailTemplate` for `AddedWhen` and introduced a new nullable `ChangedWhen` property.
- Included necessary using directives for data annotations in `EmailTemplate.cs`.
2025-05-07 17:00:26 +02:00

52 lines
1.9 KiB
C#

using DigitalData.Core.Abstractions.Infrastructure;
using EnvelopeGenerator.Application.DTOs;
using EnvelopeGenerator.Application.Exceptions;
using EnvelopeGenerator.Domain.Entities;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update;
/// <summary>
///
/// </summary>
public class UpdateEmailTemplateCommandHandler : IRequestHandler<UpdateEmailTemplateCommand>
{
private readonly IRepository<EmailTemplate> _repository;
/// <summary>
///
/// </summary>
/// <param name="repository"></param>
public UpdateEmailTemplateCommandHandler(IRepository<EmailTemplate> repository)
{
_repository = repository;
}
/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancel"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="NotFoundException"></exception>
public async Task Handle(UpdateEmailTemplateCommand request, CancellationToken cancel)
{
var temp = (request.EmailTemplateQuery?.Id is int id
? await _repository.ReadOrDefaultAsync<EmailTemplateDto>(t => t.Id == id, single: false, cancel)
: request!.EmailTemplateQuery!.Type is Common.Constants.EmailTemplateType type
? await _repository.ReadOrDefaultAsync<EmailTemplateDto>(t => t.Name == type.ToString(), single: false, cancel)
: throw new InvalidOperationException("Both id and type is null. Id: " + request.EmailTemplateQuery.Id + ". Type: " + request.EmailTemplateQuery.Type.ToString())) ?? throw new NotFoundException();
if(request.Body is not null)
temp.Body = request.Body;
if (request.Subject is not null)
temp.Subject = request.Subject;
await _repository.UpdateAsync(temp, t => t.Id == temp.Id, cancel);
}
}