Various blog improvements

This commit is contained in:
Robert Marshall 2020-01-03 17:56:51 +00:00
parent 929adac910
commit 8e79aed647
8 changed files with 37 additions and 44 deletions

View file

@ -14,7 +14,7 @@ namespace Website.Tests.VIewModels
Post_Content = new string('a', 1001) Post_Content = new string('a', 1001)
}; };
var post = new BlogPost(state); var post = new BlogPost(state);
var vm = new BlogPostPreviewViewModel(post); var vm = new BlogPostSnippetViewModel(post);
vm.Content.Length.Should().Be(1000); vm.Content.Length.Should().Be(1000);
} }
@ -24,7 +24,7 @@ namespace Website.Tests.VIewModels
Post_Content = new string('a', 900) Post_Content = new string('a', 900)
}; };
var post = new BlogPost(state); var post = new BlogPost(state);
var vm = new BlogPostPreviewViewModel(post); var vm = new BlogPostSnippetViewModel(post);
vm.Content.Length.Should().Be(900); vm.Content.Length.Should().Be(900);
} }
} }

View file

@ -15,7 +15,7 @@ namespace Website.Tests.VIewModels
Post_Content="# header" Post_Content="# header"
}; };
var post = new BlogPost(state); var post = new BlogPost(state);
var vm = new BlogPostViewModel(post); var vm = new BlogPostViewModel(post, false);
vm.ContentHtml.Should().Be("<h1>header</h1>"); vm.ContentHtml.Should().Be("<h1>header</h1>");
} }
@ -26,7 +26,7 @@ namespace Website.Tests.VIewModels
Post_Timestamp = new DateTime(2018, 10, 01, 15, 1, 25) Post_Timestamp = new DateTime(2018, 10, 01, 15, 1, 25)
}; };
var post = new BlogPost(state); var post = new BlogPost(state);
var vm = new BlogPostViewModel(post); var vm = new BlogPostViewModel(post, false);
vm.Timestamp.Should().Be("Monday the 1st of October 2018"); vm.Timestamp.Should().Be("Monday the 1st of October 2018");
} }
@ -37,7 +37,7 @@ namespace Website.Tests.VIewModels
Post_Timestamp = new DateTime(2018, 10, 02, 15, 1, 25) Post_Timestamp = new DateTime(2018, 10, 02, 15, 1, 25)
}; };
var post = new BlogPost(state); var post = new BlogPost(state);
var vm = new BlogPostViewModel(post); var vm = new BlogPostViewModel(post, false);
vm.Timestamp.Should().Be("Tuesday the 2nd of October 2018"); vm.Timestamp.Should().Be("Tuesday the 2nd of October 2018");
} }
@ -48,7 +48,7 @@ namespace Website.Tests.VIewModels
Post_Timestamp = new DateTime(2018, 10, 03, 15, 1, 25) Post_Timestamp = new DateTime(2018, 10, 03, 15, 1, 25)
}; };
var post = new BlogPost(state); var post = new BlogPost(state);
var vm = new BlogPostViewModel(post); var vm = new BlogPostViewModel(post, false);
vm.Timestamp.Should().Be("Wednesday the 3rd of October 2018"); vm.Timestamp.Should().Be("Wednesday the 3rd of October 2018");
} }
} }

View file

@ -7,32 +7,29 @@ using Website.Data;
using Website.Models; using Website.Models;
using Website.ViewModels; using Website.ViewModels;
namespace Website.Controllers namespace Website.Controllers {
{ public class BlogController:Controller {
public class BlogController : Controller
{
private const int MaxPostsPerPage = 10; private const int MaxPostsPerPage = 10;
private readonly BlogRepository _repo; private readonly BlogRepository _repo;
public BlogController(BlogRepository repo) public BlogController(BlogRepository repo) => _repo = repo;
{
_repo = repo;
}
public async Task<IActionResult> Page(int page) public async Task<IActionResult> Page(int page) {
{
var offset = (page - 1) * MaxPostsPerPage; 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 maxPages = (await _repo.GetCountAsync()) / MaxPostsPerPage;
var model = new BlogViewModel(posts, page, maxPages); var model = new BlogViewModel(posts, page, maxPages);
return View(model); return View(model);
} }
public async Task<IActionResult> Entry(string url) public async Task<IActionResult> Entry(string url, bool preview = false) {
{
try { try {
var post = await _repo.GetPostByUrlAsync(url); 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); return View(model);
} catch (InvalidOperationException) { } catch (InvalidOperationException) {
return NotFound(); return NotFound();
@ -52,8 +49,7 @@ namespace Website.Controllers
Content = post.Draft Content = post.Draft
}; };
return View(model); return View(model);
} } catch (InvalidOperationException) {
catch (InvalidOperationException) {
return NotFound(); return NotFound();
} }
} }
@ -74,7 +70,7 @@ namespace Website.Controllers
[Authorize] [Authorize]
public async Task<IActionResult> Manage() { public async Task<IActionResult> Manage() {
var posts = await _repo.GetAllPostsAsync(); 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); return View(models);
} }

View file

@ -17,7 +17,7 @@ namespace Website.Controllers
public async Task<IActionResult> Index() public async Task<IActionResult> Index()
{ {
var post = await _blogRepo.GetLatestPostAsync(); var post = await _blogRepo.GetLatestPostAsync();
var model = new BlogPostPreviewViewModel(post); var model = new BlogPostSnippetViewModel(post);
return View(model); return View(model);
} }
} }

View file

@ -1,9 +0,0 @@
using Website.Models;
namespace Website.ViewModels
{
public class BlogPostPreviewViewModel:BlogPostViewModel
{
public BlogPostPreviewViewModel(BlogPost blogPost) : base(blogPost) => Content = Content.Length < 1000 ? Content : Content.Substring(0, 1000);
}
}

View file

@ -0,0 +1,9 @@
using Website.Models;
namespace Website.ViewModels
{
public class BlogPostSnippetViewModel:BlogPostViewModel
{
public BlogPostSnippetViewModel(BlogPost blogPost) : base(blogPost, false) => Content = Content.Length < 1000 ? Content : Content.Substring(0, 1000);
}
}

View file

@ -7,12 +7,12 @@ namespace Website.ViewModels
{ {
public class BlogPostViewModel public class BlogPostViewModel
{ {
public BlogPostViewModel(BlogPost blogPost) { public BlogPostViewModel(BlogPost blogPost, bool preview) {
Id = blogPost.Id; Id = blogPost.Id;
Title = blogPost.Title; Title = blogPost.Title;
Timestamp = FormatTimestamp(blogPost.Timestamp); Timestamp = FormatTimestamp(blogPost.Timestamp);
Url = blogPost.Url; Url = blogPost.Url;
Content = blogPost.Content; Content = preview ? blogPost.Draft : blogPost.Content;
} }
private static string GetDaySuffix(int day) { private static string GetDaySuffix(int day) {

View file

@ -5,16 +5,13 @@
} }
<table> <table>
<thead><tr>
<td>Title</td>
<td colspan="3">Action</td>
</tr></thead>
@foreach (var post in Model) { @foreach (var post in Model) {
<tr> <tr>
<td>@post.Title</td> <td><a href="/blog/view/@post.Url">@post.Title</a></td>
<td><a href="edit?id=@post.Id">Edit</a></td> <td><a href="/blog/view/@post.Url?preview=true"><img src="/images/blog.svg" alt="Preview" title="Preview"/></a></td>
<td><a href="publish?id=@post.Id">Publish</a></td> <td><a href="edit?id=@post.Id"><img src="/images/edit.svg" alt="Edit" title="Edit"/></a></td>
<td><a href="delete?id=@post.Id">Delete</a></td> <td><a href="publish?id=@post.Id"><img src="/images/send.svg" alt="Publish" title="Publish" onclick="return confirm('Are you sure?')"/></a></td>
<td><a href="delete?id=@post.Id"><img src="/images/delete.svg" alt="Delete" title="Delete" onclick="return confirm('Are you sure?')"/></a></td>
</tr> </tr>
} }
</table> </table>