Skip to content

Commit

Permalink
Order media by tag count. Closes #265.
Browse files Browse the repository at this point in the history
  • Loading branch information
impworks committed Jan 2, 2024
1 parent 111e06d commit 4fb1111
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/Bonsai/Areas/Admin/Logic/MediaManagerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,18 @@ public async Task<MediaListVM> GetMediaAsync(MediaListRequestVM request)
var totalCount = await query.CountAsync();
result.PageCount = (int) Math.Ceiling((double) totalCount / PageSize);

result.Items = await query.OrderBy(request.OrderBy, request.OrderDescending ?? false)
.ProjectToType<MediaThumbnailExtendedVM>(_mapper.Config)
if (request.OrderBy == nameof(Media.Tags))
{
query = request.OrderDescending ?? false
? query.OrderByDescending(x => x.Tags.Count)
: query.OrderBy(x => x.Tags.Count);
}
else
{
query = query.OrderBy(request.OrderBy, request.OrderDescending ?? false);
}

result.Items = await query.ProjectToType<MediaThumbnailExtendedVM>(_mapper.Config)
.Skip(PageSize * request.Page)
.Take(PageSize)
.ToListAsync();
Expand Down Expand Up @@ -323,7 +333,7 @@ private MediaListRequestVM NormalizeListRequest(MediaListRequestVM vm)
if(vm == null)
vm = new MediaListRequestVM { OrderDescending = true };

var orderableFields = new[] { nameof(Media.UploadDate), nameof(Media.Date), nameof(Media.Title) };
var orderableFields = new[] { nameof(Media.UploadDate), nameof(Media.Date), nameof(Media.Title), nameof(Media.Tags) };
if(!orderableFields.Contains(vm.OrderBy))
vm.OrderBy = orderableFields[0];

Expand Down
2 changes: 1 addition & 1 deletion src/Bonsai/Areas/Admin/Views/Dashboard/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
{
<div class="card-footer">
<small class="text-muted">
<a href="@Url.Action("Index", "Media", new {area = "Admin"})">@Model.MediaToTagCount без отметок</a>
<a href="@Url.Action("Index", "Media", new {area = "Admin", OrderBy = "Tags", OrderDescending = false})">@Model.MediaToTagCount без отметок</a>
</small>
</div>
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bonsai/Areas/Admin/Views/Media/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<th width="50%">@await Component.InvokeAsync("ListHeaderComponent", new { url = baseUrl, request = Model.Request, propName = nameof(MediaThumbnailExtendedVM.Title), title = "Название" })</th>
<th width="20%">@await Component.InvokeAsync("ListHeaderComponent", new { url = baseUrl, request = Model.Request, propName = nameof(MediaThumbnailExtendedVM.Date), title = "Снято" })</th>
<th width="20%">@await Component.InvokeAsync("ListHeaderComponent", new { url = baseUrl, request = Model.Request, propName = nameof(MediaThumbnailExtendedVM.UploadDate), title = "Загружено" })</th>
<th width="10%">Отметки</th>
<th width="10%">@await Component.InvokeAsync("ListHeaderComponent", new { url = baseUrl, request = Model.Request, propName = nameof(Media.Tags), title = "Теги" })</th>
<th width="1px"></th>
</tr>
</thead>
Expand Down

0 comments on commit 4fb1111

Please sign in to comment.