diff --git a/Cargo.lock b/Cargo.lock index f882398..3696e5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -698,6 +698,7 @@ dependencies = [ name = "flix-model" version = "0.0.16" dependencies = [ + "itertools", "seamantic", "serde", "thiserror 2.0.17", diff --git a/Cargo.toml b/Cargo.toml index 28a1761..30dbd0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = ["crates/*"] [workspace.package] edition = "2024" -rust-version = "1.85.0" +rust-version = "1.87.0" license-file = "LICENSE.md" [workspace.dependencies] @@ -20,6 +20,7 @@ flix-model = { path = "crates/model", version = "=0.0.16", default-features = fa flix-tmdb = { path = "crates/tmdb", version = "=0.0.16", default-features = false } futures = { version = "^0.3", default-features = false } governor = { version = "^0.10", default-features = false } +itertools = { version = "^0.14", default-features = false } nonzero_ext = { version = "^0.3", default-features = false } regex = { version = "^1", default-features = false } reqwest = { version = "^0.13", default-features = false } diff --git a/README.md b/README.md index 0dbad06..d2f723c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Libraries and tools for dealing with media metadata - build: `cargo hack --feature-powerset build` - clippy: `cargo hack --feature-powerset clippy -- -D warnings` - test: `cargo hack --feature-powerset test` -- test old: `cargo +1.85 hack --feature-powerset test` +- test old: `cargo +1.87 hack --feature-powerset test` - fmt: `cargo fmt --check` - docs: `RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features` - install: `cargo install --path crates/cli` diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs deleted file mode 100644 index 4a69339..0000000 --- a/crates/cli/src/lib.rs +++ /dev/null @@ -1,3 +0,0 @@ -//! Placeholder - -#![cfg_attr(docsrs, feature(doc_cfg))] diff --git a/crates/cli/src/run/flix.rs b/crates/cli/src/run/flix.rs index 06cf2b7..8b1875d 100644 --- a/crates/cli/src/run/flix.rs +++ b/crates/cli/src/run/flix.rs @@ -1,5 +1,6 @@ use flix::db::entity; use flix::model::id::CollectionId; +use flix::model::text; use anyhow::Result; use sea_orm::ActiveValue::{NotSet, Set}; @@ -14,11 +15,18 @@ pub async fn add(db: &DatabaseConnection, command: AddCommand) -> Result<()> { .transaction(|txn| { let title = title.clone(); + let sort_title = text::make_sortable_title(&title); + let fs_slug = text::make_fs_slug(&title); + let web_slug = text::make_web_slug(&title); + Box::pin(async move { let flix = entity::info::collections::ActiveModel { id: NotSet, title: Set(title), overview: Set(overview), + sort_title: Set(sort_title), + fs_slug: Set(fs_slug), + web_slug: Set(web_slug), } .insert(txn) .await?; diff --git a/crates/cli/src/run/tmdb.rs b/crates/cli/src/run/tmdb.rs index 2532369..0be2f2e 100644 --- a/crates/cli/src/run/tmdb.rs +++ b/crates/cli/src/run/tmdb.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use flix::db::entity; use flix::model::id::{CollectionId, MovieId, ShowId}; use flix::model::numbers::{EpisodeNumber, SeasonNumber}; +use flix::model::text; use flix::tmdb::Client; use flix::tmdb::model::id::{ CollectionId as TmdbCollectionId, MovieId as TmdbMovieId, ShowId as TmdbShowId, @@ -37,6 +38,10 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R let title = collection.title.clone(); + let sort_title = text::make_sortable_title(&title); + let fs_slug = text::make_fs_slug(&title); + let web_slug = text::make_web_slug(&title); + let result: Result> = db .transaction(|txn| { Box::pin(async move { @@ -44,6 +49,9 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R id: NotSet, title: Set(collection.title), overview: Set(collection.overview), + sort_title: Set(sort_title), + fs_slug: Set(fs_slug), + web_slug: Set(web_slug), } .insert(txn) .await?; @@ -88,6 +96,10 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R let title = movie.title.clone(); let year = movie.release_date.year(); + let sort_title = text::make_sortable_title(&title); + let fs_slug = text::make_fs_slug_year(&title, year); + let web_slug = text::make_web_slug_year(&title, year); + let result: Result> = db .transaction(|txn| { Box::pin(async move { @@ -97,6 +109,9 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R tagline: Set(movie.tagline), overview: Set(movie.overview), date: Set(movie.release_date), + sort_title: Set(sort_title), + fs_slug: Set(fs_slug), + web_slug: Set(web_slug), } .insert(txn) .await?; @@ -203,6 +218,10 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R seasons.push(season); } + let sort_title = text::make_sortable_title(&show.title); + let fs_slug = text::make_fs_slug_year(&show.title, show.first_air_date.year()); + let web_slug = text::make_web_slug_year(&show.title, show.first_air_date.year()); + let result: Result> = db .transaction(|txn| { Box::pin(async move { @@ -212,6 +231,9 @@ pub async fn add(client: Client, db: &DatabaseConnection, command: Command) -> R tagline: Set(show.tagline), overview: Set(show.overview), date: Set(show.first_air_date), + sort_title: Set(sort_title), + fs_slug: Set(fs_slug), + web_slug: Set(web_slug), } .insert(txn) .await?; diff --git a/crates/db/src/entity/content.rs b/crates/db/src/entity/content.rs index 7eda9a3..8ef625d 100644 --- a/crates/db/src/entity/content.rs +++ b/crates/db/src/entity/content.rs @@ -15,7 +15,7 @@ pub mod libraries { #[sea_orm(table_name = "flix_libraries")] pub struct Model { /// The library's ID - #[sea_orm(column_type = "Integer", primary_key, nullable, auto_increment = false)] + #[sea_orm(primary_key, auto_increment = false)] pub id: LibraryId, /// The library's directory pub directory: PathBytes, @@ -23,19 +23,19 @@ pub mod libraries { pub last_scan: Option>, /// Collections that are part of this library - #[sea_orm(has_many, on_update = "Cascade", on_delete = "Cascade")] + #[sea_orm(has_many)] pub collections: HasMany, /// Movies that are part of this library - #[sea_orm(has_many, on_update = "Cascade", on_delete = "Cascade")] + #[sea_orm(has_many)] pub movies: HasMany, /// Shows that are part of this library - #[sea_orm(has_many, on_update = "Cascade", on_delete = "Cascade")] + #[sea_orm(has_many)] pub shows: HasMany, /// Seasons that are part of this library - #[sea_orm(has_many, on_update = "Cascade", on_delete = "Cascade")] + #[sea_orm(has_many)] pub seasons: HasMany, /// Episodes that are part of this library - #[sea_orm(has_many, on_update = "Cascade", on_delete = "Cascade")] + #[sea_orm(has_many)] pub episodes: HasMany, } @@ -58,14 +58,11 @@ pub mod collections { #[sea_orm(table_name = "flix_collections")] pub struct Model { /// The collection's ID - #[sea_orm(column_type = "Integer", primary_key, nullable, auto_increment = false)] + #[sea_orm(primary_key, auto_increment = false)] pub id: CollectionId, /// The collection's parent #[sea_orm(indexed)] pub parent_id: Option, - /// The collection's slug - #[sea_orm(unique)] - pub slug: String, /// The collection's library ID pub library_id: LibraryId, /// The collection's directory @@ -74,14 +71,35 @@ pub mod collections { pub relative_poster_path: Option, /// This collection's parent - #[sea_orm(self_ref, relation_enum = "Parent", from = "parent_id", to = "id")] + #[sea_orm( + self_ref, + relation_enum = "Parent", + from = "parent_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub parent: HasOne, /// The library this collection belongs to - #[sea_orm(belongs_to, from = "library_id", to = "id")] + #[sea_orm( + belongs_to, + from = "library_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub library: HasOne, /// The info for this collection - #[sea_orm(belongs_to, relation_enum = "Info", from = "id", to = "id")] + #[sea_orm( + belongs_to, + relation_enum = "Info", + from = "id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub info: HasOne, + /// The watched info for this collection #[sea_orm(has_many, relation_enum = "Watched", from = "id", to = "id")] pub watched: HasMany, @@ -106,14 +124,11 @@ pub mod movies { #[sea_orm(table_name = "flix_movies")] pub struct Model { /// The movie's ID - #[sea_orm(column_type = "Integer", primary_key, nullable, auto_increment = false)] + #[sea_orm(primary_key, auto_increment = false)] pub id: MovieId, /// The movie's parent #[sea_orm(indexed)] pub parent_id: Option, - /// The movie's slug - #[sea_orm(unique)] - pub slug: String, /// The movie's library pub library_id: LibraryId, /// The movie's directory @@ -124,14 +139,34 @@ pub mod movies { pub relative_poster_path: Option, /// This movie's parent - #[sea_orm(belongs_to, from = "parent_id", to = "id")] + #[sea_orm( + belongs_to, + from = "parent_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub parent: HasOne, /// The library this movie belongs to - #[sea_orm(belongs_to, from = "library_id", to = "id")] + #[sea_orm( + belongs_to, + from = "library_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub library: HasOne, /// The info for this movie - #[sea_orm(belongs_to, relation_enum = "Info", from = "id", to = "id")] + #[sea_orm( + belongs_to, + relation_enum = "Info", + from = "id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub info: HasOne, + /// The watched info for this movie #[sea_orm(has_many, relation_enum = "Watched", from = "id", to = "id")] pub watched: HasMany, @@ -156,14 +191,11 @@ pub mod shows { #[sea_orm(table_name = "flix_shows")] pub struct Model { /// The show's ID - #[sea_orm(column_type = "Integer", primary_key, nullable, auto_increment = false)] + #[sea_orm(primary_key, auto_increment = false)] pub id: ShowId, /// The show's parent #[sea_orm(indexed)] pub parent_id: Option, - /// The show's slug - #[sea_orm(unique)] - pub slug: String, /// The show's library pub library_id: LibraryId, /// The show's directory @@ -172,14 +204,40 @@ pub mod shows { pub relative_poster_path: Option, /// This show's parent - #[sea_orm(belongs_to, from = "parent_id", to = "id")] + #[sea_orm( + belongs_to, + from = "parent_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub parent: HasOne, /// The library this show belongs to - #[sea_orm(belongs_to, from = "library_id", to = "id")] + #[sea_orm( + belongs_to, + from = "library_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub library: HasOne, /// The info for this show - #[sea_orm(belongs_to, relation_enum = "Info", from = "id", to = "id")] + #[sea_orm( + belongs_to, + relation_enum = "Info", + from = "id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub info: HasOne, + + /// Seasons that are part of this show + #[sea_orm(has_many)] + pub seasons: HasMany, + /// Episodes that are part of this show + #[sea_orm(has_many)] + pub episodes: HasMany, /// The watched info for this show #[sea_orm(has_many, relation_enum = "Watched", from = "id", to = "id")] pub watched: HasMany, @@ -210,9 +268,6 @@ pub mod seasons { /// The season's number #[sea_orm(primary_key, auto_increment = false)] pub season_number: SeasonNumber, - /// The season's slug - #[sea_orm(unique)] - pub slug: String, /// The season's library pub library_id: LibraryId, /// The season's directory @@ -220,17 +275,38 @@ pub mod seasons { /// The season's poster path pub relative_poster_path: Option, + /// This season's show + #[sea_orm( + belongs_to, + from = "show_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] + pub show: HasOne, /// The library this season belongs to - #[sea_orm(belongs_to, from = "library_id", to = "id")] + #[sea_orm( + belongs_to, + from = "library_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub library: HasOne, /// The info for this season #[sea_orm( belongs_to, relation_enum = "Info", from = "(show_id, season_number)", - to = "(show_id, season_number)" + to = "(show_id, season_number)", + on_update = "Cascade", + on_delete = "Cascade" )] pub info: HasOne, + + /// Episodes that are part of this show + #[sea_orm(has_many)] + pub episodes: HasMany, /// The watched info for this season #[sea_orm( has_many, @@ -271,9 +347,6 @@ pub mod episodes { pub episode_number: EpisodeNumber, /// The number of additional contained episodes pub count: u8, - /// The episode's slug - #[sea_orm(unique)] - pub slug: String, /// The episode's library pub library_id: LibraryId, /// The episode's directory @@ -283,17 +356,44 @@ pub mod episodes { /// The episode's poster path pub relative_poster_path: Option, + /// This episode's show + #[sea_orm( + belongs_to, + from = "show_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] + pub show: HasOne, + /// This episode's season + #[sea_orm( + belongs_to, + from = "(show_id, season_number)", + to = "(show_id, season_number)", + on_update = "Cascade", + on_delete = "Cascade" + )] + pub season: HasOne, /// The library this episode belongs to - #[sea_orm(belongs_to, from = "library_id", to = "id")] + #[sea_orm( + belongs_to, + from = "library_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub library: HasOne, /// The info for this episode #[sea_orm( belongs_to, relation_enum = "Info", from = "(show_id, season_number, episode_number)", - to = "(show_id, season_number, episode_number)" + to = "(show_id, season_number, episode_number)", + on_update = "Cascade", + on_delete = "Cascade" )] pub info: HasOne, + /// The watched info for this episode #[sea_orm( has_many, @@ -311,7 +411,7 @@ pub mod episodes { #[cfg(test)] pub mod test { macro_rules! make_content_library { - ($db:expr, $id:literal) => { + ($db:expr, $id:expr) => { $crate::entity::content::libraries::ActiveModel { id: Set(::flix_model::id::LibraryId::from_raw($id)), directory: Set(::std::path::PathBuf::new().into()), @@ -325,12 +425,11 @@ pub mod test { pub(crate) use make_content_library; macro_rules! make_content_collection { - ($db:expr, $lid:literal, $id:literal, $pid:expr) => { + ($db:expr, $lid:expr, $id:expr, $pid:expr) => { $crate::entity::info::test::make_info_collection!($db, $id); $crate::entity::content::collections::ActiveModel { id: Set(::flix_model::id::CollectionId::from_raw($id)), parent_id: Set($pid.map(::flix_model::id::CollectionId::from_raw)), - slug: Set(concat!("C ", $id).to_string()), library_id: Set(::flix_model::id::LibraryId::from_raw($lid)), directory: Set(::std::path::PathBuf::new().into()), relative_poster_path: Set(::core::option::Option::None), @@ -343,12 +442,11 @@ pub mod test { pub(crate) use make_content_collection; macro_rules! make_content_movie { - ($db:expr, $lid:literal, $id:literal, $pid:expr) => { + ($db:expr, $lid:expr, $id:expr, $pid:expr) => { $crate::entity::info::test::make_info_movie!($db, $id); $crate::entity::content::movies::ActiveModel { id: Set(::flix_model::id::MovieId::from_raw($id)), parent_id: Set($pid.map(::flix_model::id::CollectionId::from_raw)), - slug: Set(concat!("< ", $id).to_string()), library_id: Set(::flix_model::id::LibraryId::from_raw($lid)), directory: Set(::std::path::PathBuf::new().into()), relative_media_path: Set(::std::string::String::new()), @@ -362,12 +460,11 @@ pub mod test { pub(crate) use make_content_movie; macro_rules! make_content_show { - ($db:expr, $lid:literal, $id:literal, $pid:expr) => { + ($db:expr, $lid:expr, $id:expr, $pid:expr) => { $crate::entity::info::test::make_info_show!($db, $id); $crate::entity::content::shows::ActiveModel { id: Set(::flix_model::id::ShowId::from_raw($id)), parent_id: Set($pid.map(::flix_model::id::CollectionId::from_raw)), - slug: Set(concat!("S ", $id).to_string()), library_id: Set(::flix_model::id::LibraryId::from_raw($lid)), directory: Set(::std::path::PathBuf::new().into()), relative_poster_path: Set(::core::option::Option::None), @@ -380,12 +477,11 @@ pub mod test { pub(crate) use make_content_show; macro_rules! make_content_season { - ($db:expr, $lid:literal, $show:literal, $season:literal) => { + ($db:expr, $lid:expr, $show:expr, $season:expr) => { $crate::entity::info::test::make_info_season!($db, $show, $season); $crate::entity::content::seasons::ActiveModel { show_id: Set(::flix_model::id::ShowId::from_raw($show)), season_number: Set(::flix_model::numbers::SeasonNumber::new($season)), - slug: Set(concat!("SS ", $show, $season).to_string()), library_id: Set(::flix_model::id::LibraryId::from_raw($lid)), directory: Set(::std::path::PathBuf::new().into()), relative_poster_path: Set(::core::option::Option::None), @@ -398,20 +494,19 @@ pub mod test { pub(crate) use make_content_season; macro_rules! make_content_episode { - ($db:expr, $lid:literal, $show:literal, $season:literal, $episode:literal) => { + ($db:expr, $lid:expr, $show:expr, $season:expr, $episode:expr) => { make_content_episode!(@make, $db, $lid, $show, $season, $episode, 0); }; ($db:expr, $lid:literal, $show:literal, $season:literal, $episode:literal, >1) => { make_content_episode!(@make, $db, $lid, $show, $season, $episode, 1); }; - (@make, $db:expr, $lid:literal, $show:literal, $season:literal, $episode:literal, $count:literal) => { + (@make, $db:expr, $lid:expr, $show:expr, $season:expr, $episode:expr, $count:literal) => { $crate::entity::info::test::make_info_episode!($db, $show, $season, $episode); $crate::entity::content::episodes::ActiveModel { show_id: Set(::flix_model::id::ShowId::from_raw($show)), season_number: Set(::flix_model::numbers::SeasonNumber::new($season)), episode_number: Set(::flix_model::numbers::EpisodeNumber::new($episode)), count: Set($count), - slug: Set(concat!("SSE ", $show, $season, $episode).to_string()), library_id: Set(::flix_model::id::LibraryId::from_raw($lid)), directory: Set(::std::path::PathBuf::new().into()), relative_media_path: Set(::std::string::String::new()), @@ -507,7 +602,6 @@ mod tests { assert_eq!(model.id, CollectionId::from_raw($id)); assert_eq!(model.parent_id, $pid.map(CollectionId::from_raw)); - assert_eq!(model.slug, concat!("C Slug ", $id).to_string()); assert_eq!(model.library_id, LibraryId::from_raw($lid)); assert_eq!(model.directory, Path::new(concat!("C Directory ", $id)).to_owned().into()); assert_eq!(model.relative_poster_path, noneable!(relative_poster_path, concat!("C Poster ", $id).to_owned() $(, $($skip),+)?)); @@ -522,7 +616,6 @@ mod tests { super::collections::ActiveModel { id: notsettable!(id, CollectionId::from_raw($id) $(, $($skip),+)?), parent_id: notsettable!(parent_id, $pid.map(CollectionId::from_raw) $(, $($skip),+)?), - slug: notsettable!(slug, concat!("C Slug ", $id).to_string() $(, $($skip),+)?), library_id: notsettable!(library_id, LibraryId::from_raw($lid) $(, $($skip),+)?), directory: notsettable!(directory, Path::new(concat!("C Directory ", $id)).to_owned().into() $(, $($skip),+)?), relative_poster_path: notsettable!(relative_poster_path, Some(concat!("C Poster ", $id).to_owned()) $(, $($skip),+)?), @@ -548,10 +641,9 @@ mod tests { make_info_collection!(&db, 8); assert_collection!(&db, 3, None, 1, Success; id); assert_collection!(&db, 4, None, 1, Success; parent_id); - assert_collection!(&db, 5, None, 1, NotNullViolation; slug); - assert_collection!(&db, 6, None, 1, NotNullViolation; library_id); - assert_collection!(&db, 7, None, 1, NotNullViolation; directory); - assert_collection!(&db, 8, None, 1, Success; relative_poster_path); + assert_collection!(&db, 5, None, 1, NotNullViolation; library_id); + assert_collection!(&db, 6, None, 1, NotNullViolation; directory); + assert_collection!(&db, 7, None, 1, Success; relative_poster_path); } #[tokio::test] @@ -565,7 +657,6 @@ mod tests { assert_eq!(model.id, MovieId::from_raw($id)); assert_eq!(model.parent_id, $pid.map(CollectionId::from_raw)); - assert_eq!(model.slug, concat!("M Slug ", $id).to_string()); assert_eq!(model.library_id, LibraryId::from_raw($lid)); assert_eq!(model.directory, Path::new(concat!("M Directory ", $id)).to_owned().into()); assert_eq!(model.relative_media_path, concat!("M Media ", $id)); @@ -581,7 +672,6 @@ mod tests { super::movies::ActiveModel { id: notsettable!(id, MovieId::from_raw($id) $(, $($skip),+)?), parent_id: notsettable!(parent_id, $pid.map(CollectionId::from_raw) $(, $($skip),+)?), - slug: notsettable!(slug, concat!("M Slug ", $id).to_string() $(, $($skip),+)?), library_id: notsettable!(library_id, LibraryId::from_raw($lid) $(, $($skip),+)?), directory: notsettable!(directory, Path::new(concat!("M Directory ", $id)).to_owned().into() $(, $($skip),+)?), relative_media_path: notsettable!(relative_media_path, concat!("M Media ", $id).to_owned() $(, $($skip),+)?), @@ -612,11 +702,10 @@ mod tests { make_info_movie!(&db, 9); assert_movie!(&db, 3, None, 1, Success; id); assert_movie!(&db, 4, None, 1, Success; parent_id); - assert_movie!(&db, 5, None, 1, NotNullViolation; slug); - assert_movie!(&db, 6, None, 1, NotNullViolation; library_id); - assert_movie!(&db, 7, None, 1, NotNullViolation; directory); - assert_movie!(&db, 8, None, 1, NotNullViolation; relative_media_path); - assert_movie!(&db, 9, None, 1, Success; relative_poster_path); + assert_movie!(&db, 5, None, 1, NotNullViolation; library_id); + assert_movie!(&db, 6, None, 1, NotNullViolation; directory); + assert_movie!(&db, 7, None, 1, NotNullViolation; relative_media_path); + assert_movie!(&db, 8, None, 1, Success; relative_poster_path); } #[tokio::test] @@ -630,7 +719,6 @@ mod tests { assert_eq!(model.id, ShowId::from_raw($id)); assert_eq!(model.parent_id, $pid.map(CollectionId::from_raw)); - assert_eq!(model.slug, concat!("S Slug ", $id).to_string()); assert_eq!(model.library_id, LibraryId::from_raw($lid)); assert_eq!(model.directory, Path::new(concat!("S Directory ", $id)).to_owned().into()); assert_eq!(model.relative_poster_path, noneable!(relative_poster_path, concat!("S Poster ", $id).to_owned() $(, $($skip),+)?)); @@ -645,7 +733,6 @@ mod tests { super::shows::ActiveModel { id: notsettable!(id, ShowId::from_raw($id) $(, $($skip),+)?), parent_id: notsettable!(parent_id, $pid.map(CollectionId::from_raw) $(, $($skip),+)?), - slug: notsettable!(slug, concat!("S Slug ", $id).to_string() $(, $($skip),+)?), library_id: notsettable!(library_id, LibraryId::from_raw($lid) $(, $($skip),+)?), directory: notsettable!(directory, Path::new(concat!("S Directory ", $id)).to_owned().into() $(, $($skip),+)?), relative_poster_path: notsettable!(relative_poster_path, Some(concat!("S Poster ", $id).to_owned()) $(, $($skip),+)?), @@ -674,10 +761,9 @@ mod tests { make_info_show!(&db, 8); assert_show!(&db, 3, None, 1, Success; id); assert_show!(&db, 4, None, 1, Success; parent_id); - assert_show!(&db, 5, None, 1, NotNullViolation; slug); - assert_show!(&db, 6, None, 1, NotNullViolation; library_id); - assert_show!(&db, 7, None, 1, NotNullViolation; directory); - assert_show!(&db, 8, None, 1, Success; relative_poster_path); + assert_show!(&db, 5, None, 1, NotNullViolation; library_id); + assert_show!(&db, 6, None, 1, NotNullViolation; directory); + assert_show!(&db, 7, None, 1, Success; relative_poster_path); } #[tokio::test] @@ -691,7 +777,6 @@ mod tests { assert_eq!(model.show_id, ShowId::from_raw($id)); assert_eq!(model.season_number, ::flix_model::numbers::SeasonNumber::new($season)); - assert_eq!(model.slug, concat!("SS Slug ", $id, ",", $season).to_string()); assert_eq!(model.library_id, LibraryId::from_raw($lid)); assert_eq!(model.directory, Path::new(concat!("SS Directory ", $id, ",", $season)).to_owned().into()); assert_eq!(model.relative_poster_path, noneable!(relative_poster_path, concat!("SS Poster ", $id, ",", $season).to_owned() $(, $($skip),+)?)); @@ -706,7 +791,6 @@ mod tests { super::seasons::ActiveModel { show_id: notsettable!(show_id, ShowId::from_raw($id) $(, $($skip),+)?), season_number: notsettable!(season_number, ::flix_model::numbers::SeasonNumber::new($season) $(, $($skip),+)?), - slug: notsettable!(slug, concat!("SS Slug ", $id, ",", $season).to_string() $(, $($skip),+)?), library_id: notsettable!(library_id, LibraryId::from_raw($lid) $(, $($skip),+)?), directory: notsettable!(directory, Path::new(concat!("SS Directory ", $id, ",", $season)).to_owned().into() $(, $($skip),+)?), relative_poster_path: notsettable!(relative_poster_path, Some(concat!("SS Poster ", $id, ",", $season).to_owned()) $(, $($skip),+)?), @@ -715,7 +799,7 @@ mod tests { } make_content_library!(&db, 1); - make_info_show!(&db, 1); + make_content_show!(&db, 1, 1, None); assert_season!(&db, 1, 1, 1, ForeignKeyViolation); make_info_season!(&db, 1, 1); assert_season!(&db, 1, 1, 1, Success); @@ -729,10 +813,9 @@ mod tests { make_info_season!(&db, 1, 8); assert_season!(&db, 1, 3, 1, NotNullViolation; show_id); assert_season!(&db, 1, 4, 1, NotNullViolation; season_number); - assert_season!(&db, 1, 5, 1, NotNullViolation; slug); - assert_season!(&db, 1, 6, 1, NotNullViolation; library_id); - assert_season!(&db, 1, 7, 1, NotNullViolation; directory); - assert_season!(&db, 1, 8, 1, Success; relative_poster_path); + assert_season!(&db, 1, 5, 1, NotNullViolation; library_id); + assert_season!(&db, 1, 6, 1, NotNullViolation; directory); + assert_season!(&db, 1, 7, 1, Success; relative_poster_path); } #[tokio::test] @@ -747,7 +830,6 @@ mod tests { assert_eq!(model.show_id, ShowId::from_raw($id)); assert_eq!(model.season_number, ::flix_model::numbers::SeasonNumber::new($season)); assert_eq!(model.episode_number, ::flix_model::numbers::EpisodeNumber::new($episode)); - assert_eq!(model.slug, concat!("SS Slug ", $id, ",", $season, $episode).to_string()); assert_eq!(model.library_id, LibraryId::from_raw($lid)); assert_eq!(model.directory, Path::new(concat!("SS Directory ", $id, ",", $season, $episode)).to_owned().into()); assert_eq!(model.relative_media_path, concat!("SS Media ", $id, ",", $season, $episode)); @@ -765,7 +847,6 @@ mod tests { season_number: notsettable!(season_number, ::flix_model::numbers::SeasonNumber::new($season) $(, $($skip),+)?), episode_number: notsettable!(episode_number, ::flix_model::numbers::EpisodeNumber::new($episode) $(, $($skip),+)?), count: notsettable!(count, 0 $(, $($skip),+)?), - slug: notsettable!(slug, concat!("SS Slug ", $id, ",", $season, $episode).to_string() $(, $($skip),+)?), library_id: notsettable!(library_id, LibraryId::from_raw($lid) $(, $($skip),+)?), directory: notsettable!(directory, Path::new(concat!("SS Directory ", $id, ",", $season, $episode)).to_owned().into() $(, $($skip),+)?), relative_media_path: notsettable!(relative_media_path, concat!("SS Media ", $id, ",", $season, $episode).to_owned() $(, $($skip),+)?), @@ -775,8 +856,8 @@ mod tests { } make_content_library!(&db, 1); - make_info_show!(&db, 1); - make_info_season!(&db, 1, 1); + make_content_show!(&db, 1, 1, None); + make_content_season!(&db, 1, 1, 1); assert_episode!(&db, 1, 1, 1, 1, ForeignKeyViolation); make_info_episode!(&db, 1, 1, 1); assert_episode!(&db, 1, 1, 1, 1, Success); @@ -793,10 +874,9 @@ mod tests { assert_episode!(&db, 1, 1, 3, 1, NotNullViolation; show_id); assert_episode!(&db, 1, 1, 4, 1, NotNullViolation; season_number); assert_episode!(&db, 1, 1, 5, 1, NotNullViolation; episode_number); - assert_episode!(&db, 1, 1, 6, 1, NotNullViolation; slug); - assert_episode!(&db, 1, 1, 7, 1, NotNullViolation; library_id); - assert_episode!(&db, 1, 1, 8, 1, NotNullViolation; directory); - assert_episode!(&db, 1, 1, 9, 1, NotNullViolation; relative_media_path); - assert_episode!(&db, 1, 1, 10, 1, Success; relative_poster_path); + assert_episode!(&db, 1, 1, 6, 1, NotNullViolation; library_id); + assert_episode!(&db, 1, 1, 7, 1, NotNullViolation; directory); + assert_episode!(&db, 1, 1, 8, 1, NotNullViolation; relative_media_path); + assert_episode!(&db, 1, 1, 9, 1, Success; relative_poster_path); } } diff --git a/crates/db/src/entity/info.rs b/crates/db/src/entity/info.rs index 433b89f..245f63d 100644 --- a/crates/db/src/entity/info.rs +++ b/crates/db/src/entity/info.rs @@ -13,13 +13,22 @@ pub mod collections { #[sea_orm(table_name = "flix_info_collections")] pub struct Model { /// The collection's ID - #[sea_orm(column_type = "Integer", primary_key, nullable, auto_increment = false)] + #[sea_orm(primary_key, auto_increment = false)] pub id: CollectionId, /// The collection's title - #[sea_orm(indexed)] pub title: String, /// The collection's overview pub overview: String, + + /// The sortable title + #[sea_orm(indexed)] + pub sort_title: String, + /// The filesystem-safe slug + #[sea_orm(indexed, unique)] + pub fs_slug: String, + /// The url-safe slug + #[sea_orm(indexed, unique)] + pub web_slug: String, } impl ActiveModelBehavior for ActiveModel {} @@ -38,10 +47,9 @@ pub mod movies { #[sea_orm(table_name = "flix_info_movies")] pub struct Model { /// The movie's ID - #[sea_orm(column_type = "Integer", primary_key, nullable, auto_increment = false)] + #[sea_orm(primary_key, auto_increment = false)] pub id: MovieId, /// The movie's title - #[sea_orm(indexed)] pub title: String, /// The movie's tagline pub tagline: String, @@ -50,6 +58,16 @@ pub mod movies { /// The movie's release date #[sea_orm(indexed)] pub date: NaiveDate, + + /// The sortable title + #[sea_orm(indexed)] + pub sort_title: String, + /// The filesystem-safe slug + #[sea_orm(indexed, unique)] + pub fs_slug: String, + /// The url-safe slug + #[sea_orm(indexed, unique)] + pub web_slug: String, } impl ActiveModelBehavior for ActiveModel {} @@ -68,10 +86,9 @@ pub mod shows { #[sea_orm(table_name = "flix_info_shows")] pub struct Model { /// The show's ID - #[sea_orm(column_type = "Integer", primary_key, nullable, auto_increment = false)] + #[sea_orm(primary_key, auto_increment = false)] pub id: ShowId, /// The show's title - #[sea_orm(indexed)] pub title: String, /// The show's tagline pub tagline: String, @@ -81,11 +98,21 @@ pub mod shows { #[sea_orm(indexed)] pub date: NaiveDate, + /// The sortable title + #[sea_orm(indexed)] + pub sort_title: String, + /// The filesystem-safe slug + #[sea_orm(indexed, unique)] + pub fs_slug: String, + /// The url-safe slug + #[sea_orm(indexed, unique)] + pub web_slug: String, + /// Seasons that are part of this show - #[sea_orm(has_many, on_update = "Cascade", on_delete = "Cascade")] + #[sea_orm(has_many)] pub seasons: HasMany, /// Episodes that are part of this show - #[sea_orm(has_many, on_update = "Cascade", on_delete = "Cascade")] + #[sea_orm(has_many)] pub episodes: HasMany, } @@ -120,10 +147,16 @@ pub mod seasons { pub date: NaiveDate, /// The show this season belongs to - #[sea_orm(belongs_to, from = "show_id", to = "id")] + #[sea_orm( + belongs_to, + from = "show_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub show: HasOne, /// Episodes that are part of this season - #[sea_orm(has_many, on_update = "Cascade", on_delete = "Cascade")] + #[sea_orm(has_many)] pub episodes: HasMany, } @@ -161,13 +194,21 @@ pub mod episodes { pub date: NaiveDate, /// The show this episode belongs to - #[sea_orm(belongs_to, from = "show_id", to = "id")] + #[sea_orm( + belongs_to, + from = "show_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub show: HasOne, /// The season this episode belongs to #[sea_orm( belongs_to, from = "(show_id, season_number)", - to = "(show_id, season_number)" + to = "(show_id, season_number)", + on_update = "Cascade", + on_delete = "Cascade" )] pub season: HasOne, } @@ -179,11 +220,14 @@ pub mod episodes { #[cfg(test)] pub mod test { macro_rules! make_info_collection { - ($db:expr, $id:literal) => { + ($db:expr, $id:expr) => { $crate::entity::info::collections::ActiveModel { id: Set(::flix_model::id::CollectionId::from_raw($id)), title: Set(::std::string::String::new()), overview: Set(::std::string::String::new()), + sort_title: Set(::std::string::String::new()), + fs_slug: Set(format!("C FS {}", $id)), + web_slug: Set(format!("C Web {}", $id)), } .insert($db) .await @@ -193,13 +237,16 @@ pub mod test { pub(crate) use make_info_collection; macro_rules! make_info_movie { - ($db:expr, $id:literal) => { + ($db:expr, $id:expr) => { $crate::entity::info::movies::ActiveModel { id: Set(::flix_model::id::MovieId::from_raw($id)), title: Set(::std::string::String::new()), tagline: Set(::std::string::String::new()), overview: Set(::std::string::String::new()), date: Set(::chrono::NaiveDate::from_yo_opt(1, 1).expect("from_yo_opt")), + sort_title: Set(::std::string::String::new()), + fs_slug: Set(format!("M FS {}", $id)), + web_slug: Set(format!("M Web {}", $id)), } .insert($db) .await @@ -209,13 +256,16 @@ pub mod test { pub(crate) use make_info_movie; macro_rules! make_info_show { - ($db:expr, $id:literal) => { + ($db:expr, $id:expr) => { $crate::entity::info::shows::ActiveModel { id: Set(::flix_model::id::ShowId::from_raw($id)), title: Set(::std::string::String::new()), tagline: Set(::std::string::String::new()), overview: Set(::std::string::String::new()), date: Set(::chrono::NaiveDate::from_yo_opt(1, 1).expect("from_yo_opt")), + sort_title: Set(::std::string::String::new()), + fs_slug: Set(format!("S FS {}", $id)), + web_slug: Set(format!("S Web {}", $id)), } .insert($db) .await @@ -225,7 +275,7 @@ pub mod test { pub(crate) use make_info_show; macro_rules! make_info_season { - ($db:expr, $show:literal, $season:literal) => { + ($db:expr, $show:expr, $season:expr) => { $crate::entity::info::seasons::ActiveModel { show_id: Set(::flix_model::id::ShowId::from_raw($show)), season_number: Set(::flix_model::numbers::SeasonNumber::new($season)), @@ -241,7 +291,7 @@ pub mod test { pub(crate) use make_info_season; macro_rules! make_info_episode { - ($db:expr, $show:literal, $season:literal, $episode:literal) => { + ($db:expr, $show:expr, $season:expr, $episode:expr) => { $crate::entity::info::episodes::ActiveModel { show_id: Set(::flix_model::id::ShowId::from_raw($show)), season_number: Set(::flix_model::numbers::SeasonNumber::new($season)), @@ -310,6 +360,9 @@ mod tests { id: notsettable!(id, CollectionId::from_raw($id) $(, $($skip),+)?), title: notsettable!(title, concat!("C Title ", $id).to_string() $(, $($skip),+)?), overview: notsettable!(overview, concat!("C Overview ", $id).to_string() $(, $($skip),+)?), + sort_title: notsettable!(sort_title, concat!("C Sort Title ", $id).to_string() $(, $($skip),+)?), + fs_slug: notsettable!(fs_slug, concat!("C FS Slug ", $id).to_string() $(, $($skip),+)?), + web_slug: notsettable!(web_slug, concat!("C Web Slug ", $id).to_string() $(, $($skip),+)?), }.insert($db).await }; } @@ -321,6 +374,9 @@ mod tests { assert_collection!(&db, 3, Success; id); assert_collection!(&db, 4, NotNullViolation; title); assert_collection!(&db, 5, NotNullViolation; overview); + assert_collection!(&db, 6, NotNullViolation; sort_title); + assert_collection!(&db, 7, NotNullViolation; fs_slug); + assert_collection!(&db, 8, NotNullViolation; web_slug); } #[tokio::test] @@ -351,6 +407,9 @@ mod tests { tagline: notsettable!(tagline, concat!("M Tagline ", $id).to_string() $(, $($skip),+)?), overview: notsettable!(overview, concat!("M Overview ", $id).to_string() $(, $($skip),+)?), date: notsettable!(date, NaiveDate::from_yo_opt($id, 1).expect("from_yo_opt") $(, $($skip),+)?), + sort_title: notsettable!(sort_title, concat!("M Sort Title ", $id).to_string() $(, $($skip),+)?), + fs_slug: notsettable!(fs_slug, concat!("M FS Slug ", $id).to_string() $(, $($skip),+)?), + web_slug: notsettable!(web_slug, concat!("M Web Slug ", $id).to_string() $(, $($skip),+)?), }.insert($db).await }; } @@ -364,6 +423,9 @@ mod tests { assert_movie!(&db, 5, NotNullViolation; tagline); assert_movie!(&db, 6, NotNullViolation; overview); assert_movie!(&db, 7, NotNullViolation; date); + assert_movie!(&db, 8, NotNullViolation; sort_title); + assert_movie!(&db, 9, NotNullViolation; fs_slug); + assert_movie!(&db, 10, NotNullViolation; web_slug); } #[tokio::test] @@ -397,6 +459,9 @@ mod tests { tagline: notsettable!(tagline, concat!("S Tagline ", $id).to_string() $(, $($skip),+)?), overview: notsettable!(overview, concat!("S Overview ", $id).to_string() $(, $($skip),+)?), date: notsettable!(date, NaiveDate::from_yo_opt($id, 1).expect("from_yo_opt") $(, $($skip),+)?), + sort_title: notsettable!(sort_title, concat!("S Sort Title ", $id).to_string() $(, $($skip),+)?), + fs_slug: notsettable!(fs_slug, concat!("S FS Slug ", $id).to_string() $(, $($skip),+)?), + web_slug: notsettable!(web_slug, concat!("S Web Slug ", $id).to_string() $(, $($skip),+)?), }.insert($db).await }; } @@ -410,6 +475,9 @@ mod tests { assert_show!(&db, 5, NotNullViolation; tagline); assert_show!(&db, 6, NotNullViolation; overview); assert_show!(&db, 7, NotNullViolation; date); + assert_show!(&db, 8, NotNullViolation; sort_title); + assert_show!(&db, 9, NotNullViolation; fs_slug); + assert_show!(&db, 10, NotNullViolation; web_slug); } #[tokio::test] diff --git a/crates/db/src/entity/mod.rs b/crates/db/src/entity/mod.rs index 6c49fd5..6bdc903 100644 --- a/crates/db/src/entity/mod.rs +++ b/crates/db/src/entity/mod.rs @@ -8,8 +8,28 @@ pub mod watched; #[cfg(test)] mod tests { - pub use sea_orm::DbErr; + use flix_model::numbers::{EpisodeNumber, SeasonNumber}; + #[cfg(feature = "tmdb")] + use flix_tmdb::model::id::TmdbRepr as TmdbReprId; + + use sea_orm::ActiveValue::Set; + use sea_orm::DatabaseConnection; + use sea_orm::DbErr; + use sea_orm::entity::prelude::*; use sea_orm::sqlx::error::ErrorKind; + use seamantic::model::id::SeaOrmRepr as SeaOrmReprId; + + use crate::entity; + use crate::entity::content::test::{ + make_content_collection, make_content_episode, make_content_library, make_content_movie, + make_content_season, make_content_show, + }; + #[cfg(feature = "tmdb")] + use crate::entity::tmdb::test::{ + make_tmdb_collection, make_tmdb_episode, make_tmdb_movie, make_tmdb_season, make_tmdb_show, + }; + use crate::entity::watched::test::{make_watched_episode, make_watched_movie}; + use crate::tests::new_initialized_memory_db; #[derive(Debug)] pub enum ErrorKindError { @@ -70,4 +90,1723 @@ mod tests { }; } pub(super) use noneable; + + struct FlixId { + collection: SeaOrmReprId, + movie: SeaOrmReprId, + show: SeaOrmReprId, + season: u32, + episode: u32, + } + + #[cfg(feature = "tmdb")] + struct TmdbId { + collection: TmdbReprId, + movie: TmdbReprId, + show: TmdbReprId, + season: u32, + episode: u32, + } + + struct ContentId { + library: SeaOrmReprId, + } + + struct WatchId { + user: SeaOrmReprId, + } + + struct DbId { + flix: FlixId, + #[cfg(feature = "tmdb")] + tmdb: TmdbId, + content: ContentId, + watch: WatchId, + } + + impl Default for DbId { + fn default() -> Self { + Self { + flix: FlixId { + collection: 1, + movie: 2, + show: 3, + season: 4, + episode: 5, + }, + #[cfg(feature = "tmdb")] + tmdb: TmdbId { + collection: 6, + movie: 7, + show: 8, + season: 9, + episode: 10, + }, + content: ContentId { library: 11 }, + watch: WatchId { user: 12 }, + } + } + } + + async fn initialize_deletion_test_database(id: &DbId) -> DatabaseConnection { + let db = new_initialized_memory_db().await; + + // Populate the database + make_content_library!(&db, id.content.library); + make_content_collection!(&db, id.content.library, id.flix.collection, None); + make_content_movie!( + &db, + id.content.library, + id.flix.movie, + Some(id.flix.collection) + ); + make_content_show!( + &db, + id.content.library, + id.flix.show, + Some(id.flix.collection) + ); + make_content_season!(&db, id.content.library, id.flix.show, id.flix.season); + make_content_episode!( + &db, + id.content.library, + id.flix.show, + id.flix.season, + id.flix.episode + ); + + #[cfg(feature = "tmdb")] + make_tmdb_collection!(&db, id.tmdb.collection, id.flix.collection); + #[cfg(feature = "tmdb")] + make_tmdb_movie!(&db, id.tmdb.movie, id.flix.movie); + #[cfg(feature = "tmdb")] + make_tmdb_show!(&db, id.tmdb.show, id.flix.show); + #[cfg(feature = "tmdb")] + make_tmdb_season!( + &db, + id.tmdb.show, + id.tmdb.season, + id.flix.show, + id.flix.season + ); + #[cfg(feature = "tmdb")] + make_tmdb_episode!( + &db, + id.tmdb.show, + id.tmdb.season, + id.tmdb.episode, + id.flix.show, + id.flix.season, + id.flix.episode + ); + + make_watched_movie!(&db, id.flix.movie, id.watch.user); + make_watched_episode!( + &db, + id.flix.show, + id.flix.season, + id.flix.episode, + id.watch.user + ); + + // Check that everything has one entry + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + + db + } + + #[tokio::test] + async fn test_delete_info_collection() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::info::collections::Entity::delete_by_id(seamantic::model::id::Id::from_raw( + id.flix.collection, + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(0), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(0), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_info_movie() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::info::movies::Entity::delete_by_id(seamantic::model::id::Id::from_raw( + id.flix.movie, + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(0), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(0), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(0), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(0), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_info_show() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::info::shows::Entity::delete_by_id(seamantic::model::id::Id::from_raw(id.flix.show)) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(0), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(0), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(0), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_info_season() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::info::seasons::Entity::delete_by_id(( + seamantic::model::id::Id::from_raw(id.flix.show), + SeasonNumber::new(id.flix.season), + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(0), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_info_episodes() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::info::episodes::Entity::delete_by_id(( + seamantic::model::id::Id::from_raw(id.flix.show), + SeasonNumber::new(id.flix.season), + EpisodeNumber::new(id.flix.episode), + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[cfg(feature = "tmdb")] + #[tokio::test] + async fn test_delete_tmdb_collection() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::tmdb::collections::Entity::delete_by_id(flix_tmdb::model::id::Id::from_raw( + id.tmdb.collection, + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[cfg(feature = "tmdb")] + #[tokio::test] + async fn test_delete_tmdb_movie() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::tmdb::movies::Entity::delete_by_id(flix_tmdb::model::id::Id::from_raw( + id.tmdb.movie, + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(0), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[cfg(feature = "tmdb")] + #[tokio::test] + async fn test_delete_tmdb_show() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::tmdb::shows::Entity::delete_by_id(flix_tmdb::model::id::Id::from_raw(id.tmdb.show)) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(0), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[cfg(feature = "tmdb")] + #[tokio::test] + async fn test_delete_tmdb_season() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::tmdb::seasons::Entity::delete_by_id(( + flix_tmdb::model::id::Id::from_raw(id.tmdb.show), + SeasonNumber::new(id.tmdb.season), + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[cfg(feature = "tmdb")] + #[tokio::test] + async fn test_delete_tmdb_episode() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::tmdb::episodes::Entity::delete_by_id(( + flix_tmdb::model::id::Id::from_raw(id.tmdb.show), + SeasonNumber::new(id.tmdb.season), + EpisodeNumber::new(id.tmdb.episode), + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(0), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_content_library() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::content::libraries::Entity::delete_by_id(seamantic::model::id::Id::from_raw( + id.content.library, + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(0), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(0), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_content_collection() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::content::collections::Entity::delete_by_id(seamantic::model::id::Id::from_raw( + id.flix.collection, + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(0), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_content_movie() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::content::movies::Entity::delete_by_id(seamantic::model::id::Id::from_raw( + id.flix.movie, + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(0), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_content_show() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::content::shows::Entity::delete_by_id(seamantic::model::id::Id::from_raw( + id.flix.show, + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_content_season() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::content::seasons::Entity::delete_by_id(( + seamantic::model::id::Id::from_raw(id.flix.show), + SeasonNumber::new(id.flix.season), + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_content_episode() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::content::episodes::Entity::delete_by_id(( + seamantic::model::id::Id::from_raw(id.flix.show), + SeasonNumber::new(id.flix.season), + EpisodeNumber::new(id.flix.episode), + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_watched_movie() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::watched::movies::Entity::delete_by_id(( + seamantic::model::id::Id::from_raw(id.flix.movie), + id.watch.user, + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(0), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::watched::episodes::Entity::find().count(&db).await + ); + } + + #[tokio::test] + async fn test_delete_watched_episode() { + let id = DbId::default(); + let db = initialize_deletion_test_database(&id).await; + + entity::watched::episodes::Entity::delete_by_id(( + seamantic::model::id::Id::from_raw(id.flix.show), + SeasonNumber::new(id.flix.season), + EpisodeNumber::new(id.flix.episode), + id.watch.user, + )) + .exec(&db) + .await + .expect("Entity::delete_by_id"); + + assert_eq!( + Ok(1), + entity::info::collections::Entity::find().count(&db).await + ); + assert_eq!(Ok(1), entity::info::movies::Entity::find().count(&db).await); + assert_eq!(Ok(1), entity::info::shows::Entity::find().count(&db).await); + assert_eq!( + Ok(1), + entity::info::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::info::episodes::Entity::find().count(&db).await + ); + + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::collections::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::movies::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!(Ok(1), entity::tmdb::shows::Entity::find().count(&db).await); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::seasons::Entity::find().count(&db).await + ); + #[cfg(feature = "tmdb")] + assert_eq!( + Ok(1), + entity::tmdb::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::content::libraries::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::collections::Entity::find() + .count(&db) + .await + ); + assert_eq!( + Ok(1), + entity::content::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::shows::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::seasons::Entity::find().count(&db).await + ); + assert_eq!( + Ok(1), + entity::content::episodes::Entity::find().count(&db).await + ); + + assert_eq!( + Ok(1), + entity::watched::movies::Entity::find().count(&db).await + ); + assert_eq!( + Ok(0), + entity::watched::episodes::Entity::find().count(&db).await + ); + } } diff --git a/crates/db/src/entity/tmdb.rs b/crates/db/src/entity/tmdb.rs index 13eb927..d562780 100644 --- a/crates/db/src/entity/tmdb.rs +++ b/crates/db/src/entity/tmdb.rs @@ -16,7 +16,7 @@ pub mod collections { #[sea_orm(table_name = "flix_tmdb_collections")] pub struct Model { /// The collection's TMDB ID - #[sea_orm(column_type = "Integer", primary_key, nullable, auto_increment = false)] + #[sea_orm(primary_key, auto_increment = false)] pub tmdb_id: CollectionId, /// The collection's ID #[sea_orm(unique)] @@ -26,12 +26,19 @@ pub mod collections { /// The number of movies in the collection pub movie_count: u16, - /// Movies that are in this collection - #[sea_orm(has_many, on_update = "Cascade", on_delete = "Cascade")] - pub movies: HasMany, /// The info for this collection - #[sea_orm(belongs_to, from = "flix_id", to = "id")] + #[sea_orm( + belongs_to, + from = "flix_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub info: HasOne, + + /// Movies that are in this collection + #[sea_orm(has_many)] + pub movies: HasMany, } impl ActiveModelBehavior for ActiveModel {} @@ -55,7 +62,7 @@ pub mod movies { #[sea_orm(table_name = "flix_tmdb_movies")] pub struct Model { /// The movie's TMDB ID - #[sea_orm(column_type = "Integer", primary_key, nullable, auto_increment = false)] + #[sea_orm(primary_key, auto_increment = false)] pub tmdb_id: MovieId, /// The movie's ID #[sea_orm(unique)] @@ -68,11 +75,23 @@ pub mod movies { #[sea_orm(indexed)] pub collection_id: Option, - /// The info for this collection - #[sea_orm(belongs_to, from = "collection_id", to = "tmdb_id")] + /// The collection this movie belongs to + #[sea_orm( + belongs_to, + from = "collection_id", + to = "tmdb_id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub collection: HasOne, /// The info for this movie - #[sea_orm(belongs_to, from = "flix_id", to = "id")] + #[sea_orm( + belongs_to, + from = "flix_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub info: HasOne, } @@ -95,7 +114,7 @@ pub mod shows { #[sea_orm(table_name = "flix_tmdb_shows")] pub struct Model { /// The show's TMDB ID - #[sea_orm(column_type = "Integer", primary_key, nullable, auto_increment = false)] + #[sea_orm(primary_key, auto_increment = false)] pub tmdb_id: ShowId, /// The show's ID #[sea_orm(unique)] @@ -105,15 +124,22 @@ pub mod shows { /// The number of seasons the show has pub number_of_seasons: u32, + /// The info for this show + #[sea_orm( + belongs_to, + from = "flix_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] + pub info: HasOne, + /// Seasons that are part of this show - #[sea_orm(has_many, on_update = "Cascade", on_delete = "Cascade")] + #[sea_orm(has_many)] pub seasons: HasMany, /// Episodes that are part of this show - #[sea_orm(has_many, on_update = "Cascade", on_delete = "Cascade")] + #[sea_orm(has_many)] pub episodes: HasMany, - /// The info for this show - #[sea_orm(belongs_to, from = "flix_id", to = "id")] - pub info: HasOne, } impl ActiveModelBehavior for ActiveModel {} @@ -151,15 +177,27 @@ pub mod seasons { pub last_update: DateTime, /// The show this season belongs to - #[sea_orm(belongs_to, from = "tmdb_show", to = "tmdb_id")] + #[sea_orm( + belongs_to, + from = "tmdb_show", + to = "tmdb_id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub show: HasOne, /// The info for this season #[sea_orm( belongs_to, from = "(flix_show, flix_season)", - to = "(show_id, season_number)" + to = "(show_id, season_number)", + on_update = "Cascade", + on_delete = "Cascade" )] pub info: HasOne, + + /// Episodes that are part of this season + #[sea_orm(has_many)] + pub episodes: HasMany, } impl ActiveModelBehavior for ActiveModel {} @@ -206,20 +244,30 @@ pub mod episodes { pub runtime: Seconds, /// The show this episode belongs to - #[sea_orm(belongs_to, from = "tmdb_show", to = "tmdb_id")] + #[sea_orm( + belongs_to, + from = "tmdb_show", + to = "tmdb_id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub show: HasOne, /// The season this episode belongs to #[sea_orm( belongs_to, from = "(tmdb_show, tmdb_season)", - to = "(tmdb_show, tmdb_season)" + to = "(tmdb_show, tmdb_season)", + on_update = "Cascade", + on_delete = "Cascade" )] pub season: HasOne, /// The info for this episode #[sea_orm( belongs_to, from = "(flix_show, flix_season, flix_episode)", - to = "(show_id, season_number, episode_number)" + to = "(show_id, season_number, episode_number)", + on_update = "Cascade", + on_delete = "Cascade" )] pub info: HasOne, } @@ -231,7 +279,7 @@ pub mod episodes { #[cfg(test)] pub mod test { macro_rules! make_tmdb_collection { - ($db:expr, $id:literal, $flix_id:literal) => { + ($db:expr, $id:expr, $flix_id:expr) => { $crate::entity::tmdb::collections::ActiveModel { tmdb_id: Set(::flix_tmdb::model::id::CollectionId::from_raw($id)), flix_id: Set(::flix_model::id::CollectionId::from_raw($flix_id)), @@ -246,7 +294,7 @@ pub mod test { pub(crate) use make_tmdb_collection; macro_rules! make_tmdb_movie { - ($db:expr, $id:literal, $flix_id:literal) => { + ($db:expr, $id:expr, $flix_id:expr) => { $crate::entity::tmdb::movies::ActiveModel { tmdb_id: Set(::flix_tmdb::model::id::MovieId::from_raw($id)), flix_id: Set(::flix_model::id::MovieId::from_raw($flix_id)), @@ -262,7 +310,7 @@ pub mod test { pub(crate) use make_tmdb_movie; macro_rules! make_tmdb_show { - ($db:expr, $id:literal, $flix_id:literal) => { + ($db:expr, $id:expr, $flix_id:expr) => { $crate::entity::tmdb::shows::ActiveModel { tmdb_id: Set(::flix_tmdb::model::id::ShowId::from_raw($id)), flix_id: Set(::flix_model::id::ShowId::from_raw($flix_id)), @@ -277,7 +325,7 @@ pub mod test { pub(crate) use make_tmdb_show; macro_rules! make_tmdb_season { - ($db:expr, $show:literal, $season:literal, $flix_show:literal, $flix_season:literal) => { + ($db:expr, $show:expr, $season:expr, $flix_show:expr, $flix_season:expr) => { $crate::entity::tmdb::seasons::ActiveModel { tmdb_show: Set(::flix_tmdb::model::id::ShowId::from_raw($show)), tmdb_season: Set(::flix_model::numbers::SeasonNumber::new($season)), @@ -293,7 +341,7 @@ pub mod test { pub(crate) use make_tmdb_season; macro_rules! make_tmdb_episode { - ($db:expr, $show:literal, $season:literal, $episode:literal, $flix_show:literal, $flix_season:literal, $flix_episode:literal) => { + ($db:expr, $show:expr, $season:expr, $episode:expr, $flix_show:expr, $flix_season:expr, $flix_episode:expr) => { $crate::entity::tmdb::episodes::ActiveModel { tmdb_show: Set(::flix_tmdb::model::id::ShowId::from_raw($show)), tmdb_season: Set(::flix_model::numbers::SeasonNumber::new($season)), diff --git a/crates/db/src/entity/watched.rs b/crates/db/src/entity/watched.rs index f97dfc1..c64bafe 100644 --- a/crates/db/src/entity/watched.rs +++ b/crates/db/src/entity/watched.rs @@ -24,7 +24,14 @@ pub mod collections { pub watched_date: DateTime, /// The info for this collection - #[sea_orm(belongs_to, relation_enum = "Info", from = "id", to = "id")] + #[sea_orm( + belongs_to, + relation_enum = "Info", + from = "id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub info: HasOne, /// The content for this collection #[sea_orm(belongs_to, relation_enum = "Content", from = "id", to = "id", skip_fk)] @@ -58,7 +65,13 @@ pub mod movies { pub watched_date: DateTime, /// The info for this movie - #[sea_orm(belongs_to, from = "id", to = "id")] + #[sea_orm( + belongs_to, + from = "id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub info: HasOne, /// The content for this movie #[sea_orm(belongs_to, relation_enum = "Content", from = "id", to = "id", skip_fk)] @@ -92,7 +105,14 @@ pub mod shows { pub watched_date: DateTime, /// The info for this show - #[sea_orm(belongs_to, relation_enum = "Info", from = "id", to = "id")] + #[sea_orm( + belongs_to, + relation_enum = "Info", + from = "id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] pub info: HasOne, /// The content for this show #[sea_orm(belongs_to, relation_enum = "Content", from = "id", to = "id", skip_fk)] @@ -134,7 +154,9 @@ pub mod seasons { belongs_to, relation_enum = "Info", from = "(show_id, season_number)", - to = "(show_id, season_number)" + to = "(show_id, season_number)", + on_update = "Cascade", + on_delete = "Cascade" )] pub info: HasOne, /// The content for this season @@ -186,7 +208,9 @@ pub mod episodes { belongs_to, relation_enum = "Info", from = "(show_id, season_number, episode_number)", - to = "(show_id, season_number, episode_number)" + to = "(show_id, season_number, episode_number)", + on_update = "Cascade", + on_delete = "Cascade" )] pub info: HasOne, /// The content for this episode @@ -207,7 +231,7 @@ pub mod episodes { #[cfg(test)] pub mod test { macro_rules! make_watched_movie { - ($db:expr, $id:literal, $user:literal) => { + ($db:expr, $id:expr, $user:expr) => { $crate::entity::watched::movies::ActiveModel { id: Set(::flix_model::id::MovieId::from_raw($id)), user_id: Set($user), @@ -221,7 +245,7 @@ pub mod test { pub(crate) use make_watched_movie; macro_rules! make_watched_episode { - ($db:expr, $show:literal, $season:literal, $episode:literal, $user:literal) => { + ($db:expr, $show:expr, $season:expr, $episode:expr, $user:expr) => { $crate::entity::watched::episodes::ActiveModel { show_id: Set(::flix_model::id::ShowId::from_raw($show)), season_number: Set(::flix_model::numbers::SeasonNumber::new($season)), diff --git a/crates/model/Cargo.toml b/crates/model/Cargo.toml index 1651b4d..49681b5 100644 --- a/crates/model/Cargo.toml +++ b/crates/model/Cargo.toml @@ -13,6 +13,7 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [dependencies] +itertools = { workspace = true } seamantic = { workspace = true } serde = { workspace = true, features = ["derive", "std"], optional = true } thiserror = { workspace = true } diff --git a/crates/model/src/lib.rs b/crates/model/src/lib.rs index 4d390e8..7fbd2df 100644 --- a/crates/model/src/lib.rs +++ b/crates/model/src/lib.rs @@ -4,3 +4,4 @@ pub mod id; pub mod numbers; +pub mod text; diff --git a/crates/model/src/text.rs b/crates/model/src/text.rs new file mode 100644 index 0000000..c3939a5 --- /dev/null +++ b/crates/model/src/text.rs @@ -0,0 +1,165 @@ +//! This module contains helper functions for normalizing media titles + +use core::iter::Peekable; + +use itertools::Itertools; + +/// # Panics +/// +/// Panics if `input` is not ASCII. +fn split_normalized_words(input: &str) -> impl Iterator { + if !input.is_ascii() { + panic!("Input is not ASCII: {input}"); + } + + input.split_ascii_whitespace().map(|s| { + let chars = s + .chars() + .filter(|c| c.is_ascii_alphanumeric() || *c == '-') + .map(|c| c.to_ascii_lowercase()); + + if s.len() > 4 + && s.len().is_multiple_of(2) + && chars.clone().tuples().all(|(l, r)| l != '.' && r == '.') + { + // Collapse acronym + chars.tuples().map(|(l, _)| l).collect() + } else { + chars.collect() + } + }) +} + +fn split_leading_article>(iter: I) -> (Option, Peekable) { + let mut iter = iter.peekable(); + match iter.peek().map(String::as_str) { + Some("a" | "an" | "the") => (iter.next(), iter), + _ => (None, iter), + } +} + +/// Convert a media title to be sortable and searchable +/// +/// use flix_model::text::make_sortable_title; +/// +/// assert_eq!(make_sortable_title("The Matrix"), "matrix, the"); +/// assert_eq!(make_sortable_title("Marvel's Agents of S.H.I.E.L.D."), "marvels agents of shield"); +/// assert_eq!(make_sortable_title("Avatar: The Last Airbender"), "avatar the last airbender"); +/// +/// # Panics +/// +/// Panics if `input` is not ASCII. +pub fn make_sortable_title(title: &str) -> String { + let words = split_normalized_words(title); + let (article, words) = split_leading_article(words); + + let output = Itertools::intersperse(words, " ".to_string()); + if let Some(article) = article { + output.chain([", ".to_string(), article]).collect() + } else { + output.collect() + } +} + +/// Convert a media title to a folder name representable on filesystems +/// +/// use flix_model::text::make_fs_slug; +/// +/// assert_eq!(make_fs_slug("The Matrix"), "matrix"); +/// assert_eq!(make_fs_slug("Marvel's Agents of S.H.I.E.L.D."), "marvels agents of shield"); +/// assert_eq!(make_fs_slug("Avatar: The Last Airbender"), "avatar the last airbender"); +/// +/// # Panics +/// +/// Panics if `input` is not ASCII. +pub fn make_fs_slug(title: &str) -> String { + let words = split_normalized_words(title); + let (_, words) = split_leading_article(words); + + Itertools::intersperse(words, " ".to_string()).collect() +} + +/// Convert a media title and year to a folder name representable on filesystems +/// +/// use flix_model::text::make_fs_slug_year; +/// +/// assert_eq!(make_fs_slug_year("The Matrix", 1999), "matrix (1999)"); +/// assert_eq!(make_fs_slug_year("Marvel's Agents of S.H.I.E.L.D.", 2013), "marvels agents of shield (2013)"); +/// assert_eq!(make_fs_slug_year("Avatar: The Last Airbender", 2005), "avatar the last airbender (2005)"); +/// +/// # Panics +/// +/// Panics if `input` is not ASCII. +pub fn make_fs_slug_year(title: &str, year: i32) -> String { + let words = split_normalized_words(title); + let (_, words) = split_leading_article(words); + + Itertools::intersperse(words, " ".to_string()) + .chain([format!(" ({year})")]) + .collect() +} + +/// Normalize a filesystem name +/// +/// use flix_model::text::normalize_fs_name; +/// +/// assert_eq!(normalize_fs_name("Matrix (1999)"), "matrix (1999)"); +/// assert_eq!(normalize_fs_name("Marvel's Agents of SHIELD (2013)"), "marvels agents of shield (2013)"); +/// assert_eq!(normalize_fs_name("Avatar The Last Airbender (2005)"), "avatar the last airbender (2005)"); +pub fn normalize_fs_name(input: &str) -> String { + let chars = input.split_ascii_whitespace().map(|s| { + let chars = s + .chars() + .filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '(' || *c == ')') + .map(|c| c.to_ascii_lowercase()); + + if s.len() > 4 + && s.len().is_multiple_of(2) + && chars.clone().tuples().all(|(l, r)| l != '.' && r == '.') + { + // Collapse acronym + chars.tuples().map(|(l, _)| l).collect() + } else { + chars.collect() + } + }); + Itertools::intersperse(chars, " ".to_string()).collect() +} + +/// Convert a media title to a url compatible string +/// +/// use flix_model::text::make_web_slug; +/// +/// assert_eq!(make_web_slug("The Matrix"), "matrix"); +/// assert_eq!(make_web_slug("Marvel's Agents of S.H.I.E.L.D."), "marvels-agents-of-shield"); +/// assert_eq!(make_web_slug("Avatar: The Last Airbender"), "avatar-the-last-airbender"); +/// +/// # Panics +/// +/// Panics if `input` is not ASCII. +pub fn make_web_slug(title: &str) -> String { + let words = split_normalized_words(title); + let (_, words) = split_leading_article(words); + + Itertools::intersperse(words, "-".to_string()).collect() +} + +/// Convert a media title and year to a url compatible string +/// +/// use flix_model::text::make_web_slug_year; +/// +/// assert_eq!(make_web_slug_year("The Matrix", 1999), "matrix-1999"); +/// assert_eq!(make_web_slug_year("Marvel's Agents of S.H.I.E.L.D.", 2013), "marvels-agents-of-shield-2013"); +/// assert_eq!(make_web_slug_year("Avatar: The Last Airbender", 2005), "avatar-the-last-airbender-2005"); +/// +/// # Panics +/// +/// Panics if `input` is not ASCII. +pub fn make_web_slug_year(title: &str, year: i32) -> String { + let words = split_normalized_words(title); + let (_, words) = split_leading_article(words); + + Itertools::intersperse(words, "-".to_string()) + .chain([format!("-{year}")]) + .collect() +}