From 1a96616699ab41bf6343bc1acc45a836c3e6caf3 Mon Sep 17 00:00:00 2001 From: Paweł Bernaciak Date: Fri, 22 Dec 2023 16:02:41 +0100 Subject: Backup --- CoreWiki/Pages/All.cshtml | 48 ++++++++++++++++++++++++++++++++++++ CoreWiki/Pages/All.cshtml.cs | 39 +++++++++++++++++++++++++++++ CoreWiki/Pages/Create.cshtml | 4 +-- CoreWiki/Pages/Create.cshtml.cs | 14 ++++++++--- CoreWiki/Pages/Delete.cshtml | 6 ++--- CoreWiki/Pages/Delete.cshtml.cs | 33 +++++++++++-------------- CoreWiki/Pages/Details.cshtml | 8 +++--- CoreWiki/Pages/Details.cshtml.cs | 23 ++++++----------- CoreWiki/Pages/Edit.cshtml | 12 ++++----- CoreWiki/Pages/Edit.cshtml.cs | 17 ++++--------- CoreWiki/Pages/LatestChanges.cshtml | 6 ++--- CoreWiki/Pages/Shared/_Layout.cshtml | 15 ++++++----- 12 files changed, 152 insertions(+), 73 deletions(-) create mode 100644 CoreWiki/Pages/All.cshtml create mode 100644 CoreWiki/Pages/All.cshtml.cs (limited to 'CoreWiki/Pages') diff --git a/CoreWiki/Pages/All.cshtml b/CoreWiki/Pages/All.cshtml new file mode 100644 index 0000000..ca06cb5 --- /dev/null +++ b/CoreWiki/Pages/All.cshtml @@ -0,0 +1,48 @@ +@page +@model CoreWiki.Pages.All + +@{ + ViewData["Title"] = "All articles"; +} + +

All articles

+ +@foreach (var item in Model.Articles) +{ +
+
+

+ @item.Topic +

+
+ @item.Published +
+ + Edit + Delete +
+
+} + +
+ +
+ + +
+
\ No newline at end of file diff --git a/CoreWiki/Pages/All.cshtml.cs b/CoreWiki/Pages/All.cshtml.cs new file mode 100644 index 0000000..c41e216 --- /dev/null +++ b/CoreWiki/Pages/All.cshtml.cs @@ -0,0 +1,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
? Articles { get; set; } + public int TotalPages { get; set; } + + public All(ApplicationDbContext context) + { + _context = context; + } + + public async Task 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(); + } +} \ No newline at end of file diff --git a/CoreWiki/Pages/Create.cshtml b/CoreWiki/Pages/Create.cshtml index fc74317..4abbf11 100644 --- a/CoreWiki/Pages/Create.cshtml +++ b/CoreWiki/Pages/Create.cshtml @@ -9,8 +9,8 @@

Article


-
-
+
+
diff --git a/CoreWiki/Pages/Create.cshtml.cs b/CoreWiki/Pages/Create.cshtml.cs index d89b97b..0bf9327 100644 --- a/CoreWiki/Pages/Create.cshtml.cs +++ b/CoreWiki/Pages/Create.cshtml.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using CoreWiki.Models; +using CoreWiki.Utils; using NodaTime; namespace CoreWiki.Pages @@ -30,19 +31,26 @@ namespace CoreWiki.Pages public Article Article { get; set; } = default!; - // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD public async Task OnPostAsync() { - if (!ModelState.IsValid || _context.Articles == null || Article == null) + ModelState.Remove("Article.Slug"); + if (!ModelState.IsValid) { return Page(); } + Article.Slug = SafeUrl.Create(true, Article.Topic); Article.Published = _clock.GetCurrentInstant(); + if (_context.Articles.Any(a => a.Slug == Article.Slug)) + { + ModelState.AddModelError("Article.Topic", "Article already exists"); + return Page(); + } + _context.Articles.Add(Article); await _context.SaveChangesAsync(); - return Redirect($"/{Article.Topic}"); + return Redirect($"/{Article.Slug}"); } } } diff --git a/CoreWiki/Pages/Delete.cshtml b/CoreWiki/Pages/Delete.cshtml index ec40822..0b680b8 100644 --- a/CoreWiki/Pages/Delete.cshtml +++ b/CoreWiki/Pages/Delete.cshtml @@ -1,4 +1,4 @@ -@page +@page "/Delete/{slug}" @model CoreWiki.Pages.DeleteModel @{ @@ -27,8 +27,8 @@ - + | - Back to List + Back to Home Page
diff --git a/CoreWiki/Pages/Delete.cshtml.cs b/CoreWiki/Pages/Delete.cshtml.cs index 9de563e..584dc48 100644 --- a/CoreWiki/Pages/Delete.cshtml.cs +++ b/CoreWiki/Pages/Delete.cshtml.cs @@ -19,42 +19,39 @@ namespace CoreWiki.Pages } [BindProperty] - public Article Article { get; set; } = default!; + public Article Article { get; set; } = default!; - public async Task OnGetAsync(string id) + public async Task OnGetAsync(string slug) { - if (id == null || _context.Articles == null) + if (slug == "home-page") { return NotFound(); } - - var article = await _context.Articles.FirstOrDefaultAsync(m => m.Topic == id); + + var article = await _context.Articles.FirstOrDefaultAsync(a => a.Slug == slug); if (article == null) { return NotFound(); } - else - { - Article = article; - } + + Article = article; return Page(); } - public async Task OnPostAsync(string id) + public async Task OnPostAsync(string slug) { - if (id == null || _context.Articles == null) + if (slug == "home-page") { return NotFound(); } - var article = await _context.Articles.FindAsync(id); + + var article = await _context.Articles.FirstOrDefaultAsync(a => a.Slug == slug); - if (article != null) - { - Article = article; - _context.Articles.Remove(Article); - await _context.SaveChangesAsync(); - } + if (article == null) return NotFound(); + Article = article; + _context.Articles.Remove(Article); + await _context.SaveChangesAsync(); return Redirect("/"); } diff --git a/CoreWiki/Pages/Details.cshtml b/CoreWiki/Pages/Details.cshtml index bf1a616..9f48f9f 100644 --- a/CoreWiki/Pages/Details.cshtml +++ b/CoreWiki/Pages/Details.cshtml @@ -1,8 +1,8 @@ -@page "{topicName?}" +@page "/{slug?}" @model CoreWiki.Pages.DetailsModel @{ - ViewData["Title"] = "Details"; + ViewData["Title"] = Model.Article.Topic; }

@Model.Article.Topic

@@ -11,8 +11,8 @@
- Edit - @if (Model.Article.Topic != "HomePage") + Edit + @if (Model.Article.Slug != "home-page") { | Back to Home } diff --git a/CoreWiki/Pages/Details.cshtml.cs b/CoreWiki/Pages/Details.cshtml.cs index 25ff6c0..e4100ac 100644 --- a/CoreWiki/Pages/Details.cshtml.cs +++ b/CoreWiki/Pages/Details.cshtml.cs @@ -11,33 +11,26 @@ namespace CoreWiki.Pages { public class DetailsModel : PageModel { - private readonly CoreWiki.Models.ApplicationDbContext _context; + private readonly ApplicationDbContext _context; - public DetailsModel(CoreWiki.Models.ApplicationDbContext context) + public DetailsModel(ApplicationDbContext context) { _context = context; } - public Article Article { get; set; } = default!; + public Article Article { get; set; } = default!; - public async Task OnGetAsync(string? topicName) + public async Task OnGetAsync(string? slug) { - topicName ??= "HomePage"; - - if (_context.Articles == null) - { - return NotFound(); - } + slug ??= "home-page"; - var article = await _context.Articles.FirstOrDefaultAsync(m => m.Topic == topicName); + var article = await _context.Articles.FirstOrDefaultAsync(a => a.Slug == slug); if (article == null) { return NotFound(); } - else - { - Article = article; - } + + Article = article; return Page(); } } diff --git a/CoreWiki/Pages/Edit.cshtml b/CoreWiki/Pages/Edit.cshtml index 7609348..8793034 100644 --- a/CoreWiki/Pages/Edit.cshtml +++ b/CoreWiki/Pages/Edit.cshtml @@ -1,4 +1,4 @@ -@page +@page "/Edit/{slug}" @using CoreWiki.Models @model CoreWiki.Pages.EditModel @@ -8,17 +8,15 @@

Edit

-

@Model.Article.Topic

-
+
-
+
+
- - - +

@Model.Article.Topic

diff --git a/CoreWiki/Pages/Edit.cshtml.cs b/CoreWiki/Pages/Edit.cshtml.cs index e3b11c2..46920cb 100644 --- a/CoreWiki/Pages/Edit.cshtml.cs +++ b/CoreWiki/Pages/Edit.cshtml.cs @@ -25,14 +25,9 @@ namespace CoreWiki.Pages [BindProperty] public Article Article { get; set; } = default!; - public async Task OnGetAsync(string id) + public async Task OnGetAsync(string slug) { - if (id == null || _context.Articles == null) - { - return NotFound(); - } - - var article = await _context.Articles.FirstOrDefaultAsync(m => m.Topic == id); + var article = await _context.Articles.FirstOrDefaultAsync(m => m.Slug == slug); if (article == null) { return NotFound(); @@ -40,9 +35,7 @@ namespace CoreWiki.Pages Article = article; return Page(); } - - // To protect from overposting attacks, enable the specific properties you want to bind to. - // For more details, see https://aka.ms/RazorPagesCRUD. + public async Task OnPostAsync() { if (!ModelState.IsValid) @@ -69,12 +62,12 @@ namespace CoreWiki.Pages } } - return Redirect($"./{(Article.Topic == "HomePage" ? "" : Article.Topic)}"); + return Redirect($"/{(Article.Slug == "home-page" ? "" : Article.Slug)}"); } private bool ArticleExists(string id) { - return (_context.Articles?.Any(e => e.Topic == id)).GetValueOrDefault(); + return (_context.Articles?.Any(e => e.Slug == id)).GetValueOrDefault(); } } } diff --git a/CoreWiki/Pages/LatestChanges.cshtml b/CoreWiki/Pages/LatestChanges.cshtml index 390c8ab..677887b 100644 --- a/CoreWiki/Pages/LatestChanges.cshtml +++ b/CoreWiki/Pages/LatestChanges.cshtml @@ -15,11 +15,11 @@ {
-

@item.Topic

+

@item.Topic

@item.Published
- Edit - Delete + Edit + Delete
} \ No newline at end of file diff --git a/CoreWiki/Pages/Shared/_Layout.cshtml b/CoreWiki/Pages/Shared/_Layout.cshtml index dbe5e47..ac74eba 100644 --- a/CoreWiki/Pages/Shared/_Layout.cshtml +++ b/CoreWiki/Pages/Shared/_Layout.cshtml @@ -7,14 +7,14 @@ - + @await RenderSectionAsync("Styles", required: false)
-