Skip to content

Commit

Permalink
Feat: Randomise sorting (#2894)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fesaa committed Apr 25, 2024
1 parent f59ad9f commit 8894b2e
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion API/DTOs/Filtering/SortField.cs
Expand Up @@ -33,5 +33,9 @@ public enum SortField
/// <summary>
/// Kavita+ Only - External Average Rating
/// </summary>
AverageRating = 8
AverageRating = 8,
/// <summary>
/// Randomise the order
/// </summary>
Random = 9
}
2 changes: 2 additions & 0 deletions API/Extensions/QueryExtensions/Filtering/BookmarkSort.cs
@@ -1,6 +1,7 @@
using System.Linq;
using API.DTOs.Filtering;
using API.Entities;
using Microsoft.EntityFrameworkCore;

namespace API.Extensions.QueryExtensions.Filtering;
#nullable enable
Expand Down Expand Up @@ -39,6 +40,7 @@ public static IQueryable<BookmarkSeriesPair> Sort(this IQueryable<BookmarkSeries
SortField.ReadProgress => query.DoOrderBy(s => s.Series.Progress.Where(p => p.SeriesId == s.Series.Id).Select(p => p.LastModified).Max(), sortOptions),
SortField.AverageRating => query.DoOrderBy(s => s.Series.ExternalSeriesMetadata.ExternalRatings
.Where(p => p.SeriesId == s.Series.Id).Average(p => p.AverageScore), sortOptions),
SortField.Random => query.DoOrderBy(s => EF.Functions.Random(), sortOptions),
_ => query
};

Expand Down
2 changes: 2 additions & 0 deletions API/Extensions/QueryExtensions/Filtering/SeriesSort.cs
@@ -1,6 +1,7 @@
using System.Linq;
using API.DTOs.Filtering;
using API.Entities;
using Microsoft.EntityFrameworkCore;

namespace API.Extensions.QueryExtensions.Filtering;
#nullable enable
Expand Down Expand Up @@ -35,6 +36,7 @@ public static IQueryable<Series> Sort(this IQueryable<Series> query, int userId,
.Max(), sortOptions),
SortField.AverageRating => query.DoOrderBy(s => s.ExternalSeriesMetadata.ExternalRatings
.Where(p => p.SeriesId == s.Id).Average(p => p.AverageScore), sortOptions),
SortField.Random => query.DoOrderBy(s => EF.Functions.Random(), sortOptions),
_ => query
};

Expand Down
3 changes: 2 additions & 1 deletion UI/Web/src/app/_models/metadata/series-filter.ts
Expand Up @@ -24,7 +24,8 @@ export enum SortField {
/**
* Kavita+ only
*/
AverageRating = 8
AverageRating = 8,
Random = 9
}

export const allSortFields = Object.keys(SortField)
Expand Down
2 changes: 2 additions & 0 deletions UI/Web/src/app/_pipes/sort-field.pipe.ts
Expand Up @@ -29,6 +29,8 @@ export class SortFieldPipe implements PipeTransform {
return this.translocoService.translate('sort-field-pipe.read-progress');
case SortField.AverageRating:
return this.translocoService.translate('sort-field-pipe.average-rating');
case SortField.Random:
return this.translocoService.translate('sort-field-pipe.random');
}

}
Expand Down
3 changes: 2 additions & 1 deletion UI/Web/src/assets/langs/en.json
Expand Up @@ -1745,7 +1745,8 @@
"time-to-read": "Time to Read",
"release-year": "Release Year",
"read-progress": "Last Read",
"average-rating": "Average Rating"
"average-rating": "Average Rating",
"random": "Random"
},

"edit-series-modal": {
Expand Down

0 comments on commit 8894b2e

Please sign in to comment.