using System.Text.Json; using HrynCo.NotificationService.DAL.Abstract.Templates; using HrynCo.NotificationService.Services.EmailTemplates.Create; using HrynCo.NotificationService.Services.EmailTemplates.Delete; using HrynCo.NotificationService.Services.EmailTemplates.Get; using HrynCo.NotificationService.Services.EmailTemplates.GetAll; using HrynCo.NotificationService.Services.EmailTemplates.Update; using HrynCo.NotificationService.Web.Controllers.Admin.ViewModels; using MediatR; using Microsoft.AspNetCore.Mvc; namespace HrynCo.NotificationService.Web.Controllers.Admin; [Route("admin/templates")] public class AdminTemplatesController : Controller { private readonly IMediator _mediator; public AdminTemplatesController(IMediator mediator) { _mediator = mediator; } // GET /admin/templates [HttpGet("")] public async Task Index(CancellationToken ct) { var result = await _mediator.Send(new GetAllEmailTemplatesQuery(), ct); if (!result.IsSuccess) { ModelState.AddModelError("", result.Error?.Message ?? "Failed to load templates."); return View(Array.Empty()); } return View(result.Result); } // GET /admin/templates/create [HttpGet("create")] public IActionResult Create() { return View("Edit", new EmailTemplateEditViewModel()); } // GET /admin/templates/{serviceName}/{key}/{languageCode} [HttpGet("{serviceName}/{key}/{languageCode}")] public async Task Edit(string serviceName, string key, string languageCode, CancellationToken ct) { var result = await _mediator.Send(new GetEmailTemplateQuery(serviceName, key, languageCode), ct); if (!result.IsSuccess || result.Result is null) return NotFound(); var template = result.Result; var vm = new EmailTemplateEditViewModel { Id = template.Id, ServiceName = template.ServiceName, Key = template.Key, LanguageCode = template.LanguageCode, Subject = template.Subject, HtmlBody = template.HtmlBody, TextBody = template.TextBody, VariablesJson = JsonSerializer.Serialize(template.Variables) }; return View(vm); } // POST /admin/templates/save [HttpPost("save")] [ValidateAntiForgeryToken] public async Task Save(EmailTemplateEditViewModel model, CancellationToken ct) { if (!ModelState.IsValid) return View("Edit", model); List variables; try { variables = JsonSerializer.Deserialize>(model.VariablesJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? []; } catch (JsonException) { ModelState.AddModelError(nameof(model.VariablesJson), "Invalid JSON format for variables."); return View("Edit", model); } if (model.IsNew) { var command = new CreateEmailTemplateCommand( model.ServiceName, model.Key, model.LanguageCode, model.Subject, model.HtmlBody, model.TextBody, variables); var result = await _mediator.Send(command, ct); if (!result.IsSuccess) { ModelState.AddModelError("", result.Error?.Message ?? "Failed to create template."); return View("Edit", model); } } else { var command = new UpdateEmailTemplateCommand( model.ServiceName, model.Key, model.LanguageCode, model.Subject, model.HtmlBody, model.TextBody, variables); var result = await _mediator.Send(command, ct); if (!result.IsSuccess) { ModelState.AddModelError("", result.Error?.Message ?? "Failed to update template."); return View("Edit", model); } } return RedirectToAction(nameof(Index)); } // 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) { await _mediator.Send(new DeleteEmailTemplateCommand(serviceName, key, languageCode), ct); return RedirectToAction(nameof(Index)); } }