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:
parent
e4eb3e1192
commit
613b2130a5
@ -22,11 +22,11 @@ namespace EnvelopeGenerator.Application.DTOs
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public required string Body { get; init; }
|
public required string Body { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public required string Subject { get; init; }
|
public required string Subject { get; set; }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
using System.Text.Json.Serialization;
|
using MediatR;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update;
|
namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update;
|
||||||
|
|
||||||
@ -12,7 +13,7 @@ namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update;
|
|||||||
/// <param name="Subject">
|
/// <param name="Subject">
|
||||||
/// (Optional) Der neue Betreff der E-Mail. Wenn null, bleibt der vorhandene Betreff unverändert.
|
/// (Optional) Der neue Betreff der E-Mail. Wenn null, bleibt der vorhandene Betreff unverändert.
|
||||||
/// </param>
|
/// </param>
|
||||||
public record UpdateEmailTemplateCommand(string? Body = null, string? Subject = null)
|
public record UpdateEmailTemplateCommand(string? Body = null, string? Subject = null) : IRequest
|
||||||
{
|
{
|
||||||
/// <param>
|
/// <param>
|
||||||
/// Die Abfrage, die die E-Mail-Vorlage darstellt, die aktualisiert werden soll.
|
/// Die Abfrage, die die E-Mail-Vorlage darstellt, die aktualisiert werden soll.
|
||||||
|
|||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
namespace EnvelopeGenerator.Application.Exceptions;
|
||||||
|
|
||||||
|
public class NotFoundException : Exception
|
||||||
|
{
|
||||||
|
public NotFoundException()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public NotFoundException(string? message) : base(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -10,6 +10,7 @@ using EnvelopeGenerator.Application.DTOs;
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using DigitalData.UserManager.Application.Services;
|
using DigitalData.UserManager.Application.Services;
|
||||||
|
using EnvelopeGenerator.Application.Exceptions;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.GeneratorAPI.Controllers;
|
namespace EnvelopeGenerator.GeneratorAPI.Controllers;
|
||||||
|
|
||||||
@ -116,11 +117,16 @@ public class EmailTemplateController : ControllerBase
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
update.EmailTemplateQuery = email;
|
var reset = _mapper.Map<ResetEnvelopeTemplateCommand>(email);
|
||||||
// Logic for updating the email template
|
await _mediator.Send(update);
|
||||||
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
catch(NotFoundException)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "An unexpected error occurred. {message}", ex.Message);
|
_logger.LogError(ex, "An unexpected error occurred. {message}", ex.Message);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user