using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; namespace MyWebLog.Features.Admin; /// /// Controller for admin-specific displays and routes /// [Route("/admin")] [Authorize] public class AdminController : MyWebLogController { /// public AdminController(WebLogDbContext db) : base(db) { } [HttpGet("")] public async Task Index() => View(new DashboardModel(WebLog) { Posts = await Db.Posts.CountByStatus(PostStatus.Published), Drafts = await Db.Posts.CountByStatus(PostStatus.Draft), Pages = await Db.Pages.CountAll(), ListedPages = await Db.Pages.CountListed(), Categories = await Db.Categories.CountAll(), TopLevelCategories = await Db.Categories.CountTopLevel() }); [HttpGet("settings")] public async Task Settings() => View(new SettingsModel(WebLog) { DefaultPages = Enumerable.Repeat(new SelectListItem($"- {Resources.FirstPageOfPosts} -", "posts"), 1) .Concat((await Db.Pages.FindAll()).Select(p => new SelectListItem(p.Title, p.Id))) }); [HttpPost("settings")] public async Task SaveSettings(SettingsModel model) { var details = await Db.WebLogDetails.GetByHost(WebLog.UrlBase); if (details is null) return NotFound(); model.PopulateSettings(details); await Db.SaveChangesAsync(); // Update cache WebLogCache.Set(WebLogCache.HostToDb(HttpContext), (await Db.WebLogDetails.FindByHost(WebLog.UrlBase))!); // TODO: confirmation message return RedirectToAction(nameof(Index)); } }