173 lines
6.3 KiB
C#
173 lines
6.3 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using PowderCoating.Core.Entities;
|
|
using PowderCoating.Infrastructure.Data;
|
|
using PowderCoating.Shared.Constants;
|
|
|
|
namespace PowderCoating.Web.Controllers;
|
|
|
|
/// <summary>
|
|
/// SuperAdmin-only CRUD interface for managing the rotating tip-of-the-day entries
|
|
/// displayed on the tenant dashboard. The system ships with 40 seed tips loaded by
|
|
/// <c>SeedDataService.SeedSystemDataAsync()</c>; this controller allows platform
|
|
/// operators to add, edit, deactivate, or remove tips without a code deployment.
|
|
/// The tip shown each day is selected by the dashboard controller using
|
|
/// <c>DayOfYear % activeCount</c> so it rotates predictably.
|
|
/// </summary>
|
|
[Authorize(Policy = AppConstants.Policies.SuperAdminOnly)]
|
|
public class DashboardTipsController : Controller
|
|
{
|
|
private readonly ApplicationDbContext _db;
|
|
|
|
public DashboardTipsController(ApplicationDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a paginated, optionally filtered list of all dashboard tips.
|
|
/// Active tips are sorted first (then by newest Id) to make the currently
|
|
/// live pool easy to review at a glance. ViewBag includes both the filtered
|
|
/// count and the global active/total counts for the header summary cards.
|
|
/// </summary>
|
|
// GET: /DashboardTips
|
|
public async Task<IActionResult> Index(string? search, bool? activeOnly, int page = 1)
|
|
{
|
|
const int pageSize = 25;
|
|
|
|
var query = _db.DashboardTips.AsQueryable();
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
query = query.Where(t => t.TipText.Contains(search));
|
|
|
|
if (activeOnly == true)
|
|
query = query.Where(t => t.IsActive);
|
|
|
|
var total = await query.CountAsync();
|
|
var tips = await query
|
|
.OrderByDescending(t => t.IsActive)
|
|
.ThenByDescending(t => t.Id)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
|
|
ViewBag.Search = search;
|
|
ViewBag.ActiveOnly = activeOnly ?? false;
|
|
ViewBag.Page = page;
|
|
ViewBag.PageSize = pageSize;
|
|
ViewBag.Total = total;
|
|
ViewBag.TotalPages = (int)Math.Ceiling(total / (double)pageSize);
|
|
ViewBag.ActiveCount = await _db.DashboardTips.CountAsync(t => t.IsActive);
|
|
ViewBag.TotalCount = await _db.DashboardTips.CountAsync();
|
|
|
|
return View(tips);
|
|
}
|
|
|
|
/// <summary>Returns the Create form with an empty <see cref="DashboardTip"/> model.</summary>
|
|
// GET: /DashboardTips/Create
|
|
public IActionResult Create() => View(new DashboardTip());
|
|
|
|
/// <summary>
|
|
/// Persists a new dashboard tip. Text is trimmed before saving to prevent
|
|
/// whitespace-only entries from appearing as blank tiles on the dashboard.
|
|
/// Model validation is done manually (rather than relying solely on
|
|
/// <c>[Required]</c> attributes) to ensure a meaningful error message is shown.
|
|
/// </summary>
|
|
// POST: /DashboardTips/Create
|
|
[HttpPost, ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Create(DashboardTip model)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(model.TipText))
|
|
{
|
|
ModelState.AddModelError(nameof(model.TipText), "Tip text is required.");
|
|
return View(model);
|
|
}
|
|
|
|
_db.DashboardTips.Add(new DashboardTip
|
|
{
|
|
TipText = model.TipText.Trim(),
|
|
IsActive = model.IsActive,
|
|
CreatedAt = DateTime.UtcNow
|
|
});
|
|
await _db.SaveChangesAsync();
|
|
|
|
TempData["Success"] = "Tip added successfully.";
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
/// <summary>Returns the Edit form for an existing tip, or 404 if not found.</summary>
|
|
// GET: /DashboardTips/Edit/5
|
|
public async Task<IActionResult> Edit(int id)
|
|
{
|
|
var tip = await _db.DashboardTips.FindAsync(id);
|
|
if (tip == null) return NotFound();
|
|
return View(tip);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates text and active flag for an existing tip. Returns the tracked entity
|
|
/// (not the posted model) to the view on validation failure so the form shows
|
|
/// the database version rather than potentially mangled posted data.
|
|
/// </summary>
|
|
// POST: /DashboardTips/Edit/5
|
|
[HttpPost, ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Edit(int id, DashboardTip model)
|
|
{
|
|
var tip = await _db.DashboardTips.FindAsync(id);
|
|
if (tip == null) return NotFound();
|
|
|
|
if (string.IsNullOrWhiteSpace(model.TipText))
|
|
{
|
|
ModelState.AddModelError(nameof(model.TipText), "Tip text is required.");
|
|
return View(tip);
|
|
}
|
|
|
|
tip.TipText = model.TipText.Trim();
|
|
tip.IsActive = model.IsActive;
|
|
await _db.SaveChangesAsync();
|
|
|
|
TempData["Success"] = "Tip updated.";
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Permanently (hard) deletes a dashboard tip. Tips are platform metadata so
|
|
/// they do not use soft delete — they have no foreign-key relationships to
|
|
/// tenant data and nothing references a deleted tip's Id. A missing Id is
|
|
/// silently ignored to keep the action idempotent.
|
|
/// </summary>
|
|
// POST: /DashboardTips/Delete/5
|
|
[HttpPost, ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Delete(int id)
|
|
{
|
|
var tip = await _db.DashboardTips.FindAsync(id);
|
|
if (tip != null)
|
|
{
|
|
_db.DashboardTips.Remove(tip);
|
|
await _db.SaveChangesAsync();
|
|
TempData["Success"] = "Tip deleted.";
|
|
}
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Flips the <c>IsActive</c> flag on a tip without a full edit round-trip.
|
|
/// This lets operators quickly remove a tip from the rotation (deactivate)
|
|
/// without deleting it, preserving the ability to reactivate it later.
|
|
/// A missing Id is silently ignored to keep the action idempotent.
|
|
/// </summary>
|
|
// POST: /DashboardTips/ToggleActive/5
|
|
[HttpPost, ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> ToggleActive(int id)
|
|
{
|
|
var tip = await _db.DashboardTips.FindAsync(id);
|
|
if (tip != null)
|
|
{
|
|
tip.IsActive = !tip.IsActive;
|
|
await _db.SaveChangesAsync();
|
|
}
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
}
|