From fe252b9979c2213acdbf2dadaf5e82af8775ef93 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Sat, 12 Apr 2025 01:20:04 +0200 Subject: [PATCH] Refactor email template commands and add controller - Updated `UpdateEmailTemplateCommand` to use a property for `EmailTemplateQuery` with `JsonIgnore`, allowing optional body and subject parameters. - Simplified `UpdateToDefaultCommand` by removing documentation comments and constructor parameters. - Corrected namespace for `ReadEmailTemplateQuery` from `Query.Read` to `Queries.Read`. - Introduced `ResetEnvelopeTemplateCommand` with optional ID and type, inheriting from `EmailTemplateQuery`, along with detailed XML documentation. - Added `EmailTemplateController` to manage email templates, including methods for retrieval and updates, utilizing AutoMapper and authorization attributes. --- .../ResetEnvelopeTemplateCommand.cs} | 6 +- .../Update/UpdateEmailTemplateCommand.cs | 14 ++- .../Queries/Read/ReadEmailTemplateQuery.cs | 2 +- .../Controllers/EmailTemplateController.cs | 91 +++++++++++++++++++ 4 files changed, 105 insertions(+), 8 deletions(-) rename EnvelopeGenerator.Application/EmailTemplates/Commands/{UpdateToDefault/UpdateToDefaultCommand.cs => Reset/ResetEnvelopeTemplateCommand.cs} (85%) create mode 100644 EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs diff --git a/EnvelopeGenerator.Application/EmailTemplates/Commands/UpdateToDefault/UpdateToDefaultCommand.cs b/EnvelopeGenerator.Application/EmailTemplates/Commands/Reset/ResetEnvelopeTemplateCommand.cs similarity index 85% rename from EnvelopeGenerator.Application/EmailTemplates/Commands/UpdateToDefault/UpdateToDefaultCommand.cs rename to EnvelopeGenerator.Application/EmailTemplates/Commands/Reset/ResetEnvelopeTemplateCommand.cs index ee79caf2..79ce1295 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/Commands/UpdateToDefault/UpdateToDefaultCommand.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/Commands/Reset/ResetEnvelopeTemplateCommand.cs @@ -1,4 +1,6 @@ -namespace EnvelopeGenerator.Application.EmailTemplates.Commands.UpdateToDefault; +using EnvelopeGenerator.Common; + +namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Reset; /// /// Ein Befehl zum Zurücksetzen einer E-Mail-Vorlage auf die Standardwerte. @@ -17,4 +19,4 @@ /// 8 - DocumentRejected_REC (Für den ablehnenden Empfänger): Mail an den ablehnenden Empfänger, wenn das Dokument abgelehnt wird. /// 9 - DocumentRejected_REC_2 (Für sonstige Empfänger): Mail an andere Empfänger (Brief), wenn das Dokument abgelehnt wird. /// -public record UpdateToDefaultCommand(int? Id, Common.Constants.EmailTemplateType? Type) : EmailTemplateQuery(Id, Type); +public record ResetEnvelopeTemplateCommand(int? Id, Constants.EmailTemplateType? Type) : EmailTemplateQuery(Id, Type); diff --git a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs index bc1af611..63d2a7fd 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs @@ -1,18 +1,22 @@ -namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update; +using System.Text.Json.Serialization; + +namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update; /// /// Befehl zum Aktualisieren einer E-Mail-Vorlage. /// -/// -/// Die Abfrage, die die E-Mail-Vorlage darstellt, die aktualisiert werden soll. -/// /// /// (Optional)Der neue Inhalt des E-Mail-Textkörpers. Wenn null, bleibt der vorhandene Inhalt unverändert. /// /// /// (Optional) Der neue Betreff der E-Mail. Wenn null, bleibt der vorhandene Betreff unverändert. /// -public record UpdateEmailTemplateCommand(EmailTemplateQuery EmailTemplate, string? Body = null, string? Subject = null) +public record UpdateEmailTemplateCommand(string? Body = null, string? Subject = null) { + /// + /// Die Abfrage, die die E-Mail-Vorlage darstellt, die aktualisiert werden soll. + /// + [JsonIgnore] + public EmailTemplateQuery? EmailTemplateQuery { get; set; } } diff --git a/EnvelopeGenerator.Application/EmailTemplates/Queries/Read/ReadEmailTemplateQuery.cs b/EnvelopeGenerator.Application/EmailTemplates/Queries/Read/ReadEmailTemplateQuery.cs index c94ed22d..0c82bab2 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/Queries/Read/ReadEmailTemplateQuery.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/Queries/Read/ReadEmailTemplateQuery.cs @@ -1,4 +1,4 @@ -namespace EnvelopeGenerator.Application.EmailTemplates.Query.Read; +namespace EnvelopeGenerator.Application.EmailTemplates.Queries.Read; /// diff --git a/EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs b/EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs new file mode 100644 index 00000000..fa4fb84a --- /dev/null +++ b/EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs @@ -0,0 +1,91 @@ +using AutoMapper; +using EnvelopeGenerator.Application.EmailTemplates; +using EnvelopeGenerator.Application.EmailTemplates.Commands.Update; +using EnvelopeGenerator.Application.EmailTemplates.Commands.Reset; +using EnvelopeGenerator.Application.EmailTemplates.Queries.Read; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace EnvelopeGenerator.GeneratorAPI.Controllers; + +/// +/// Controller for managing email templates. +/// Steuerung zur Verwaltung von E-Mail-Vorlagen. +/// +[Route("api/[controller]")] +[ApiController] +[Authorize] +public class EmailTemplateController : ControllerBase +{ + private readonly IMapper _mapper; + + /// + /// Initialisiert eine neue Instanz der -Klasse. + /// + /// + /// Die AutoMapper-Instanz, die zum Zuordnen von Objekten verwendet wird. + /// + public EmailTemplateController(IMapper mapper) + { + _mapper = mapper; + } + + /// + /// Ruft E-Mail-Vorlagen basierend auf der angegebenen Abfrage ab. + /// Gibt alles zurück, wenn keine Id- oder Typ-Informationen eingegeben wurden. + /// + /// Die Abfrageparameter zum Abrufen von E-Mail-Vorlagen. + /// Gibt HTTP-Antwort zurück + /// + /// Sample request: + /// GET /api/EmailTemplate?emailTemplateId=123 + /// + /// Wenn die E-Mail-Vorlagen erfolgreich abgerufen werden. + /// Wenn die Abfrageparameter ungültig sind. + /// Wenn der Benutzer nicht authentifiziert ist. + /// Wenn die gesuchte Abfrage nicht gefunden wird. + [HttpGet] + public IActionResult Get([FromQuery] ReadEmailTemplateQuery? emailTemplate = null) + { + // Implementation logic here + return Ok(); // Placeholder for actual implementation + } + + /// + /// Updates an email template or resets it if no update command is provided. + /// Aktualisiert eine E-Mail-Vorlage oder setzt sie zurück, wenn kein Aktualisierungsbefehl angegeben ist. + /// + /// Die E-Mail-Vorlagenabfrage. + /// Der Aktualisierungsbefehl für die E-Mail-Vorlage. + /// Wird auf Standardwert aktualisiert, wenn die Anfrage ohne http-Body gesendet wird. + /// + /// Gibt HTTP-Antwort zurück + /// + /// Sample request: + /// PUT /api/EmailTemplate + /// { + /// "emailTemplateId": 123, + /// "newContent": "Updated content" + /// } + /// + /// Wenn die E-Mail-Vorlage erfolgreich aktualisiert oder zurückgesetzt wird. + /// Wenn die Abfrage ohne einen String gesendet wird. + /// Wenn der Benutzer nicht authentifiziert ist. + /// Wenn die gesuchte Abfrage nicht gefunden wird. + [HttpPut] + public IActionResult Update([FromQuery] EmailTemplateQuery email, [FromBody] UpdateEmailTemplateCommand? update = null) + { + if (update is null) + { + var reset = _mapper.Map(email); + // Logic for resetting the email template + } + else + { + update.EmailTemplateQuery = email; + // Logic for updating the email template + } + + return Ok(); // Placeholder for actual implementation + } +}