From 79f5eaf40b93d64be74d8b4f1ef80d19fadbfbe1 Mon Sep 17 00:00:00 2001 From: Paweł Bernaciak Date: Fri, 29 Dec 2023 09:41:56 +0100 Subject: Cywilizowany tailwind --- CoreWiki/Pages/Details.cshtml.cs | 86 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 5 deletions(-) (limited to 'CoreWiki/Pages/Details.cshtml.cs') diff --git a/CoreWiki/Pages/Details.cshtml.cs b/CoreWiki/Pages/Details.cshtml.cs index e4100ac..bdd576f 100644 --- a/CoreWiki/Pages/Details.cshtml.cs +++ b/CoreWiki/Pages/Details.cshtml.cs @@ -6,32 +6,108 @@ 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) + public DetailsModel(ApplicationDbContext context, IClock clock) { + _clock = clock; _context = context; } - public Article Article { get; set; } = default!; + private const int CommentsPerPage = 10; - public async Task OnGetAsync(string? slug) + public Article Article { get; set; } = default!; + [BindProperty] public Comment Comment { get; set; } + + public async Task OnGetAsync(string? slug, [FromQuery] int? pageNumber) { slug ??= "home-page"; + var pagesToSkip = CommentsPerPage * ((pageNumber ?? 1) - 1); - var article = await _context.Articles.FirstOrDefaultAsync(a => a.Slug == slug); + 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 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 }); + } } -} +} \ No newline at end of file -- cgit v1.2.3