Skip to content

Commit

Permalink
Merge pull request #775 from EdiWang/remove-sortby
Browse files Browse the repository at this point in the history
Remove "sort by" selection
  • Loading branch information
EdiWang authored Jan 16, 2024
2 parents fef2dc2 + 45d4496 commit e47abeb
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 57 deletions.
6 changes: 2 additions & 4 deletions src/Moonglade.Core/PostFeature/ListPostsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@

namespace Moonglade.Core.PostFeature;

public class ListPostsQuery(int pageSize, int pageIndex, Guid? catId = null, PostsSortBy sortBy = PostsSortBy.Recent)
public class ListPostsQuery(int pageSize, int pageIndex, Guid? catId = null)
: IRequest<IReadOnlyList<PostDigest>>
{
public int PageSize { get; set; } = pageSize;

public int PageIndex { get; set; } = pageIndex;

public Guid? CatId { get; set; } = catId;

public PostsSortBy SortBy { get; set; } = sortBy;
}

public class ListPostsQueryHandler(IRepository<PostEntity> repo) : IRequestHandler<ListPostsQuery, IReadOnlyList<PostDigest>>
Expand All @@ -21,7 +19,7 @@ public Task<IReadOnlyList<PostDigest>> Handle(ListPostsQuery request, Cancellati
{
Helper.ValidatePagingParameters(request.PageSize, request.PageIndex);

var spec = new PostPagingSpec(request.PageSize, request.PageIndex, request.CatId, request.SortBy);
var spec = new PostPagingSpec(request.PageSize, request.PageIndex, request.CatId);
return repo.SelectAsync(spec, PostDigest.EntitySelector, ct);
}
}
18 changes: 2 additions & 16 deletions src/Moonglade.Data/Spec/PostPagingSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,15 @@

namespace Moonglade.Data.Spec;

public enum PostsSortBy
{
Recent,
Featured
}

public sealed class PostPagingSpec : BaseSpecification<PostEntity>
{
public PostPagingSpec(int pageSize, int pageIndex, Guid? categoryId = null, PostsSortBy postsSortBy = PostsSortBy.Recent)
public PostPagingSpec(int pageSize, int pageIndex, Guid? categoryId = null)
: base(p => !p.IsDeleted && p.IsPublished &&
(categoryId == null || p.PostCategory.Select(c => c.CategoryId).Contains(categoryId.Value)))
{
var startRow = (pageIndex - 1) * pageSize;

switch (postsSortBy)
{
case PostsSortBy.Recent:
ApplyOrderByDescending(p => p.PubDateUtc);
break;
case PostsSortBy.Featured:
ApplyOrderByDescending(p => p.IsFeatured);
break;
}
ApplyOrderByDescending(p => p.PubDateUtc);
ApplyPaging(startRow, pageSize);
}

Expand Down
21 changes: 1 addition & 20 deletions src/Moonglade.Web/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@

<span class="flex-spacer"></span>

<span class="me-1">
<select class="form-select form-select-sm" asp-for="SortBy" id="sort-selection" title="Selection box for sort post list by recent or featured">
<option value="Recent">Recent</option>
<option value="Featured">Featured</option>
</select>
</span>

<a class="d-none d-md-block" asp-controller="Subscription" asp-action="Rss" target="_blank" title="@SharedLocalizer["Subscribe recent posts"]">
<i class="bi-rss font-125rem"></i>
</a>
Expand All @@ -37,16 +30,4 @@ else
</div>
}

<partial name="_LightSwitch" />

@section scripts{
<script>
var sortSelect = document.getElementById("sort-selection");
sortSelect.addEventListener("change", function () {
var value = sortSelect.value;
var searchParams = new URLSearchParams(window.location.search);
searchParams.set("sortBy", value);
window.location.search = searchParams.toString();
});
</script>
}
<partial name="_LightSwitch" />
21 changes: 5 additions & 16 deletions src/Moonglade.Web/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using Moonglade.Core.PostFeature;
using Moonglade.Data.Spec;
using Moonglade.Web.PagedList;

namespace Moonglade.Web.Pages;

public class IndexModel(IBlogConfig blogConfig, ICacheAside cache, IMediator mediator) : PageModel
{
public string SortBy { get; set; }

public BasePagedList<PostDigest> Posts { get; set; }

public async Task OnGet(int p = 1, string sortBy = "Recent")
public async Task OnGet(int p = 1)
{
var pagesize = blogConfig.ContentSettings.PostListPageSize;
if (Enum.TryParse(sortBy, out PostsSortBy sortByEnum))
{
ViewData["sortBy"] = sortBy;

var posts = await mediator.Send(new ListPostsQuery(pagesize, p, sortBy: sortByEnum));
var totalPostsCount = await cache.GetOrCreateAsync(BlogCachePartition.General.ToString(), "postcount", _ => mediator.Send(new CountPostQuery(CountType.Public)));
var posts = await mediator.Send(new ListPostsQuery(pagesize, p));
var totalPostsCount = await cache.GetOrCreateAsync(BlogCachePartition.General.ToString(), "postcount", _ => mediator.Send(new CountPostQuery(CountType.Public)));

var list = new BasePagedList<PostDigest>(posts, p, pagesize, totalPostsCount);
var list = new BasePagedList<PostDigest>(posts, p, pagesize, totalPostsCount);

Posts = list;
}
else
{
throw new ArgumentException(message: $"Invalid argument value '{sortBy}' for argument: '{nameof(sortBy)}'!");
}
Posts = list;
}
}
2 changes: 1 addition & 1 deletion src/Moonglade.Web/Pages/Shared/_PostList.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<partial name="_PostListEntry" model="item" />
}

@Html.PagedListPager(Model, page => ViewData["sortBy"] == null ? $"?p={page}" : $"?p={page}&sortBy={ViewData["sortBy"]}", new()
@Html.PagedListPager(Model, page => $"?p={page}", new()
{
UlElementClasses = new[] { "pagination justify-content-end" },
MaximumPageNumbersToDisplay = 5,
Expand Down

0 comments on commit e47abeb

Please sign in to comment.