Repository: IgnisDa/ryot (language: rust)

Issue:
Allow ordering by num times seen in the media list pages
This will be extremely useful in the music list pages.


Discussion / hints:

Fix patch:
diff --git a/crates/models/media/src/lib.rs b/crates/models/media/src/lib.rs
index 81a7f7dbd5..b479e4c4bd 100644
--- a/crates/models/media/src/lib.rs
+++ b/crates/models/media/src/lib.rs
@@ -1263,12 +1263,13 @@ pub enum GraphqlSortOrder {
 
 #[derive(Debug, Serialize, Deserialize, Enum, Clone, PartialEq, Eq, Copy, Default)]
 pub enum MediaSortBy {
-    LastUpdated,
     Title,
-    #[default]
-    ReleaseDate,
     LastSeen,
     UserRating,
+    #[default]
+    ReleaseDate,
+    LastUpdated,
+    TimesConsumed,
     ProviderRating,
 }
 
diff --git a/crates/utils/dependent/src/lib.rs b/crates/utils/dependent/src/lib.rs
index daecc86637..f76159dcdb 100644
--- a/crates/utils/dependent/src/lib.rs
+++ b/crates/utils/dependent/src/lib.rs
@@ -2792,7 +2792,6 @@ pub async fn metadata_list(
             avg_rating_col,
         )
         .group_by(metadata::Column::Id)
-        .group_by(user_to_entity::Column::MediaReason)
         .filter(user_to_entity::Column::UserId.eq(user_id))
         .apply_if(input.lot, |query, v| {
             query.filter(metadata::Column::Lot.eq(v))
@@ -2866,10 +2865,11 @@ pub async fn metadata_list(
             })),
         })
         .apply_if(input.sort.map(|s| s.by), |query, v| match v {
+            MediaSortBy::Title => query.order_by(metadata::Column::Title, order_by),
+            MediaSortBy::TimesConsumed => query.order_by(seen::Column::Id.count(), order_by),
             MediaSortBy::LastUpdated => query
                 .order_by(user_to_entity::Column::LastUpdatedOn, order_by)
                 .group_by(user_to_entity::Column::LastUpdatedOn),
-            MediaSortBy::Title => query.order_by(metadata::Column::Title, order_by),
             MediaSortBy::ReleaseDate => query.order_by_with_nulls(
                 metadata::Column::PublishYear,
                 order_by,
diff --git a/libs/generated/src/graphql/backend/graphql.ts b/libs/generated/src/graphql/backend/graphql.ts
index af1a195137..53e2938ea3 100644
--- a/libs/generated/src/graphql/backend/graphql.ts
+++ b/libs/generated/src/graphql/backend/graphql.ts
@@ -1005,6 +1005,7 @@ export enum MediaSortBy {
   LastUpdated = 'LAST_UPDATED',
   ProviderRating = 'PROVIDER_RATING',
   ReleaseDate = 'RELEASE_DATE',
+  TimesConsumed = 'TIMES_CONSUMED',
   Title = 'TITLE',
   UserRating = 'USER_RATING'
 }
diff --git a/libs/generated/src/graphql/backend/types.generated.ts b/libs/generated/src/graphql/backend/types.generated.ts
index 96ec3ae292..0a0a121aac 100644
--- a/libs/generated/src/graphql/backend/types.generated.ts
+++ b/libs/generated/src/graphql/backend/types.generated.ts
@@ -1030,6 +1030,7 @@ export enum MediaSortBy {
   LastUpdated = 'LAST_UPDATED',
   ProviderRating = 'PROVIDER_RATING',
   ReleaseDate = 'RELEASE_DATE',
+  TimesConsumed = 'TIMES_CONSUMED',
   Title = 'TITLE',
   UserRating = 'USER_RATING'
 }


Test patch: