Various blog improvements
This commit is contained in:
parent
929adac910
commit
8e79aed647
8 changed files with 37 additions and 44 deletions
|
@ -7,32 +7,29 @@ using Website.Data;
|
|||
using Website.Models;
|
||||
using Website.ViewModels;
|
||||
|
||||
namespace Website.Controllers
|
||||
{
|
||||
public class BlogController : Controller
|
||||
{
|
||||
namespace Website.Controllers {
|
||||
public class BlogController:Controller {
|
||||
private const int MaxPostsPerPage = 10;
|
||||
private readonly BlogRepository _repo;
|
||||
|
||||
public BlogController(BlogRepository repo)
|
||||
{
|
||||
_repo = repo;
|
||||
}
|
||||
public BlogController(BlogRepository repo) => _repo = repo;
|
||||
|
||||
public async Task<IActionResult> Page(int page)
|
||||
{
|
||||
public async Task<IActionResult> Page(int page) {
|
||||
var offset = (page - 1) * MaxPostsPerPage;
|
||||
var posts = (await _repo.GetLatestPostsAsync(MaxPostsPerPage, offset)).Select(p => new BlogPostPreviewViewModel(p));
|
||||
var posts = (await _repo.GetLatestPostsAsync(MaxPostsPerPage, offset)).Select(p => new BlogPostSnippetViewModel(p));
|
||||
var maxPages = (await _repo.GetCountAsync()) / MaxPostsPerPage;
|
||||
var model = new BlogViewModel(posts, page, maxPages);
|
||||
return View(model);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Entry(string url)
|
||||
{
|
||||
public async Task<IActionResult> Entry(string url, bool preview = false) {
|
||||
try {
|
||||
var post = await _repo.GetPostByUrlAsync(url);
|
||||
var model = new BlogPostViewModel(post);
|
||||
|
||||
if (!preview && string.IsNullOrEmpty(post.Content))
|
||||
return NotFound();
|
||||
|
||||
var model = new BlogPostViewModel(post, preview);
|
||||
return View(model);
|
||||
} catch (InvalidOperationException) {
|
||||
return NotFound();
|
||||
|
@ -52,8 +49,7 @@ namespace Website.Controllers
|
|||
Content = post.Draft
|
||||
};
|
||||
return View(model);
|
||||
}
|
||||
catch (InvalidOperationException) {
|
||||
} catch (InvalidOperationException) {
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
@ -68,13 +64,13 @@ namespace Website.Controllers
|
|||
|
||||
var savedPost = await _repo.SavePost(post);
|
||||
|
||||
return RedirectToAction(nameof(Edit), new{savedPost.Id});
|
||||
return RedirectToAction(nameof(Edit), new { savedPost.Id });
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<IActionResult> Manage() {
|
||||
var posts = await _repo.GetAllPostsAsync();
|
||||
var models = posts.OrderByDescending(post => post.Timestamp).Select(post => new BlogPostViewModel(post));
|
||||
var models = posts.OrderByDescending(post => post.Timestamp).Select(post => new BlogPostViewModel(post, false));
|
||||
return View(models);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue