From cc3857a4092260a8cf24524cb7dc1bcac85d8432 Mon Sep 17 00:00:00 2001 From: Anatolii Hrynchuk Date: Sun, 17 May 2026 13:53:37 +0300 Subject: [PATCH] feat: add persistence and restoration of email template filters in UI and backend --- .../Admin/AdminTemplatesController.cs | 32 +++++++-- .../ViewModels/EmailTemplateEditViewModel.cs | 2 + .../Views/AdminTemplates/Edit.cshtml | 4 +- .../Views/AdminTemplates/Index.cshtml | 71 +++++++++++++++++-- 4 files changed, 97 insertions(+), 12 deletions(-) diff --git a/HrynCo.NotificationService.Web/Controllers/Admin/AdminTemplatesController.cs b/HrynCo.NotificationService.Web/Controllers/Admin/AdminTemplatesController.cs index c421397..c8c35b6 100644 --- a/HrynCo.NotificationService.Web/Controllers/Admin/AdminTemplatesController.cs +++ b/HrynCo.NotificationService.Web/Controllers/Admin/AdminTemplatesController.cs @@ -40,14 +40,24 @@ public class AdminTemplatesController : Controller // GET /admin/templates/create [HttpGet("create")] - public IActionResult Create() + public IActionResult Create([FromQuery] string? serviceNameFilter, [FromQuery] string? keyFilter) { - return View("Edit", new EmailTemplateEditViewModel()); + return View("Edit", new EmailTemplateEditViewModel + { + ServiceNameFilter = serviceNameFilter, + KeyFilter = keyFilter + }); } // GET /admin/templates/{serviceName}/{key}/{languageCode} [HttpGet("{serviceName}/{key}/{languageCode}")] - public async Task Edit(string serviceName, string key, string languageCode, CancellationToken ct) + public async Task Edit( + string serviceName, + string key, + string languageCode, + [FromQuery] string? serviceNameFilter, + [FromQuery] string? keyFilter, + CancellationToken ct) { var result = await _mediator.Send(new GetEmailTemplateQuery(serviceName, key, languageCode), ct); if (!result.IsSuccess || result.Result is null) @@ -63,7 +73,9 @@ public class AdminTemplatesController : Controller Subject = template.Subject, HtmlBody = template.HtmlBody, TextBody = template.TextBody, - VariablesJson = JsonSerializer.Serialize(template.Variables) + VariablesJson = JsonSerializer.Serialize(template.Variables), + ServiceNameFilter = serviceNameFilter, + KeyFilter = keyFilter }; return View(vm); @@ -127,15 +139,21 @@ public class AdminTemplatesController : Controller } } - return RedirectToAction(nameof(Index)); + return RedirectToAction(nameof(Index), new { serviceName = model.ServiceNameFilter, key = model.KeyFilter }); } // POST /admin/templates/{serviceName}/{key}/{languageCode}/delete [HttpPost("{serviceName}/{key}/{languageCode}/delete")] [ValidateAntiForgeryToken] - public async Task Delete(string serviceName, string key, string languageCode, CancellationToken ct) + public async Task Delete( + string serviceName, + string key, + string languageCode, + [FromForm] string? serviceNameFilter, + [FromForm] string? keyFilter, + CancellationToken ct) { await _mediator.Send(new DeleteEmailTemplateCommand(serviceName, key, languageCode), ct); - return RedirectToAction(nameof(Index)); + return RedirectToAction(nameof(Index), new { serviceName = serviceNameFilter, key = keyFilter }); } } diff --git a/HrynCo.NotificationService.Web/Controllers/Admin/ViewModels/EmailTemplateEditViewModel.cs b/HrynCo.NotificationService.Web/Controllers/Admin/ViewModels/EmailTemplateEditViewModel.cs index d6600a3..ae61226 100644 --- a/HrynCo.NotificationService.Web/Controllers/Admin/ViewModels/EmailTemplateEditViewModel.cs +++ b/HrynCo.NotificationService.Web/Controllers/Admin/ViewModels/EmailTemplateEditViewModel.cs @@ -25,6 +25,8 @@ public class EmailTemplateEditViewModel // JSON array: [{"name":"UserName","required":true}, ...] public string VariablesJson { get; set; } = "[]"; + public string? ServiceNameFilter { get; set; } + public string? KeyFilter { get; set; } public bool IsNew => Id == null; public string PageTitle => IsNew ? "Create Email Template" : "Edit Email Template"; diff --git a/HrynCo.NotificationService.Web/Views/AdminTemplates/Edit.cshtml b/HrynCo.NotificationService.Web/Views/AdminTemplates/Edit.cshtml index 5171b6e..414e512 100644 --- a/HrynCo.NotificationService.Web/Views/AdminTemplates/Edit.cshtml +++ b/HrynCo.NotificationService.Web/Views/AdminTemplates/Edit.cshtml @@ -10,6 +10,8 @@ @Html.AntiForgeryToken() + + @if (!ViewData.ModelState.IsValid) { @@ -116,7 +118,7 @@ - + Cancel } diff --git a/HrynCo.NotificationService.Web/Views/AdminTemplates/Index.cshtml b/HrynCo.NotificationService.Web/Views/AdminTemplates/Index.cshtml index d16bf52..20907cb 100644 --- a/HrynCo.NotificationService.Web/Views/AdminTemplates/Index.cshtml +++ b/HrynCo.NotificationService.Web/Views/AdminTemplates/Index.cshtml @@ -4,18 +4,24 @@ ViewData["Title"] = "Email Templates"; var serviceNameFilter = ViewData["ServiceNameFilter"] as string ?? string.Empty; var keyFilter = ViewData["KeyFilter"] as string ?? string.Empty; + var filterQuery = string.IsNullOrWhiteSpace(serviceNameFilter) && string.IsNullOrWhiteSpace(keyFilter) + ? string.Empty + : $"?serviceNameFilter={Uri.EscapeDataString(serviceNameFilter)}&keyFilter={Uri.EscapeDataString(keyFilter)}"; + var listQuery = string.IsNullOrWhiteSpace(serviceNameFilter) && string.IsNullOrWhiteSpace(keyFilter) + ? string.Empty + : $"?serviceName={Uri.EscapeDataString(serviceNameFilter)}&key={Uri.EscapeDataString(keyFilter)}"; }
-
+
- Clear + Clear
@@ -50,6 +56,61 @@
} + + @if (Model is null || Model.Count == 0) {
@@ -85,13 +146,15 @@ else @t.LanguageCode @t.Subject - Edit
+ + @Html.AntiForgeryToken()