42 lines
1.1 KiB
C#
42 lines
1.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Website.Data;
|
|
using Website.ViewModels;
|
|
|
|
namespace Website.Controllers
|
|
{
|
|
public class BlogController : Controller
|
|
{
|
|
const int MaxPostsPerPage = 10;
|
|
private readonly BlogRepository _repo;
|
|
|
|
public BlogController(BlogRepository repo)
|
|
{
|
|
_repo = repo;
|
|
}
|
|
|
|
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 maxPages = (await _repo.GetCountAsync()) / MaxPostsPerPage;
|
|
var model = new BlogViewModel(posts, page, maxPages);
|
|
return View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> Entry(string url)
|
|
{
|
|
try {
|
|
var post = await _repo.GetPostByUrlAsync(url);
|
|
var model = new BlogPostViewModel(post);
|
|
return View(model);
|
|
} catch (InvalidOperationException e) {
|
|
return NotFound();
|
|
}
|
|
}
|
|
}
|
|
}
|