Refactor email template handling and error management

- Updated EmailTemplateDto to allow mutable Body and Subject properties.
- Implemented IRequest interface in UpdateEmailTemplateCommand for MediatR.
- Enhanced error handling in EmailTemplateController with NotFoundException.
- Introduced UpdateEmailTemplateCommandHandler for processing update commands.
- Added NotFoundException class for improved error handling.
This commit is contained in:
Developer 02 2025-05-07 01:02:30 +02:00
parent e4eb3e1192
commit 613b2130a5
5 changed files with 68 additions and 6 deletions

View File

@ -22,11 +22,11 @@ namespace EnvelopeGenerator.Application.DTOs
/// <summary>
///
/// </summary>
public required string Body { get; init; }
public required string Body { get; set; }
/// <summary>
///
/// </summary>
public required string Subject { get; init; }
public required string Subject { get; set; }
};
}

View File

@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using MediatR;
using System.Text.Json.Serialization;
namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update;
@ -12,7 +13,7 @@ namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update;
/// <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)
public record UpdateEmailTemplateCommand(string? Body = null, string? Subject = null) : IRequest
{
/// <param>
/// Die Abfrage, die die E-Mail-Vorlage darstellt, die aktualisiert werden soll.

View File

@ -0,0 +1,43 @@
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;
}
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);
}
}

View File

@ -0,0 +1,12 @@
namespace EnvelopeGenerator.Application.Exceptions;
public class NotFoundException : Exception
{
public NotFoundException()
{
}
public NotFoundException(string? message) : base(message)
{
}
}

View File

@ -10,6 +10,7 @@ using EnvelopeGenerator.Application.DTOs;
using MediatR;
using System.Threading.Tasks;
using DigitalData.UserManager.Application.Services;
using EnvelopeGenerator.Application.Exceptions;
namespace EnvelopeGenerator.GeneratorAPI.Controllers;
@ -116,11 +117,16 @@ public class EmailTemplateController : ControllerBase
}
else
{
update.EmailTemplateQuery = email;
// Logic for updating the email template
var reset = _mapper.Map<ResetEnvelopeTemplateCommand>(email);
await _mediator.Send(update);
return Ok();
}
}
catch(NotFoundException)
{
return BadRequest();
}
catch (Exception ex)
{
_logger.LogError(ex, "An unexpected error occurred. {message}", ex.Message);