5 Commits

11 changed files with 34 additions and 160 deletions
@@ -4,7 +4,7 @@ namespace HrynCo.NotificationService.DAL.Abstract.Repositories;
public interface IEmailTemplateRepository public interface IEmailTemplateRepository
{ {
Task<IReadOnlyList<EmailTemplate>> GetAllAsync(string? serviceName = null, string? key = null, CancellationToken ct = default); Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default);
Task<IReadOnlyList<EmailTemplate>> GetByServiceAsync(string serviceName, 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<EmailTemplate?> GetAsync(string serviceName, string key, string languageCode, CancellationToken ct = default);
Task AddAsync(EmailTemplate template, CancellationToken ct = default); Task AddAsync(EmailTemplate template, CancellationToken ct = default);
@@ -13,22 +13,9 @@ internal sealed class EmailTemplateRepository
{ {
} }
public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(string? serviceName = null, string? key = null, CancellationToken ct = default) public async Task<IReadOnlyList<EmailTemplate>> GetAllAsync(CancellationToken ct = default)
{ {
IQueryable<EmailTemplateEntity> query = EfRepository.Get(); List<EmailTemplateEntity> entities = await 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() .AsNoTracking()
.ToListAsync(ct); .ToListAsync(ct);
return entities.Select(MapToDomain).ToList(); return entities.Select(MapToDomain).ToList();
@@ -22,7 +22,7 @@ internal sealed class GetAllEmailTemplatesHandler
protected override async Task<ServiceResult<IReadOnlyList<EmailTemplate>>> DoOnHandle( protected override async Task<ServiceResult<IReadOnlyList<EmailTemplate>>> DoOnHandle(
GetAllEmailTemplatesQuery request, CancellationToken cancellationToken) GetAllEmailTemplatesQuery request, CancellationToken cancellationToken)
{ {
var templates = await _templates.GetAllAsync(request.ServiceName, request.Key, cancellationToken); var templates = await _templates.GetAllAsync(cancellationToken);
return Success(templates); return Success(templates);
} }
} }
@@ -4,5 +4,4 @@ using HrynCo.NotificationService.Services.Core;
namespace HrynCo.NotificationService.Services.EmailTemplates.GetAll; namespace HrynCo.NotificationService.Services.EmailTemplates.GetAll;
public sealed record GetAllEmailTemplatesQuery(string? ServiceName = null, string? Key = null) public sealed record GetAllEmailTemplatesQuery : IRequest<ServiceResult<IReadOnlyList<EmailTemplate>>>;
: IRequest<ServiceResult<IReadOnlyList<EmailTemplate>>>;
@@ -23,12 +23,9 @@ public class AdminTemplatesController : Controller
// GET /admin/templates // GET /admin/templates
[HttpGet("")] [HttpGet("")]
public async Task<IActionResult> Index([FromQuery] string? serviceName, [FromQuery] string? key, CancellationToken ct) public async Task<IActionResult> Index(CancellationToken ct)
{ {
ViewData["ServiceNameFilter"] = serviceName; var result = await _mediator.Send(new GetAllEmailTemplatesQuery(), ct);
ViewData["KeyFilter"] = key;
var result = await _mediator.Send(new GetAllEmailTemplatesQuery(serviceName, key), ct);
if (!result.IsSuccess) if (!result.IsSuccess)
{ {
ModelState.AddModelError("", result.Error?.Message ?? "Failed to load templates."); ModelState.AddModelError("", result.Error?.Message ?? "Failed to load templates.");
@@ -40,24 +37,14 @@ public class AdminTemplatesController : Controller
// GET /admin/templates/create // GET /admin/templates/create
[HttpGet("create")] [HttpGet("create")]
public IActionResult Create([FromQuery] string? serviceNameFilter, [FromQuery] string? keyFilter) public IActionResult Create()
{ {
return View("Edit", new EmailTemplateEditViewModel return View("Edit", new EmailTemplateEditViewModel());
{
ServiceNameFilter = serviceNameFilter,
KeyFilter = keyFilter
});
} }
// GET /admin/templates/{serviceName}/{key}/{languageCode} // GET /admin/templates/{serviceName}/{key}/{languageCode}
[HttpGet("{serviceName}/{key}/{languageCode}")] [HttpGet("{serviceName}/{key}/{languageCode}")]
public async Task<IActionResult> Edit( public async Task<IActionResult> Edit(string serviceName, string key, string languageCode, CancellationToken ct)
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); var result = await _mediator.Send(new GetEmailTemplateQuery(serviceName, key, languageCode), ct);
if (!result.IsSuccess || result.Result is null) if (!result.IsSuccess || result.Result is null)
@@ -73,9 +60,7 @@ public class AdminTemplatesController : Controller
Subject = template.Subject, Subject = template.Subject,
HtmlBody = template.HtmlBody, HtmlBody = template.HtmlBody,
TextBody = template.TextBody, TextBody = template.TextBody,
VariablesJson = JsonSerializer.Serialize(template.Variables), VariablesJson = JsonSerializer.Serialize(template.Variables)
ServiceNameFilter = serviceNameFilter,
KeyFilter = keyFilter
}; };
return View(vm); return View(vm);
@@ -139,21 +124,15 @@ public class AdminTemplatesController : Controller
} }
} }
return RedirectToAction(nameof(Index), new { serviceName = model.ServiceNameFilter, key = model.KeyFilter }); return RedirectToAction(nameof(Index));
} }
// POST /admin/templates/{serviceName}/{key}/{languageCode}/delete // POST /admin/templates/{serviceName}/{key}/{languageCode}/delete
[HttpPost("{serviceName}/{key}/{languageCode}/delete")] [HttpPost("{serviceName}/{key}/{languageCode}/delete")]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Delete( public async Task<IActionResult> Delete(string serviceName, string key, string languageCode, CancellationToken ct)
string serviceName,
string key,
string languageCode,
[FromForm] string? serviceNameFilter,
[FromForm] string? keyFilter,
CancellationToken ct)
{ {
await _mediator.Send(new DeleteEmailTemplateCommand(serviceName, key, languageCode), ct); await _mediator.Send(new DeleteEmailTemplateCommand(serviceName, key, languageCode), ct);
return RedirectToAction(nameof(Index), new { serviceName = serviceNameFilter, key = keyFilter }); return RedirectToAction(nameof(Index));
} }
} }
@@ -25,8 +25,6 @@ public class EmailTemplateEditViewModel
// JSON array: [{"name":"UserName","required":true}, ...] // JSON array: [{"name":"UserName","required":true}, ...]
public string VariablesJson { get; set; } = "[]"; public string VariablesJson { get; set; } = "[]";
public string? ServiceNameFilter { get; set; }
public string? KeyFilter { get; set; }
public bool IsNew => Id == null; public bool IsNew => Id == null;
public string PageTitle => IsNew ? "Create Email Template" : "Edit Email Template"; public string PageTitle => IsNew ? "Create Email Template" : "Edit Email Template";
@@ -1,7 +1,6 @@
using HrynCo.NotificationService.Web.Infrastructure; using HrynCo.NotificationService.Web.Infrastructure;
using HrynCo.NotificationService.Services.EmailTemplates.Create; using HrynCo.NotificationService.Services.EmailTemplates.Create;
using HrynCo.NotificationService.Services.EmailTemplates.Delete; using HrynCo.NotificationService.Services.EmailTemplates.Delete;
using HrynCo.NotificationService.Services.EmailTemplates.GetAll;
using HrynCo.NotificationService.Services.EmailTemplates.Get; using HrynCo.NotificationService.Services.EmailTemplates.Get;
using HrynCo.NotificationService.Services.EmailTemplates.GetByService; using HrynCo.NotificationService.Services.EmailTemplates.GetByService;
using HrynCo.NotificationService.Services.EmailTemplates.Update; using HrynCo.NotificationService.Services.EmailTemplates.Update;
@@ -16,9 +15,9 @@ public sealed class EmailTemplatesController : ApiControllerBase
public EmailTemplatesController(IMediator mediator) : base(mediator) { } public EmailTemplatesController(IMediator mediator) : base(mediator) { }
[HttpGet] [HttpGet]
public async Task<IActionResult> GetAll([FromQuery] string? serviceName, [FromQuery] string? key, CancellationToken cancellationToken) public async Task<IActionResult> GetAll([FromQuery] string serviceName, CancellationToken cancellationToken)
{ {
var result = await Mediator.Send(new GetAllEmailTemplatesQuery(serviceName, key), cancellationToken); var result = await Mediator.Send(new GetEmailTemplatesQuery(serviceName), cancellationToken);
return FromServiceResult(result); return FromServiceResult(result);
} }
+9 -6
View File
@@ -3,11 +3,11 @@ using HrynCo.NotificationService.DAL.EF;
using HrynCo.NotificationService.Services; using HrynCo.NotificationService.Services;
using Scalar.AspNetCore; using Scalar.AspNetCore;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.AddSerilog(); builder.AddSerilog();
AppSettings appSettings = builder.Configuration var appSettings = builder.Configuration
.GetSection(AppSettings.SectionName) .GetSection(AppSettings.SectionName)
.Get<AppSettings>() ?? throw new InvalidOperationException("App settings are not configured."); .Get<AppSettings>() ?? throw new InvalidOperationException("App settings are not configured.");
@@ -18,14 +18,17 @@ builder.Services.AddControllersWithViews()
builder.Services.AddNotificationDataAccess(appSettings.ConnectionString); builder.Services.AddNotificationDataAccess(appSettings.ConnectionString);
builder.Services.AddNotificationServices(); builder.Services.AddNotificationServices();
WebApplication app = builder.Build(); var app = builder.Build();
app.MapOpenApi(); if (app.Environment.IsDevelopment())
app.MapScalarApiReference(options =>
{ {
app.MapOpenApi();
app.MapScalarApiReference(options =>
{
options.Title = "HrynCo Notification Service"; options.Title = "HrynCo Notification Service";
options.Theme = ScalarTheme.DeepSpace; options.Theme = ScalarTheme.DeepSpace;
}); });
}
app.UseStaticFiles(); app.UseStaticFiles();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
@@ -10,8 +10,6 @@
@Html.AntiForgeryToken() @Html.AntiForgeryToken()
<input asp-for="Id" type="hidden" /> <input asp-for="Id" type="hidden" />
<input type="hidden" name="IsNew" value="@Model.IsNew" /> <input type="hidden" name="IsNew" value="@Model.IsNew" />
<input asp-for="ServiceNameFilter" type="hidden" />
<input asp-for="KeyFilter" type="hidden" />
@if (!ViewData.ModelState.IsValid) @if (!ViewData.ModelState.IsValid)
{ {
@@ -118,7 +116,7 @@
<button type="submit" form="templateForm" class="btn btn-primary"> <button type="submit" form="templateForm" class="btn btn-primary">
<i class="bi bi-floppy me-1"></i> Save <i class="bi bi-floppy me-1"></i> Save
</button> </button>
<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"> <a href="/admin/templates" class="btn btn-secondary">
<i class="bi bi-x-lg me-1"></i> Cancel <i class="bi bi-x-lg me-1"></i> Cancel
</a> </a>
} }
@@ -2,50 +2,15 @@
@model IReadOnlyList<EmailTemplate> @model IReadOnlyList<EmailTemplate>
@{ @{
ViewData["Title"] = "Email Templates"; 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"> <div class="page-header">
<h2><i class="bi bi-envelope-paper"></i> Email Templates</h2> <h2><i class="bi bi-envelope-paper"></i> Email Templates</h2>
<a href="/admin/templates/create@filterQuery" class="btn btn-primary btn-sm"> <a href="/admin/templates/create" class="btn btn-primary btn-sm">
<i class="bi bi-plus-lg me-1"></i> Create New Template <i class="bi bi-plus-lg me-1"></i> Create New Template
</a> </a>
</div> </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) @if (!ViewData.ModelState.IsValid)
{ {
<div class="alert alert-danger"> <div class="alert alert-danger">
@@ -56,61 +21,6 @@
</div> </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) @if (Model is null || Model.Count == 0)
{ {
<div class="card shadow-sm table-card"> <div class="card shadow-sm table-card">
@@ -146,15 +56,13 @@ else
<td>@t.LanguageCode</td> <td>@t.LanguageCode</td>
<td>@t.Subject</td> <td>@t.Subject</td>
<td class="text-end"> <td class="text-end">
<a href="/admin/templates/@t.ServiceName/@t.Key/@t.LanguageCode@filterQuery" <a href="/admin/templates/@t.ServiceName/@t.Key/@t.LanguageCode"
class="btn btn-sm btn-outline-primary me-1"> class="btn btn-sm btn-outline-primary me-1">
<i class="bi bi-pencil"></i> Edit <i class="bi bi-pencil"></i> Edit
</a> </a>
<form method="post" <form method="post"
action="/admin/templates/@t.ServiceName/@t.Key/@t.LanguageCode/delete" action="/admin/templates/@t.ServiceName/@t.Key/@t.LanguageCode/delete"
class="d-inline"> class="d-inline">
<input type="hidden" name="serviceNameFilter" value="@serviceNameFilter" />
<input type="hidden" name="keyFilter" value="@keyFilter" />
@Html.AntiForgeryToken() @Html.AntiForgeryToken()
<button type="submit" <button type="submit"
class="btn btn-sm btn-outline-danger" class="btn btn-sm btn-outline-danger"
+3
View File
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<config>
<add key="globalPackagesFolder" value="%USERPROFILE%\.nuget\packages" />
</config>
<packageSources> <packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources> </packageSources>