Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cc3857a409 | |||
| 07f536938f |
@@ -4,10 +4,10 @@ namespace HrynCo.NotificationService.DAL.Abstract.Repositories;
|
||||
|
||||
public interface IEmailTemplateRepository
|
||||
{
|
||||
Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default);
|
||||
Task<IReadOnlyList<EmailTemplate>> GetAllAsync(string? serviceName = null, string? key = null, CancellationToken ct = default);
|
||||
Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, CancellationToken ct = default);
|
||||
Task<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default);
|
||||
Task AddAsync(EmailTemplate template, CancellationToken ct = default);
|
||||
Task UpdateAsync(EmailTemplate template, CancellationToken ct = default);
|
||||
Task DeleteAsync(EmailTemplate template, CancellationToken ct = default);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,21 @@ internal sealed class EmailTemplateRepository
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default)
|
||||
public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(string? serviceName = null, string? key = null, CancellationToken ct = default)
|
||||
{
|
||||
List<EmailTemplateEntity> entities = await EfRepository.Get()
|
||||
IQueryable<EmailTemplateEntity> query = EfRepository.Get();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(serviceName))
|
||||
{
|
||||
query = query.Where(x => x.ServiceName == serviceName);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
query = query.Where(x => x.Key == key);
|
||||
}
|
||||
|
||||
List<EmailTemplateEntity> entities = await query
|
||||
.OrderBy(x => x.ServiceName).ThenBy(x => x.Key)
|
||||
.AsNoTracking()
|
||||
.ToListAsync(ct);
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ internal sealed class GetAllEmailTemplatesHandler
|
||||
protected override async Task<ServiceResult<IReadOnlyList<EmailTemplate>>> DoOnHandle(
|
||||
GetAllEmailTemplatesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var templates = await _templates.GetAllAsync(cancellationToken);
|
||||
var templates = await _templates.GetAllAsync(request.ServiceName, request.Key, cancellationToken);
|
||||
return Success(templates);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -4,4 +4,5 @@ using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.GetAll;
|
||||
|
||||
public sealed record GetAllEmailTemplatesQuery : IRequest<ServiceResult<IReadOnlyList<EmailTemplate>>>;
|
||||
public sealed record GetAllEmailTemplatesQuery(string? ServiceName = null, string? Key = null)
|
||||
: IRequest<ServiceResult<IReadOnlyList<EmailTemplate>>>;
|
||||
|
||||
@@ -23,9 +23,12 @@ public class AdminTemplatesController : Controller
|
||||
|
||||
// GET /admin/templates
|
||||
[HttpGet("")]
|
||||
public async Task<IActionResult> Index(CancellationToken ct)
|
||||
public async Task<IActionResult> Index([FromQuery] string? serviceName, [FromQuery] string? key, CancellationToken ct)
|
||||
{
|
||||
var result = await _mediator.Send(new GetAllEmailTemplatesQuery(), ct);
|
||||
ViewData["ServiceNameFilter"] = serviceName;
|
||||
ViewData["KeyFilter"] = key;
|
||||
|
||||
var result = await _mediator.Send(new GetAllEmailTemplatesQuery(serviceName, key), ct);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
ModelState.AddModelError("", result.Error?.Message ?? "Failed to load templates.");
|
||||
@@ -37,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<IActionResult> Edit(string serviceName, string key, string languageCode, CancellationToken ct)
|
||||
public async Task<IActionResult> 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)
|
||||
@@ -60,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);
|
||||
@@ -124,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<IActionResult> Delete(string serviceName, string key, string languageCode, CancellationToken ct)
|
||||
public async Task<IActionResult> 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 });
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -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";
|
||||
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
using HrynCo.NotificationService.Web.Infrastructure;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Create;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Delete;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.GetAll;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Get;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.GetByService;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Update;
|
||||
@@ -15,9 +16,9 @@ public sealed class EmailTemplatesController : ApiControllerBase
|
||||
public EmailTemplatesController(IMediator mediator) : base(mediator) { }
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetAll([FromQuery] string serviceName, CancellationToken cancellationToken)
|
||||
public async Task<IActionResult> GetAll([FromQuery] string? serviceName, [FromQuery] string? key, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await Mediator.Send(new GetEmailTemplatesQuery(serviceName), cancellationToken);
|
||||
var result = await Mediator.Send(new GetAllEmailTemplatesQuery(serviceName, key), cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
@Html.AntiForgeryToken()
|
||||
<input asp-for="Id" type="hidden" />
|
||||
<input type="hidden" name="IsNew" value="@Model.IsNew" />
|
||||
<input asp-for="ServiceNameFilter" type="hidden" />
|
||||
<input asp-for="KeyFilter" type="hidden" />
|
||||
|
||||
@if (!ViewData.ModelState.IsValid)
|
||||
{
|
||||
@@ -116,7 +118,7 @@
|
||||
<button type="submit" form="templateForm" class="btn btn-primary">
|
||||
<i class="bi bi-floppy me-1"></i> Save
|
||||
</button>
|
||||
<a href="/admin/templates" class="btn btn-secondary">
|
||||
<a href="/admin/templates@(string.IsNullOrWhiteSpace(Model.ServiceNameFilter) && string.IsNullOrWhiteSpace(Model.KeyFilter) ? string.Empty : $"?serviceName={Uri.EscapeDataString(Model.ServiceNameFilter ?? string.Empty)}&key={Uri.EscapeDataString(Model.KeyFilter ?? string.Empty)}")" class="btn btn-secondary">
|
||||
<i class="bi bi-x-lg me-1"></i> Cancel
|
||||
</a>
|
||||
}
|
||||
|
||||
@@ -2,15 +2,50 @@
|
||||
@model IReadOnlyList<EmailTemplate>
|
||||
@{
|
||||
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)}";
|
||||
}
|
||||
|
||||
<div class="page-header">
|
||||
<h2><i class="bi bi-envelope-paper"></i> Email Templates</h2>
|
||||
<a href="/admin/templates/create" class="btn btn-primary btn-sm">
|
||||
<a href="/admin/templates/create@filterQuery" class="btn btn-primary btn-sm">
|
||||
<i class="bi bi-plus-lg me-1"></i> Create New Template
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div class="card shadow-sm mb-3">
|
||||
<div class="card-body">
|
||||
<form id="templateFiltersForm" method="get" action="/admin/templates" class="row g-2 align-items-end">
|
||||
<div class="col-12 col-md-5">
|
||||
<label class="form-label fw-semibold" for="serviceName">Service Name</label>
|
||||
<input id="serviceName"
|
||||
name="serviceName"
|
||||
value="@serviceNameFilter"
|
||||
class="form-control"
|
||||
placeholder="Filter by service name" />
|
||||
</div>
|
||||
<div class="col-12 col-md-5">
|
||||
<label class="form-label fw-semibold" for="key">Key</label>
|
||||
<input id="key"
|
||||
name="key"
|
||||
value="@keyFilter"
|
||||
class="form-control"
|
||||
placeholder="Filter by key" />
|
||||
</div>
|
||||
<div class="col-12 col-md-2 d-flex gap-2">
|
||||
<button type="submit" class="btn btn-primary w-100">Filter</button>
|
||||
<a id="clearTemplateFilters" href="/admin/templates" class="btn btn-outline-secondary w-100">Clear</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (!ViewData.ModelState.IsValid)
|
||||
{
|
||||
<div class="alert alert-danger">
|
||||
@@ -21,6 +56,61 @@
|
||||
</div>
|
||||
}
|
||||
|
||||
<script>
|
||||
(() => {
|
||||
const storageKey = 'hrynco.notificationService.adminTemplates.filters';
|
||||
const form = document.getElementById('templateFiltersForm');
|
||||
const serviceNameInput = document.getElementById('serviceName');
|
||||
const keyInput = document.getElementById('key');
|
||||
const clearLink = document.getElementById('clearTemplateFilters');
|
||||
|
||||
if (!form || !serviceNameInput || !keyInput || !clearLink) {
|
||||
return;
|
||||
}
|
||||
|
||||
const saveState = () => {
|
||||
const state = {
|
||||
serviceName: serviceNameInput.value ?? '',
|
||||
key: keyInput.value ?? ''
|
||||
};
|
||||
|
||||
localStorage.setItem(storageKey, JSON.stringify(state));
|
||||
};
|
||||
|
||||
const restoreState = () => {
|
||||
const raw = localStorage.getItem(storageKey);
|
||||
if (!raw) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const state = JSON.parse(raw);
|
||||
const serviceName = typeof state.serviceName === 'string' ? state.serviceName : '';
|
||||
const key = typeof state.key === 'string' ? state.key : '';
|
||||
|
||||
serviceNameInput.value = serviceName;
|
||||
keyInput.value = key;
|
||||
|
||||
return serviceName.length > 0 || key.length > 0;
|
||||
} catch {
|
||||
localStorage.removeItem(storageKey);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
form.addEventListener('submit', saveState);
|
||||
clearLink.addEventListener('click', () => localStorage.removeItem(storageKey));
|
||||
|
||||
const hasQueryParams = new URLSearchParams(window.location.search).toString().length > 0;
|
||||
if (!hasQueryParams && restoreState()) {
|
||||
form.requestSubmit();
|
||||
return;
|
||||
}
|
||||
|
||||
saveState();
|
||||
})();
|
||||
</script>
|
||||
|
||||
@if (Model is null || Model.Count == 0)
|
||||
{
|
||||
<div class="card shadow-sm table-card">
|
||||
@@ -56,13 +146,15 @@ else
|
||||
<td>@t.LanguageCode</td>
|
||||
<td>@t.Subject</td>
|
||||
<td class="text-end">
|
||||
<a href="/admin/templates/@t.ServiceName/@t.Key/@t.LanguageCode"
|
||||
<a href="/admin/templates/@t.ServiceName/@t.Key/@t.LanguageCode@filterQuery"
|
||||
class="btn btn-sm btn-outline-primary me-1">
|
||||
<i class="bi bi-pencil"></i> Edit
|
||||
</a>
|
||||
<form method="post"
|
||||
action="/admin/templates/@t.ServiceName/@t.Key/@t.LanguageCode/delete"
|
||||
class="d-inline">
|
||||
<input type="hidden" name="serviceNameFilter" value="@serviceNameFilter" />
|
||||
<input type="hidden" name="keyFilter" value="@keyFilter" />
|
||||
@Html.AntiForgeryToken()
|
||||
<button type="submit"
|
||||
class="btn btn-sm btn-outline-danger"
|
||||
|
||||
Reference in New Issue
Block a user