diff --git a/EnvelopeGenerator.Application/DTOs/EmailTemplateDto.cs b/EnvelopeGenerator.Application/DTOs/EmailTemplateDto.cs index fbad5c4e..d9b82487 100644 --- a/EnvelopeGenerator.Application/DTOs/EmailTemplateDto.cs +++ b/EnvelopeGenerator.Application/DTOs/EmailTemplateDto.cs @@ -22,11 +22,11 @@ namespace EnvelopeGenerator.Application.DTOs /// /// /// - public required string Body { get; init; } + public required string Body { get; set; } /// /// /// - public required string Subject { get; init; } + public required string Subject { get; set; } }; } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs index 63d2a7fd..b6350bd1 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs @@ -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; /// /// (Optional) Der neue Betreff der E-Mail. Wenn null, bleibt der vorhandene Betreff unverändert. /// -public record UpdateEmailTemplateCommand(string? Body = null, string? Subject = null) +public record UpdateEmailTemplateCommand(string? Body = null, string? Subject = null) : IRequest { /// /// Die Abfrage, die die E-Mail-Vorlage darstellt, die aktualisiert werden soll. diff --git a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommandHandler.cs b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommandHandler.cs new file mode 100644 index 00000000..3d1462e9 --- /dev/null +++ b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommandHandler.cs @@ -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; + +/// +/// +/// +public class UpdateEmailTemplateCommandHandler : IRequestHandler +{ + private readonly IRepository _repository; + + /// + /// + /// + /// + public UpdateEmailTemplateCommandHandler(IRepository repository) + { + _repository = repository; + } + + public async Task Handle(UpdateEmailTemplateCommand request, CancellationToken cancel) + { + var temp = (request.EmailTemplateQuery?.Id is int id + ? await _repository.ReadOrDefaultAsync(t => t.Id == id, single: false, cancel) + : request!.EmailTemplateQuery!.Type is Common.Constants.EmailTemplateType type + ? await _repository.ReadOrDefaultAsync(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); + } +} diff --git a/EnvelopeGenerator.Application/Exceptions/NotFoundException.cs b/EnvelopeGenerator.Application/Exceptions/NotFoundException.cs new file mode 100644 index 00000000..2e887b44 --- /dev/null +++ b/EnvelopeGenerator.Application/Exceptions/NotFoundException.cs @@ -0,0 +1,12 @@ +namespace EnvelopeGenerator.Application.Exceptions; + +public class NotFoundException : Exception +{ + public NotFoundException() + { + } + + public NotFoundException(string? message) : base(message) + { + } +} diff --git a/EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs b/EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs index fd93c584..db98a28e 100644 --- a/EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs +++ b/EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs @@ -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(email); + await _mediator.Send(update); + return Ok(); } } + catch(NotFoundException) + { + return BadRequest(); + } catch (Exception ex) { _logger.LogError(ex, "An unexpected error occurred. {message}", ex.Message);