summaryrefslogtreecommitdiff
path: root/CoreWiki/Pages/Details.cshtml.cs
blob: bdd576f0b5d5ce68a5fd10bc01849f2754be7cd8 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using CoreWiki.Models;
using Htmx;
using Microsoft.AspNetCore.Http.HttpResults;
using NodaTime;

namespace CoreWiki.Pages
{
    public class DetailsModel : PageModel
    {
        private readonly IClock _clock;
        private readonly ApplicationDbContext _context;

        public DetailsModel(ApplicationDbContext context, IClock clock)
        {
            _clock = clock;
            _context = context;
        }

        private const int CommentsPerPage = 10;

        public Article Article { get; set; } = default!;
        [BindProperty] public Comment Comment { get; set; }

        public async Task<IActionResult> OnGetAsync(string? slug, [FromQuery] int? pageNumber)
        {
            slug ??= "home-page";
            var pagesToSkip = CommentsPerPage * ((pageNumber ?? 1) - 1);

            var article = await _context.Articles
                .Include(a => a.Comments
                    .OrderByDescending(c => c.SubmittedDateTime)
                    .Skip(pagesToSkip)
                    .Take(CommentsPerPage))
                .FirstOrDefaultAsync(a => a.Slug == slug);
            if (article == null)
            {
                return NotFound();
            }

            Article = article;
            if (Request.Cookies[Article.Slug] == null)
            {
                Article.ViewCount++;
                Response.Cookies.Append(Article.Slug, "viewed", new CookieOptions()
                {
                    Expires = DateTime.UtcNow.AddMinutes(5)
                });

                await _context.SaveChangesAsync();
            }

            if (Request.IsHtmx() && !Request.IsHtmxBoosted())
            {
                return Partial("_commentsList", new ArticleCommentListViewModel
                {
                    Comments = Article.Comments,
                    Slug = slug,
                    PageNumber = pageNumber ?? 1
                });
            }

            return Page();
        }

        public async Task<IActionResult> OnPostAsync(string? slug)
        {
            slug ??= "home-page";
            var article = await _context.Articles.FirstOrDefaultAsync(a => a.Slug == slug);
            if (article == null)
            {
                return NotFound();
            }

            Article = article;

            ModelState.Remove("Comment.Article");
            if (!ModelState.IsValid)
            {
                if (Request.IsHtmx() && !Request.IsHtmxBoosted())
                {
                    return Partial("_commentsForm", this);
                }

                return Page();
            }

            Comment.Submitted = _clock.GetCurrentInstant();
            article.Comments.Add(Comment);
            await _context.SaveChangesAsync();

            if (Request.IsHtmx() && !Request.IsHtmxBoosted())
            {
                Response.Htmx(h => { h.WithTrigger("commentsChanged"); });

                Comment.DisplayName = String.Empty;
                Comment.EMail = String.Empty;
                Comment.Content = String.Empty;
                ModelState.Clear();

                return Partial("_commentsForm", this);
            }

            return RedirectToPage("Details", new { slug = slug == "home-page" ? "" : slug });
        }
    }
}