Skip to content

Commit 1e0747c

Browse files
committed
BlogItem and Model.Blog to take over editable app settings
1 parent 732795c commit 1e0747c

File tree

30 files changed

+239
-159
lines changed

30 files changed

+239
-159
lines changed

docs/Themes.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ Here is another super simple example using Bootstrap stylesheet.
5151
<!DOCTYPE html>
5252
<html>
5353
<head>
54-
<title>@AppSettings.Title</title>
54+
<title>@Model.Blog.Title</title>
5555
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
5656
</head>
5757
<body>
5858
<div class="container">
5959
<div class="jumbotron">
60-
<h1>@AppSettings.Title</h1>
61-
<p>@AppSettings.Description</p>
60+
<h1>@Model.Blog.Title</h1>
61+
<p>@Model.Blog.Description</p>
6262
</div>
6363
<div class="row">
6464
@if (Model.Posts != null)
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
using Core;
1+
using Core.Data;
2+
using Core.Services;
23
using Microsoft.AspNetCore.Mvc;
4+
using System.Threading.Tasks;
35

46
namespace Common.Modules.Archive
57
{
68
public class ArchiveController : Controller
79
{
8-
public IActionResult Index()
10+
IDataService _db;
11+
12+
public ArchiveController(IDataService db)
913
{
10-
ViewBag.Logo = $"{Url.Content("~/")}{AppSettings.Logo}";
11-
ViewBag.Cover = $"{Url.Content("~/")}{AppSettings.Cover}";
12-
ViewBag.Title = AppSettings.Title;
13-
ViewBag.Description = AppSettings.Description;
14+
_db = db;
15+
}
16+
17+
public async Task<IActionResult> Index()
18+
{
19+
var model = new ListModel();
20+
21+
model.Blog = await _db.CustomFields.GetBlogSettings();
22+
model.Blog.Cover = $"{Url.Content("~/")}{model.Blog.Cover}";
1423

15-
return View("~/Views/Modules/Archive/Index.cshtml");
24+
return View("~/Views/Modules/Archive/Index.cshtml", model);
1625
}
1726
}
1827
}
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
@{
2-
Layout = $"~/Views/Themes/{Core.AppSettings.Theme}/_Layout.cshtml";
3-
}
4-
<div>Archive page.</div>
1+
@model Core.Data.ListModel
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<partial name="~/Views/Themes/Standard/_Shared/_Head.cshtml" />
6+
</head>
7+
<body class="@ViewData["bodyClass"]">
8+
<partial name="~/Views/Themes/Standard/_Shared/_Header.cshtml" />
9+
10+
<div class="page-cover" style="background-image: url('@Model.Blog.Cover')">
11+
<h1 class="page-cover-title">
12+
<a href="~/">@Model.Blog.Title</a>
13+
</h1>
14+
<p class="page-cover-desc">@Model.Blog.Description</p>
15+
</div>
16+
17+
<div class="page-content">
18+
<div class="container" style="padding: 40px">
19+
<h1>Archive page</h1>
20+
</div>
21+
</div>
22+
<partial name="~/Views/Themes/Standard/_Shared/_Footer.cshtml" />
23+
</body>
24+
</html>

plugins/Common/Views/Themes/Simple/List.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
<div class="row">
1414
<div class="col-md-8">
1515
<h1 class="my-4">
16-
@Core.AppSettings.Title
17-
<small>@Core.AppSettings.Description</small>
16+
@Model.Blog.Title
17+
<small>@Model.Blog.Description</small>
1818
</h1>
1919
@if (Model != null && Model.Posts != null && Model.Posts.Any())
2020
{
2121
foreach (var post in Model.Posts)
2222
{
23-
var img = string.IsNullOrEmpty(post.Cover) ? Core.AppSettings.DefaultCover : post.Cover;
23+
var img = string.IsNullOrEmpty(post.Cover) ? Model.Blog.Cover : post.Cover;
2424
img = $"{Url.Content("~/")}{img}";
2525
<div class="card mb-4">
2626
<img class="card-img-top" src="@img" alt="Card image cap">

plugins/Common/Views/Themes/Simple/Post.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
<div class="row">
1616
<div class="col-md-8">
1717
<h1 class="my-4">
18-
@Core.AppSettings.Title
19-
<small>@Core.AppSettings.Description</small>
18+
@Model.Blog.Title
19+
<small>@Model.Blog.Description</small>
2020
</h1>
2121
<div class="card mb-4">
22-
<img class="card-img-top" src="@ViewBag.Cover" alt="Card image cap">
22+
<img class="card-img-top" src="@Model.Blog.Cover" alt="Card image cap">
2323
<div class="card-body post">
2424
<h2 class="card-title">@Model.Post.Title</h2>
2525
@Html.Raw(Model.Post.Content)

plugins/Common/Views/Themes/Simple/Shared/_Footer.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<footer class="py-5 bg-dark">
22
<div class="container">
3-
<p class="m-0 text-center text-white">Copyright &copy; @Core.AppSettings.Title @DateTime.Now.Year</p>
3+
<p class="m-0 text-center text-white">Copyright &copy; @Model.Blog.Title @DateTime.Now.Year</p>
44
</div>
55
</footer>
66
<script src="~/themes/simple/vendor/jquery/jquery.min.js"></script>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<meta charset="utf-8">
22
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
3-
<meta name="description" content="@Html.Raw(ViewBag.Description)">
3+
<meta name="description" content="@Html.Raw(Model.Blog.Description)">
44
<link href="~/themes/simple/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
55
<link href="~/themes/simple/css/blog-home.css" rel="stylesheet">

plugins/Common/Views/Themes/Simple/Shared/_Navbar.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
22
<div class="container">
33
<a class="navbar-brand" href="~/">
4-
<img src="~/@Core.AppSettings.Logo" alt="@Core.AppSettings.Title" class="my-auto" style="width: 32px" />
4+
<img src="~/@Model.Blog.Logo" alt="@Model.Blog.Title" class="my-auto" style="width: 32px" />
55
</a>
66
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
77
<span class="navbar-toggler-icon"></span>

src/App/Controllers/AdminController.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,21 @@ public async Task<AssetItem> Pick(string type, string asset, string post)
129129
}
130130
else if (type == "appCover")
131131
{
132-
_app.Update(opt => { opt.Cover = asset; });
133-
AppSettings.Cover = asset;
132+
var cover = _db.CustomFields.Single(f => f.AuthorId == 0 && f.Name == Constants.BlogCover);
133+
if (cover == null)
134+
_db.CustomFields.Add(new CustomField { AuthorId = 0, Name = Constants.BlogCover, Content = asset });
135+
else
136+
cover.Content = asset;
137+
_db.Complete();
134138
}
135139
else if (type == "appLogo")
136140
{
137-
_app.Update(opt => { opt.Logo = asset; });
138-
AppSettings.Logo = asset;
141+
var logo = _db.CustomFields.Single(f => f.AuthorId == 0 && f.Name == Constants.BlogLogo);
142+
if (logo == null)
143+
_db.CustomFields.Add(new CustomField { AuthorId = 0, Name = Constants.BlogLogo, Content = asset });
144+
else
145+
logo.Content = asset;
146+
_db.Complete();
139147
}
140148
else if (type == "avatar")
141149
{

src/App/Controllers/BlogController.cs

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Core;
2-
using Core.Data;
1+
using Core.Data;
32
using Core.Helpers;
43
using Core.Services;
54
using Markdig;
@@ -47,22 +46,23 @@ public async Task<IActionResult> Index(int page = 1, string term = "")
4746
if (pager.ShowOlder) pager.LinkToOlder = $"blog?page={pager.Older}";
4847
if (pager.ShowNewer) pager.LinkToNewer = $"blog?page={pager.Newer}";
4948

49+
var blog = await _db.CustomFields.GetBlogSettings();
50+
5051
var model = new ListModel {
52+
Blog = blog,
5153
PostListType = PostListType.Blog,
5254
Posts = posts,
5355
Pager = pager
5456
};
5557

56-
SetViewBag();
57-
5858
if (!string.IsNullOrEmpty(term))
5959
{
60-
ViewBag.Title = term;
61-
ViewBag.Description = "";
60+
model.Blog.Title = term;
61+
model.Blog.Description = "";
6262
model.PostListType = PostListType.Search;
6363
}
6464

65-
return View(string.Format(_listView, AppSettings.Theme), model);
65+
return View(string.Format(_listView, blog.Theme), model);
6666
}
6767

6868
[Route("posts/{slug}")]
@@ -73,14 +73,14 @@ public async Task<IActionResult> Single(string slug)
7373
var model = await _db.BlogPosts.GetModel(slug);
7474
model.Post.Content = Markdown.ToHtml(model.Post.Content);
7575

76-
ViewBag.Logo = $"{Url.Content("~/")}{AppSettings.Logo}";
77-
ViewBag.Cover = string.IsNullOrEmpty(model.Post.Cover) ?
78-
$"{Url.Content("~/")}{AppSettings.DefaultCover}" :
76+
model.Blog = await _db.CustomFields.GetBlogSettings();
77+
78+
model.Blog.Cover = string.IsNullOrEmpty(model.Post.Cover) ?
79+
$"{Url.Content("~/")}{model.Blog.Cover}" :
7980
$"{Url.Content("~/")}{model.Post.Cover}";
80-
ViewBag.Title = model.Post.Title;
81-
ViewBag.Description = model.Post.Description;
81+
model.Blog.Title = model.Post.Title;
8282

83-
return View($"~/Views/Themes/{AppSettings.Theme}/Post.cshtml", model);
83+
return View($"~/Views/Themes/{model.Blog.Theme}/Post.cshtml", model);
8484
}
8585
catch
8686
{
@@ -107,11 +107,11 @@ public async Task<IActionResult> Authors(string name, int page = 1)
107107
Pager = pager
108108
};
109109

110-
SetViewBag();
111-
112-
ViewBag.Description = "";
110+
model.Blog = await _db.CustomFields.GetBlogSettings();
111+
model.Blog.Cover = $"{Url.Content("~/")}{model.Blog.Cover}";
112+
model.Blog.Description = "";
113113

114-
return View(string.Format(_listView, AppSettings.Theme), model);
114+
return View(string.Format(_listView, model.Blog.Theme), model);
115115
}
116116

117117
[Route("categories/{name}")]
@@ -129,12 +129,13 @@ public async Task<IActionResult> Categories(string name, int page = 1)
129129
Pager = pager
130130
};
131131

132-
SetViewBag();
132+
model.Blog = await _db.CustomFields.GetBlogSettings();
133+
model.Blog.Cover = $"{Url.Content("~/")}{model.Blog.Cover}";
133134

134135
ViewBag.Category = name;
135-
ViewBag.Description = "";
136+
model.Blog.Description = "";
136137

137-
return View(string.Format(_listView, AppSettings.Theme), model);
138+
return View(string.Format(_listView, model.Blog.Theme), model);
138139
}
139140

140141
[Route("feed/{type}")]
@@ -162,20 +163,23 @@ public async Task Rss(string type)
162163
}
163164

164165
[Route("error/{code:int}")]
165-
public IActionResult Error(int code)
166+
public async Task<IActionResult> Error(int code)
166167
{
167-
SetViewBag();
168+
var model = new PostModel();
168169

169-
var viewName = $"~/Views/Themes/{AppSettings.Theme}/Error.cshtml";
170+
model.Blog = await _db.CustomFields.GetBlogSettings();
171+
model.Blog.Cover = $"{Url.Content("~/")}{model.Blog.Cover}";
172+
173+
var viewName = $"~/Views/Themes/{model.Blog.Theme}/Error.cshtml";
170174
var result = _viewEngine.GetView("", viewName, false);
171175

172176
if (result.Success)
173177
{
174-
return View(viewName, code);
178+
return View(viewName, model);
175179
}
176180
else
177181
{
178-
return View("~/Views/Shared/_Error.cshtml", code);
182+
return View("~/Views/Shared/_Error.cshtml", model);
179183
}
180184
}
181185

@@ -185,13 +189,5 @@ public async Task<IActionResult> Logout()
185189
await _sm.SignOutAsync();
186190
return Redirect("~/");
187191
}
188-
189-
void SetViewBag()
190-
{
191-
ViewBag.Logo = $"{Url.Content("~/")}{AppSettings.Logo}";
192-
ViewBag.Cover = $"{Url.Content("~/")}{AppSettings.Cover}";
193-
ViewBag.Title = AppSettings.Title;
194-
ViewBag.Description = AppSettings.Description;
195-
}
196192
}
197193
}

0 commit comments

Comments
 (0)