Use new API for blog
This commit is contained in:
parent
e389b2404a
commit
25c320bf6b
17 changed files with 356 additions and 314 deletions
|
@ -10,21 +10,21 @@ using Website.ViewModels;
|
|||
namespace Website.Controllers {
|
||||
public class BlogController:Controller {
|
||||
private const int MaxPostsPerPage = 10;
|
||||
private readonly BlogRepository _repo;
|
||||
private readonly IBlogApi _api;
|
||||
|
||||
public BlogController(BlogRepository repo) => _repo = repo;
|
||||
public BlogController(IBlogApi api) => _api = api;
|
||||
|
||||
public async Task<IActionResult> Page(int page) {
|
||||
var offset = (page - 1) * MaxPostsPerPage;
|
||||
var posts = (await _repo.GetLatestPostsAsync(MaxPostsPerPage, offset)).Select(p => new BlogPostSnippetViewModel(p));
|
||||
var maxPages = (await _repo.GetCountAsync()) / MaxPostsPerPage;
|
||||
var posts = (await _api.GetLatestPostsAsync(MaxPostsPerPage, offset)).Select(p => new BlogPostSnippetViewModel(p));
|
||||
var maxPages = (await _api.GetCountAsync()) / MaxPostsPerPage;
|
||||
var model = new BlogViewModel(posts, page, maxPages);
|
||||
return View(model);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Entry(string url, bool preview = false) {
|
||||
try {
|
||||
var post = await _repo.GetPostByUrlAsync(url);
|
||||
var post = await _api.GetPostByUrlAsync(url);
|
||||
|
||||
if (!preview && string.IsNullOrEmpty(post.Content))
|
||||
return NotFound();
|
||||
|
@ -42,7 +42,7 @@ namespace Website.Controllers {
|
|||
return View();
|
||||
|
||||
try {
|
||||
var post = await _repo.GetPostByIdAsync(id.Value);
|
||||
var post = await _api.GetPostByIdAsync(id.Value);
|
||||
var model = new BlogPostSubmission {
|
||||
Id = post.Id,
|
||||
Title = post.Title,
|
||||
|
@ -57,35 +57,28 @@ namespace Website.Controllers {
|
|||
[Authorize]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Save(BlogPostSubmission submission) {
|
||||
var post = submission.Id.HasValue ? await _repo.GetPostByIdAsync(submission.Id.Value) : new BlogPost();
|
||||
|
||||
post.UpdateTitle(submission.Title);
|
||||
post.UpdateDraft(submission.Content);
|
||||
|
||||
var savedPost = await _repo.SavePost(post);
|
||||
var savedPost = await _api.SavePost(submission);
|
||||
|
||||
return RedirectToAction(nameof(Edit), new { savedPost.Id });
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<IActionResult> Manage() {
|
||||
var posts = await _repo.GetAllPostsAsync();
|
||||
var posts = await _api.GetAllPostsAsync();
|
||||
var models = posts.OrderByDescending(post => post.Timestamp).Select(post => new BlogPostViewModel(post, false));
|
||||
return View(models);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<IActionResult> Publish(int id) {
|
||||
var post = await _repo.GetPostByIdAsync(id);
|
||||
post.Publish();
|
||||
await _repo.SavePost(post);
|
||||
await _api.PublishPostAsync(id);
|
||||
|
||||
return RedirectToAction(nameof(Manage));
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<IActionResult> Delete(int id) {
|
||||
await _repo.DeletePostAsync(id);
|
||||
await _api.DeletePostAsync(id);
|
||||
|
||||
return RedirectToAction(nameof(Manage));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue