summaryrefslogtreecommitdiff
path: root/CoreWiki/Pages/All.cshtml.cs
blob: c41e216dfe546824e73e5137559c03e25d48318f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using CoreWiki.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace CoreWiki.Pages;

public class All : PageModel
{
    private readonly ApplicationDbContext _context;
    
    [BindProperty(SupportsGet = true)] 
    public int PageNumber { get; set; } = 1;
    [BindProperty(SupportsGet = true)]
    public int PageSize { get; set; } = 25;
    public IEnumerable<Article>? Articles { get; set; }
    public int TotalPages { get; set; }

    public All(ApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<IActionResult> OnGetAsync()
    {
        TotalPages = (int)Math.Ceiling(await _context.Articles.CountAsync() / (float)PageSize);
        if (PageNumber > TotalPages) PageNumber = 1;
        
        Articles = await _context.Articles
            .AsNoTracking()
            .OrderBy(a => a.PublishedDateTime)
            .Skip(PageSize * (PageNumber - 1))
            .Take(PageSize)
            .ToArrayAsync();
        
        return Page();
    }
}