Skip to content

Commit

Permalink
Refactor TagList page to inline code-behind logic
Browse files Browse the repository at this point in the history
Refactor the `TagList` page by moving code-behind logic from `TagList.cshtml.cs` to `TagList.cshtml`. This includes:
- Adding necessary `using` directives
- Injecting `IMediator` dependency
- Implementing `OnGet` method for GET requests
- Removing `@model` directive
- Introducing `@functions` block for properties and methods
- Accessing `Posts` property directly in Razor markup
- Updating `partial` tag helper to use the `Posts` property
  • Loading branch information
EdiWang committed Dec 3, 2024
1 parent 7829ef4 commit efe75f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
35 changes: 32 additions & 3 deletions src/Moonglade.Web/Pages/TagList.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
@page "/tags/{normalizedName:regex(^(?!-)([a-zA-Z0-9-]){{1,32}}$)}"
@model Moonglade.Web.Pages.TagListModel
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using Moonglade.Core.PostFeature
@using Moonglade.Core.TagFeature
@using Moonglade.Web.PagedList
@inject IMediator Mediator

@{
ViewBag.BodyClass = "body-tag-list bg-gray-1";
}
Expand All @@ -8,13 +13,37 @@
@ViewBag.TitlePrefix
</h2>

@if (Model.Posts.Count > 0)
@if (Posts.Count > 0)
{
<partial name="_PostList" model="Model.Posts" />
<partial name="_PostList" model="Posts" />
}
else
{
<div class="text-muted text-center">
- @SharedLocalizer["No Posts"] -
</div>
}

@functions {

[BindProperty(SupportsGet = true)]
public int P { get; set; } = 1;
public BasePagedList<PostDigest> Posts { get; set; }

public async Task<IActionResult> OnGet(string normalizedName)
{
var tagResponse = await Mediator.Send(new GetTagQuery(normalizedName));
if (tagResponse is null) return NotFound();

var pagesize = BlogConfig.ContentSettings.PostListPageSize;
var posts = await Mediator.Send(new ListByTagQuery(tagResponse.Id, pagesize, P));
var count = await Mediator.Send(new CountPostQuery(CountType.Tag, TagId: tagResponse.Id));

ViewData["TitlePrefix"] = tagResponse.DisplayName;

var list = new BasePagedList<PostDigest>(posts, P, pagesize, count);
Posts = list;

return Page();
}
}
30 changes: 0 additions & 30 deletions src/Moonglade.Web/Pages/TagList.cshtml.cs

This file was deleted.

0 comments on commit efe75f8

Please sign in to comment.