aboutsummaryrefslogtreecommitdiff
path: root/src/Enmarcha.Backend/Controllers/Backoffice/AlertsController.cs
diff options
context:
space:
mode:
authorAriel Costas Guerrero <ariel@costas.dev>2026-03-19 18:56:34 +0100
committerAriel Costas Guerrero <ariel@costas.dev>2026-03-19 18:56:34 +0100
commitbee85bf92aab84087798ffa9f3f16336acef2fce (patch)
tree4fc8e2907e6618940cd9bdeb3da1a81172aab459 /src/Enmarcha.Backend/Controllers/Backoffice/AlertsController.cs
parentfed5d57b9e5d3df7c34bccb7a120bfa274b2039a (diff)
Basic backoffice for alert management
Diffstat (limited to 'src/Enmarcha.Backend/Controllers/Backoffice/AlertsController.cs')
-rw-r--r--src/Enmarcha.Backend/Controllers/Backoffice/AlertsController.cs83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/Enmarcha.Backend/Controllers/Backoffice/AlertsController.cs b/src/Enmarcha.Backend/Controllers/Backoffice/AlertsController.cs
new file mode 100644
index 0000000..4e83abc
--- /dev/null
+++ b/src/Enmarcha.Backend/Controllers/Backoffice/AlertsController.cs
@@ -0,0 +1,83 @@
+using Enmarcha.Backend.Data;
+using Enmarcha.Backend.ViewModels;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.EntityFrameworkCore;
+
+namespace Enmarcha.Backend.Controllers.Backoffice;
+
+[Route("backoffice/alerts")]
+[Authorize(AuthenticationSchemes = "Backoffice")]
+public class AlertsController(AppDbContext db) : Controller
+{
+ [HttpGet("")]
+ public async Task<IActionResult> Index()
+ {
+ var alerts = await db.ServiceAlerts
+ .OrderByDescending(a => a.InsertedDate)
+ .ToListAsync();
+ return View(alerts);
+ }
+
+ [HttpGet("create")]
+ public IActionResult Create() => View("Edit", new AlertFormViewModel());
+
+ [HttpPost("create")]
+ [ValidateAntiForgeryToken]
+ public async Task<IActionResult> CreatePost(AlertFormViewModel model)
+ {
+ if (!ModelState.IsValid)
+ {
+ return View("Edit", model);
+ }
+
+ db.ServiceAlerts.Add(model.ToServiceAlert());
+ await db.SaveChangesAsync();
+ return RedirectToAction(nameof(Index));
+ }
+
+ [HttpGet("{id}/edit")]
+ public async Task<IActionResult> Edit(string id)
+ {
+ var alert = await db.ServiceAlerts.FindAsync(id);
+ if (alert is null) return NotFound();
+ return View(AlertFormViewModel.FromServiceAlert(alert));
+ }
+
+ [HttpPost("{id}/edit")]
+ [ValidateAntiForgeryToken]
+ public async Task<IActionResult> EditPost(string id, AlertFormViewModel model)
+ {
+ if (!ModelState.IsValid)
+ {
+ return View("Edit", model);
+ }
+
+ var alert = await db.ServiceAlerts.FindAsync(id);
+ if (alert is null) return NotFound();
+
+ model.ApplyTo(alert);
+ await db.SaveChangesAsync();
+ return RedirectToAction(nameof(Index));
+ }
+
+ [HttpGet("{id}/delete")]
+ public async Task<IActionResult> Delete(string id)
+ {
+ var alert = await db.ServiceAlerts.FindAsync(id);
+ if (alert is null) return NotFound();
+ return View(alert);
+ }
+
+ [HttpPost("{id}/delete")]
+ [ValidateAntiForgeryToken]
+ public async Task<IActionResult> DeleteConfirm(string id)
+ {
+ var alert = await db.ServiceAlerts.FindAsync(id);
+ if (alert is null) return NotFound();
+
+ db.ServiceAlerts.Remove(alert);
+ await db.SaveChangesAsync();
+ return RedirectToAction(nameof(Index));
+ }
+}