Skip to content

Commit

Permalink
fix(services): allow sorting by user rating
Browse files Browse the repository at this point in the history
Fixes #980. Also rename the sorting constant to make way for #981.
  • Loading branch information
IgnisDa committed Aug 28, 2024
1 parent d4a31e0 commit 992af8f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crates/models/media/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ pub enum MediaSortBy {
#[default]
ReleaseDate,
LastSeen,
Rating,
UserRating,
}

#[derive(Debug, Serialize, Deserialize, Enum, Clone, PartialEq, Eq, Copy, Default)]
Expand Down
25 changes: 23 additions & 2 deletions crates/services/miscellaneous/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ use media_models::{
UserMediaNextEntry, UserMetadataDetailsEpisodeProgress, UserMetadataDetailsShowSeasonProgress,
UserUpcomingCalendarEventInput,
};
use migrations::{AliasedMetadata, AliasedMetadataToGenre, AliasedSeen, AliasedUserToEntity};
use migrations::{
AliasedMetadata, AliasedMetadataToGenre, AliasedReview, AliasedSeen, AliasedUserToEntity,
};
use nanoid::nanoid;
use notification_service::send_notification;
use providers::{
Expand Down Expand Up @@ -914,6 +916,8 @@ impl MiscellaneousService {
user_id: String,
input: MetadataListInput,
) -> Result<SearchResults<String>> {
let preferences = user_preferences_by_id(&self.db, &user_id, &self.config).await?;

let avg_rating_col = "average_rating";
let cloned_user_id_1 = user_id.clone();
let cloned_user_id_2 = user_id.clone();
Expand All @@ -931,6 +935,23 @@ impl MiscellaneousService {
let select = Metadata::find()
.select_only()
.column(metadata::Column::Id)
.expr_as(
Func::round_with_precision(
Func::avg(
Expr::col((AliasedReview::Table, AliasedReview::Rating)).div(
match preferences.general.review_scale {
UserReviewScale::OutOfFive => 20,
UserReviewScale::OutOfHundred => 1,
},
),
),
match preferences.general.review_scale {
UserReviewScale::OutOfFive => 1,
UserReviewScale::OutOfHundred => 0,
},
),
avg_rating_col,
)
.group_by(metadata::Column::Id)
.group_by(user_to_entity::Column::MediaReason)
.filter(user_to_entity::Column::UserId.eq(&user_id))
Expand Down Expand Up @@ -1004,7 +1025,7 @@ impl MiscellaneousService {
order_by,
NullOrdering::Last,
),
MediaSortBy::Rating => query.order_by_with_nulls(
MediaSortBy::UserRating => query.order_by_with_nulls(
Expr::col(Alias::new(avg_rating_col)),
order_by,
NullOrdering::Last,
Expand Down
4 changes: 2 additions & 2 deletions libs/generated/src/graphql/backend/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,9 +874,9 @@ export enum MediaLot {
export enum MediaSortBy {
LastSeen = 'LAST_SEEN',
LastUpdated = 'LAST_UPDATED',
Rating = 'RATING',
ReleaseDate = 'RELEASE_DATE',
Title = 'TITLE'
Title = 'TITLE',
UserRating = 'USER_RATING'
}

export type MediaSortInput = {
Expand Down

0 comments on commit 992af8f

Please sign in to comment.