Files
taxbaik/TaxBaik.Web/Pages/Blog/Index.cshtml.cs

48 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Mvc.RazorPages;
using TaxBaik.Application.Services;
using TaxBaik.Domain.Entities;
namespace TaxBaik.Web.Pages.Blog;
public class BlogIndexModel : PageModel
{
private readonly BlogService _blogService;
private readonly CategoryService _categoryService;
public List<BlogPost> Posts { get; set; } = [];
public List<Category> Categories { get; set; } = [];
public int CurrentPage { get; set; } = 1;
public int TotalPages { get; set; }
public int? SelectedCategoryId { get; set; }
private const int PageSize = 12;
public BlogIndexModel(BlogService blogService, CategoryService categoryService)
{
_blogService = blogService;
_categoryService = categoryService;
}
public async Task OnGetAsync(int page = 1, int? categoryId = null)
{
try
{
CurrentPage = page;
SelectedCategoryId = categoryId;
Categories = (await _categoryService.GetAllAsync()).ToList();
var (posts, total) = await _blogService.GetPublishedPagedAsync(page, PageSize, categoryId);
Posts = posts.ToList();
TotalPages = (total + PageSize - 1) / PageSize;
}
catch
{
CurrentPage = page;
SelectedCategoryId = categoryId;
Categories = [];
Posts = [];
TotalPages = 0;
}
}
}