From a3a0789a5d1c4276ff69cd64d952294bf596e183 Mon Sep 17 00:00:00 2001 From: Denis-RZ <77514212+Denis-RZ@users.noreply.github.com> Date: Wed, 18 Jun 2025 14:52:23 +0800 Subject: [PATCH] Integrate block assignment into edit --- .../AdminBlockTemplateController.cs | 103 ++++++++++++++---- .../Views/AdminBlockTemplate/AddToPage.cshtml | 26 ++++- .../Views/AdminBlockTemplate/Create.cshtml | 2 + .../Views/AdminBlockTemplate/Edit.cshtml | 2 + .../Views/AdminBlockTemplate/Index.cshtml | 3 +- .../AdminBlockTemplate/_PageAssignment.cshtml | 41 +++++++ .../Views/AdminPageSection/Edit.cshtml | 4 + 7 files changed, 152 insertions(+), 29 deletions(-) create mode 100644 website/MyWebApp/Views/AdminBlockTemplate/_PageAssignment.cshtml diff --git a/website/MyWebApp/Controllers/AdminBlockTemplateController.cs b/website/MyWebApp/Controllers/AdminBlockTemplateController.cs index 294481d..551618a 100644 --- a/website/MyWebApp/Controllers/AdminBlockTemplateController.cs +++ b/website/MyWebApp/Controllers/AdminBlockTemplateController.cs @@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Http; using System.Collections.Generic; +using System.Linq; using System.IO; using System.Text.Json; using MyWebApp.Data; @@ -35,20 +36,27 @@ public async Task Index() return View(items); } - public IActionResult Create() + public async Task Create() { + await LoadPagesAsync(); return View(new BlockTemplate()); } [HttpPost] [ValidateAntiForgeryToken] - public async Task Create(BlockTemplate model) + public async Task Create(BlockTemplate model, List? pageIds, string? zone, string? role) { - if (!ModelState.IsValid) return View(model); + if (!ModelState.IsValid) + { + await LoadPagesAsync(); + return View(model); + } model.Html = _sanitizer.Sanitize(model.Html); _db.BlockTemplates.Add(model); _db.BlockTemplateVersions.Add(new BlockTemplateVersion { Template = model, Html = model.Html }); await _db.SaveChangesAsync(); + await AddSectionsAsync(model, pageIds, zone, role); + await _db.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } @@ -56,18 +64,25 @@ public async Task Edit(int id) { var item = await _db.BlockTemplates.FindAsync(id); if (item == null) return NotFound(); + await LoadPagesAsync(); return View(item); } [HttpPost] [ValidateAntiForgeryToken] - public async Task Edit(BlockTemplate model) + public async Task Edit(BlockTemplate model, List? pageIds, string? zone, string? role) { - if (!ModelState.IsValid) return View(model); + if (!ModelState.IsValid) + { + await LoadPagesAsync(); + return View(model); + } model.Html = _sanitizer.Sanitize(model.Html); _db.Update(model); _db.BlockTemplateVersions.Add(new BlockTemplateVersion { BlockTemplateId = model.Id, Html = model.Html }); await _db.SaveChangesAsync(); + await AddSectionsAsync(model, pageIds, zone, role); + await _db.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } @@ -160,14 +175,19 @@ public async Task AddToPage(int id) [HttpPost] [ValidateAntiForgeryToken] - public async Task AddToPage(int id, int pageId, string zone, int? roleId) + public async Task AddToPage(int id, List pageIds, string zone, string role) { var template = await _db.BlockTemplates.FindAsync(id); - var page = await _db.Pages.FindAsync(pageId); - if (template == null || page == null) + if (template == null) return NotFound(); + if (pageIds == null || pageIds.Count == 0) { - return NotFound(); + await LoadPagesAsync(); + ViewBag.BlockId = id; + ModelState.AddModelError("pageIds", "Page selection required"); + return View(); } + var roleEntity = await _db.Roles.FirstOrDefaultAsync(r => r.Name == role); + int? roleId = roleEntity?.Id; zone = zone?.Trim() ?? string.Empty; if (string.IsNullOrEmpty(zone)) { @@ -176,23 +196,60 @@ public async Task AddToPage(int id, int pageId, string zone, int? ModelState.AddModelError("zone", "Zone required"); return View(); } - var sort = await _db.PageSections - .Where(s => s.PageId == pageId && s.Zone == zone) - .Select(s => s.SortOrder) - .DefaultIfEmpty(-1) - .MaxAsync() + 1; - var section = new PageSection + if (pageIds.Contains(0)) + { + pageIds = await _db.Pages.Select(p => p.Id).ToListAsync(); + } + foreach (var pageId in pageIds) { - PageId = pageId, - Zone = zone, - SortOrder = sort, - Html = template.Html, - Type = PageSectionType.Html, - RoleId = roleId - }; - _db.PageSections.Add(section); + var sort = await _db.PageSections + .Where(s => s.PageId == pageId && s.Zone == zone) + .Select(s => s.SortOrder) + .DefaultIfEmpty(-1) + .MaxAsync() + 1; + var section = new PageSection + { + PageId = pageId, + Zone = zone, + SortOrder = sort, + Html = template.Html, + Type = PageSectionType.Html, + RoleId = roleId + }; + _db.PageSections.Add(section); + } await _db.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } + private async Task AddSectionsAsync(BlockTemplate template, List? pageIds, string? zone, string? role) + { + if (pageIds == null || pageIds.Count == 0 || string.IsNullOrWhiteSpace(zone)) + return; + var roleEntity = await _db.Roles.FirstOrDefaultAsync(r => r.Name == role); + int? roleId = roleEntity?.Id; + zone = zone!.Trim(); + if (pageIds.Contains(0)) + { + pageIds = await _db.Pages.Select(p => p.Id).ToListAsync(); + } + foreach (var pageId in pageIds) + { + var sort = await _db.PageSections + .Where(s => s.PageId == pageId && s.Zone == zone) + .Select(s => s.SortOrder) + .DefaultIfEmpty(-1) + .MaxAsync() + 1; + var section = new PageSection + { + PageId = pageId, + Zone = zone, + SortOrder = sort, + Html = template.Html, + Type = PageSectionType.Html, + RoleId = roleId + }; + _db.PageSections.Add(section); + } + } } diff --git a/website/MyWebApp/Views/AdminBlockTemplate/AddToPage.cshtml b/website/MyWebApp/Views/AdminBlockTemplate/AddToPage.cshtml index c902368..6ea423b 100644 --- a/website/MyWebApp/Views/AdminBlockTemplate/AddToPage.cshtml +++ b/website/MyWebApp/Views/AdminBlockTemplate/AddToPage.cshtml @@ -7,8 +7,13 @@
- - +
+
+ + +
+ +@section Scripts { + +} diff --git a/website/MyWebApp/Views/AdminBlockTemplate/Create.cshtml b/website/MyWebApp/Views/AdminBlockTemplate/Create.cshtml index 756c339..a53d5d4 100644 --- a/website/MyWebApp/Views/AdminBlockTemplate/Create.cshtml +++ b/website/MyWebApp/Views/AdminBlockTemplate/Create.cshtml @@ -17,6 +17,8 @@ + + @await Html.PartialAsync("_PageAssignment") diff --git a/website/MyWebApp/Views/AdminBlockTemplate/Edit.cshtml b/website/MyWebApp/Views/AdminBlockTemplate/Edit.cshtml index f82cedb..9047c6f 100644 --- a/website/MyWebApp/Views/AdminBlockTemplate/Edit.cshtml +++ b/website/MyWebApp/Views/AdminBlockTemplate/Edit.cshtml @@ -18,6 +18,8 @@ + + @await Html.PartialAsync("_PageAssignment") diff --git a/website/MyWebApp/Views/AdminBlockTemplate/Index.cshtml b/website/MyWebApp/Views/AdminBlockTemplate/Index.cshtml index 5ad8c03..64f8481 100644 --- a/website/MyWebApp/Views/AdminBlockTemplate/Index.cshtml +++ b/website/MyWebApp/Views/AdminBlockTemplate/Index.cshtml @@ -10,7 +10,7 @@ - + @foreach (var t in Model) { @@ -18,7 +18,6 @@ - } diff --git a/website/MyWebApp/Views/AdminBlockTemplate/_PageAssignment.cshtml b/website/MyWebApp/Views/AdminBlockTemplate/_PageAssignment.cshtml new file mode 100644 index 0000000..33945a6 --- /dev/null +++ b/website/MyWebApp/Views/AdminBlockTemplate/_PageAssignment.cshtml @@ -0,0 +1,41 @@ +@using MyWebApp.Models +
+

Page Assignment

+
+ + + +
+
+ + +
+
+ + +
+
+@section Scripts { + +} diff --git a/website/MyWebApp/Views/AdminPageSection/Edit.cshtml b/website/MyWebApp/Views/AdminPageSection/Edit.cshtml index 05822c0..6f30388 100644 --- a/website/MyWebApp/Views/AdminPageSection/Edit.cshtml +++ b/website/MyWebApp/Views/AdminPageSection/Edit.cshtml @@ -9,6 +9,10 @@

Edit Section

+
+ + +
Name
Name
@t.Name Edit DeleteAdd to Page