-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminContentController.cs
More file actions
211 lines (194 loc) · 6.69 KB
/
Copy pathAdminContentController.cs
File metadata and controls
211 lines (194 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using MyWebApp.Data;
using System.Collections.Generic;
using MyWebApp.Filters;
using MyWebApp.Models;
using MyWebApp.Services;
using Microsoft.AspNetCore.Http;
using System.Linq;
namespace MyWebApp.Controllers;
[RoleAuthorize("Admin")]
public class AdminContentController : Controller
{
private readonly ApplicationDbContext _db;
private readonly LayoutService _layout;
private readonly ContentProcessingService _content;
private readonly TokenRenderService _tokens;
public AdminContentController(ApplicationDbContext db, LayoutService layout, ContentProcessingService content, TokenRenderService tokens)
{
_db = db;
_layout = layout;
_content = content;
_tokens = tokens;
}
private async Task LoadTemplatesAsync()
{
ViewBag.Templates = await _db.BlockTemplates.AsNoTracking()
.OrderBy(t => t.Name).ToListAsync();
ViewBag.Permissions = await _db.Permissions.AsNoTracking()
.OrderBy(p => p.Name).ToListAsync();
}
public async Task<IActionResult> Index()
{
var pages = await _db.Pages.AsNoTracking().OrderBy(p => p.Slug).ToListAsync();
return View(pages);
}
public async Task<IActionResult> Create()
{
await LoadTemplatesAsync();
ViewBag.Sections = new List<PageSection>();
return View("PageEditor", new Page());
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Page model)
{
if (!ModelState.IsValid)
{
await LoadTemplatesAsync();
ViewBag.Sections = model.Sections;
return View("PageEditor", model);
}
if (model.IsPublished && model.PublishDate == null)
{
model.PublishDate = DateTime.UtcNow;
}
var sections = model.Sections?.ToList() ?? new List<PageSection>();
if (sections.Any(s => !LayoutService.IsValidZone(model.Layout, s.Zone)))
{
ModelState.AddModelError(string.Empty, "Invalid area for selected layout.");
}
if (!sections.Any(s => s.Zone == "main"))
{
ModelState.AddModelError(string.Empty, "Main area cannot be empty.");
}
if (!ModelState.IsValid)
{
await LoadTemplatesAsync();
ViewBag.Sections = sections;
model.Sections = sections;
return View("PageEditor", model);
}
model.Sections = new List<PageSection>();
_db.Pages.Add(model);
await _db.SaveChangesAsync();
if (sections.Count > 0)
{
var files = HttpContext.Request.Form.Files;
for (int i = 0; i < sections.Count; i++)
{
var s = sections[i];
s.Id = 0;
s.PageId = model.Id;
var file = files.FirstOrDefault(f => f.Name == $"Sections[{i}].File");
await _content.PrepareHtmlAsync(s, file);
_db.PageSections.Add(s);
}
await _db.SaveChangesAsync();
}
_layout.Reset();
return RedirectToAction(nameof(Index));
}
[HttpPost]
public async Task<IActionResult> Preview([FromBody] PreviewRequest model)
{
var zones = new Dictionary<string, string>();
foreach (var kv in model.Zones ?? new Dictionary<string, string>())
{
zones[kv.Key] = await _tokens.RenderAsync(_db, kv.Value ?? string.Empty);
}
ViewBag.HeaderHtml = await _layout.GetHeaderAsync(_db);
ViewBag.FooterHtml = await _layout.GetFooterAsync(_db);
ViewBag.PageLayout = string.IsNullOrWhiteSpace(model.Layout) ? "single-column" : model.Layout;
ViewBag.ZoneHtml = zones;
return View("~/Views/Pages/Show.cshtml", new Page { Title = model.Title });
}
public async Task<IActionResult> Edit(int id)
{
var page = await _db.Pages.FindAsync(id);
if (page == null)
{
return NotFound();
}
await LoadTemplatesAsync();
ViewBag.Sections = await _db.PageSections.Where(s => s.PageId == id)
.OrderBy(s => s.SortOrder).ToListAsync();
return View("PageEditor", page);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Page model)
{
if (!ModelState.IsValid)
{
await LoadTemplatesAsync();
ViewBag.Sections = model.Sections;
return View("PageEditor", model);
}
if (model.IsPublished && model.PublishDate == null)
{
model.PublishDate = DateTime.UtcNow;
}
var sections = model.Sections?.ToList() ?? new List<PageSection>();
if (sections.Any(s => !LayoutService.IsValidZone(model.Layout, s.Zone)))
{
ModelState.AddModelError(string.Empty, "Invalid area for selected layout.");
}
if (!sections.Any(s => s.Zone == "main"))
{
ModelState.AddModelError(string.Empty, "Main area cannot be empty.");
}
if (!ModelState.IsValid)
{
await LoadTemplatesAsync();
ViewBag.Sections = sections;
model.Sections = sections;
return View("PageEditor", model);
}
model.Sections = new List<PageSection>();
_db.Update(model);
await _db.SaveChangesAsync();
var existing = _db.PageSections.Where(s => s.PageId == model.Id);
_db.PageSections.RemoveRange(existing);
if (sections.Count > 0)
{
var files = HttpContext.Request.Form.Files;
for (int i = 0; i < sections.Count; i++)
{
var s = sections[i];
s.Id = 0;
s.PageId = model.Id;
var file = files.FirstOrDefault(f => f.Name == $"Sections[{i}].File");
await _content.PrepareHtmlAsync(s, file);
_db.PageSections.Add(s);
}
await _db.SaveChangesAsync();
}
_layout.Reset();
return RedirectToAction(nameof(Index));
}
public async Task<IActionResult> Delete(int id)
{
var page = await _db.Pages.FindAsync(id);
if (page == null)
{
return NotFound();
}
return View(page);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var page = await _db.Pages.FindAsync(id);
if (page != null)
{
_db.Pages.Remove(page);
await _db.SaveChangesAsync();
_layout.Reset();
}
return RedirectToAction(nameof(Index));
}
}