Skip to content

Commit f7b54cd

Browse files
committed
Pager default list size, error handling
1 parent d8a70f3 commit f7b54cd

File tree

6 files changed

+28
-19
lines changed

6 files changed

+28
-19
lines changed

src/App/Controllers/BlogController.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public BlogController(IDataService db, IFeedService ss, SignInManager<AppUser> s
3131

3232
public async Task<IActionResult> Index(int page = 1, string term = "")
3333
{
34-
var pager = new Pager(page);
34+
var blog = await _db.CustomFields.GetBlogSettings();
35+
var pager = new Pager(page, blog.ItemsPerPage);
3536
IEnumerable<PostItem> posts;
3637

3738
if (string.IsNullOrEmpty(term))
@@ -46,8 +47,6 @@ public async Task<IActionResult> Index(int page = 1, string term = "")
4647
if (pager.ShowOlder) pager.LinkToOlder = $"blog?page={pager.Older}";
4748
if (pager.ShowNewer) pager.LinkToNewer = $"blog?page={pager.Newer}";
4849

49-
var blog = await _db.CustomFields.GetBlogSettings();
50-
5150
var model = new ListModel {
5251
Blog = blog,
5352
PostListType = PostListType.Blog,
@@ -92,9 +91,10 @@ public async Task<IActionResult> Single(string slug)
9291
[Route("authors/{name}")]
9392
public async Task<IActionResult> Authors(string name, int page = 1)
9493
{
94+
var blog = await _db.CustomFields.GetBlogSettings();
9595
var author = await _db.Authors.GetItem(a => a.AppUserName == name);
9696

97-
var pager = new Pager(page);
97+
var pager = new Pager(page, blog.ItemsPerPage);
9898
var posts = await _db.BlogPosts.GetList(p => p.Published > DateTime.MinValue && p.AuthorId == author.Id, pager);
9999

100100
if (pager.ShowOlder) pager.LinkToOlder = $"authors/{name}?page={pager.Older}";
@@ -107,7 +107,7 @@ public async Task<IActionResult> Authors(string name, int page = 1)
107107
Pager = pager
108108
};
109109

110-
model.Blog = await _db.CustomFields.GetBlogSettings();
110+
model.Blog = blog;
111111
model.Blog.Cover = $"{Url.Content("~/")}{model.Blog.Cover}";
112112
model.Blog.Description = "";
113113

@@ -117,7 +117,8 @@ public async Task<IActionResult> Authors(string name, int page = 1)
117117
[Route("categories/{name}")]
118118
public async Task<IActionResult> Categories(string name, int page = 1)
119119
{
120-
var pager = new Pager(page);
120+
var blog = await _db.CustomFields.GetBlogSettings();
121+
var pager = new Pager(page, blog.ItemsPerPage);
121122
var posts = await _db.BlogPosts.GetListByCategory(name, pager);
122123

123124
if (pager.ShowOlder) pager.LinkToOlder = $"categories/{name}?page={pager.Older}";
@@ -129,7 +130,7 @@ public async Task<IActionResult> Categories(string name, int page = 1)
129130
Pager = pager
130131
};
131132

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

135136
ViewBag.Category = name;

src/App/Pages/Admin/Posts/Index.cshtml.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ public IndexModel(IDataService db, INotificationService ns)
2828

2929
public async Task<IActionResult> OnGetAsync(int pg = 1, string status = "A")
3030
{
31+
Blog = await _db.CustomFields.GetBlogSettings();
3132
var author = await _db.Authors.GetItem(a => a.AppUserName == User.Identity.Name);
3233
IsAdmin = author.IsAdmin;
3334

3435
Expression<Func<BlogPost, bool>> predicate = p => p.Id > 0;
35-
Pager = new Pager(pg);
36+
Pager = new Pager(pg, Blog.ItemsPerPage);
3637

3738
if (IsAdmin)
3839
{
@@ -53,20 +54,21 @@ public async Task<IActionResult> OnGetAsync(int pg = 1, string status = "A")
5354

5455
Posts = await _db.BlogPosts.GetList(predicate, Pager);
5556
Notifications = await _ns.GetNotifications(author.Id);
56-
Blog = await _db.CustomFields.GetBlogSettings();
5757

5858
return Page();
5959
}
6060

6161
public async Task<IActionResult> OnPostAsync()
6262
{
63+
Blog = await _db.CustomFields.GetBlogSettings();
6364
var author = await _db.Authors.GetItem(a => a.AppUserName == User.Identity.Name);
6465
IsAdmin = author.IsAdmin;
6566

6667
var page = int.Parse(Request.Form["page"]);
6768
var term = Request.Form["search"];
6869

69-
Pager = new Pager(page);
70+
71+
Pager = new Pager(page, Blog.ItemsPerPage);
7072

7173
if(IsAdmin)
7274
Posts = await _db.BlogPosts.Search(Pager, term);

src/App/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ public static void Main(string[] args)
4545

4646
if (!context.BlogPosts.Any())
4747
{
48-
services.GetRequiredService<IStorageService>().Reset();
48+
try
49+
{
50+
services.GetRequiredService<IStorageService>().Reset();
51+
}
52+
catch { }
53+
4954
AppData.Seed(context);
5055
}
5156
}

src/App/app.db

0 Bytes
Binary file not shown.

src/Core/Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5-
<Version>2.0.2.7</Version>
5+
<Version>2.0.2.8</Version>
66
</PropertyGroup>
77

88
<ItemGroup>

src/Core/Data/Repositories/CustomFieldRepository.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ public CustomFieldRepository(AppDbContext db) : base(db)
2121
public Task<BlogItem> GetBlogSettings()
2222
{
2323
var blog = new BlogItem();
24+
CustomField title, desc, items, cover, logo, theme;
2425

25-
var title = _db.CustomFields.Single(f => f.AuthorId == 0 && f.Name == Constants.BlogTitle);
26-
var desc = _db.CustomFields.Single(f => f.AuthorId == 0 && f.Name == Constants.BlogDescription);
27-
var items = _db.CustomFields.Single(f => f.AuthorId == 0 && f.Name == Constants.BlogItemsPerPage);
28-
var cover = _db.CustomFields.Single(f => f.AuthorId == 0 && f.Name == Constants.BlogCover);
29-
var logo = _db.CustomFields.Single(f => f.AuthorId == 0 && f.Name == Constants.BlogLogo);
30-
var theme = _db.CustomFields.Single(f => f.AuthorId == 0 && f.Name == Constants.BlogTheme);
26+
title = _db.CustomFields.Where(f => f.AuthorId == 0 && f.Name == Constants.BlogTitle).FirstOrDefault();
27+
desc = _db.CustomFields.Where(f => f.AuthorId == 0 && f.Name == Constants.BlogDescription).FirstOrDefault();
28+
items = _db.CustomFields.Where(f => f.AuthorId == 0 && f.Name == Constants.BlogItemsPerPage).FirstOrDefault();
29+
cover = _db.CustomFields.Where(f => f.AuthorId == 0 && f.Name == Constants.BlogCover).FirstOrDefault();
30+
logo = _db.CustomFields.Where(f => f.AuthorId == 0 && f.Name == Constants.BlogLogo).FirstOrDefault();
31+
theme = _db.CustomFields.Where(f => f.AuthorId == 0 && f.Name == Constants.BlogTheme).FirstOrDefault();
3132

3233
blog.Title = title == null ? "Blog Title" : title.Content;
3334
blog.Description = desc == null ? "Short blog description" : desc.Content;
3435
blog.ItemsPerPage = items == null ? 10 : int.Parse(items.Content);
3536
blog.Cover = cover == null ? "lib/img/cover.png" : cover.Content;
36-
blog.Logo = logo == null ? "lib/img/logo.png" : logo.Content;
37+
blog.Logo = logo == null ? "lib/img/logo-white.png" : logo.Content;
3738
blog.Theme = theme == null ? "Standard" : theme.Content;
3839

3940
return Task.FromResult(blog);

0 commit comments

Comments
 (0)