From 1f8a733cdb2523a0cc50797174c03632f55fe8c9 Mon Sep 17 00:00:00 2001 From: Luca Date: Sat, 9 Dec 2023 16:11:03 +0100 Subject: [PATCH] populate genres when finalizing import --- entity/src/full.rs | 32 ++++++++++++++++++--------- server/src/tasks/import/populate.rs | 34 ++++++++++++++++++++++++++--- server/src/tasks/import/track.rs | 25 +++++++++++++++++++-- 3 files changed, 76 insertions(+), 15 deletions(-) diff --git a/entity/src/full.rs b/entity/src/full.rs index 082a552..05fa276 100644 --- a/entity/src/full.rs +++ b/entity/src/full.rs @@ -23,6 +23,15 @@ impl FullRelease { } } + pub fn get_release_genres(&self) -> Vec<&GenreRelease> { + self.import + .release_genres + .0 + .iter() + .filter(|rel| rel.release_id == self.release) + .collect() + } + pub fn get_artist_credits_release(&self) -> Vec<&ArtistCreditRelease> { self.import .artist_credit_releases @@ -123,6 +132,15 @@ impl FullTrack { .collect() } + pub fn get_track_genres(&self) -> Vec<&GenreTrack> { + self.import + .track_genres + .0 + .iter() + .filter(|rel| rel.track_id == self.track) + .collect() + } + pub fn get_relations(&self) -> Vec<&ArtistTrackRelation> { self.import .artist_track_relations @@ -259,12 +277,9 @@ pub trait GenreInfo { impl GenreInfo for FullTrack { fn get_genres(&self) -> Result> { let genre_ids: HashSet = self - .import - .track_genres - .0 + .get_track_genres() .iter() - .filter(|rel| rel.track_id == self.track) - .map(|rel| rel.genre_id.to_owned()) + .map(|tg| tg.genre_id.to_owned()) .collect(); Ok(self .import @@ -279,12 +294,9 @@ impl GenreInfo for FullTrack { impl GenreInfo for FullRelease { fn get_genres(&self) -> Result> { let genre_ids: HashSet = self - .import - .release_genres - .0 + .get_release_genres() .iter() - .filter(|rel| rel.release_id == self.release) - .map(|rel| rel.genre_id.to_owned()) + .map(|tg| tg.genre_id.to_owned()) .collect(); Ok(self .import diff --git a/server/src/tasks/import/populate.rs b/server/src/tasks/import/populate.rs index c14fbb0..2ad4aa6 100644 --- a/server/src/tasks/import/populate.rs +++ b/server/src/tasks/import/populate.rs @@ -24,10 +24,11 @@ use base::{ }; use entity::{ conflict::{ - ARTIST_CONFLICT, ARTIST_CREDIT_CONFLICT, ARTIST_CREDIT_RELEASE_CONFLICT, IMAGE_CONFLICT_1, - IMAGE_CONFLICT_2, IMAGE_RELEASE_CONFLICT, MEDIUM_CONFLICT, RELEASE_CONFLICT, + ARTIST_CONFLICT, ARTIST_CREDIT_CONFLICT, ARTIST_CREDIT_RELEASE_CONFLICT, GENRE_CONFLICT, + GENRE_RELEASE_CONFLICT, IMAGE_CONFLICT_1, IMAGE_CONFLICT_2, IMAGE_RELEASE_CONFLICT, + MEDIUM_CONFLICT, RELEASE_CONFLICT, }, - full::{ArtistInfo, GetArtistCredits}, + full::{ArtistInfo, GenreInfo, GetArtistCredits}, IgnoreNone, }; use tag::{sanitize_map, tag_to_string_map, tags_from_full_release, PictureType}; @@ -164,6 +165,33 @@ impl crate::tasks::TaskTrait for Data { release_root ))?; + // save genres + let genres: Vec = full_release.get_genres()?.into_iter().cloned().collect(); + let genres: Vec<_> = genres.into_iter().map(|a| a.into_active_model()).collect(); + if !genres.is_empty() { + entity::GenreEntity::insert_many(genres) + .on_conflict(GENRE_CONFLICT.to_owned()) + .exec(&tx) + .await + .ignore_none()?; + } + let genre_releases: Vec = full_release + .get_release_genres() + .into_iter() + .cloned() + .collect(); + let genre_releases: Vec<_> = genre_releases + .into_iter() + .map(|a| a.into_active_model()) + .collect(); + if !genre_releases.is_empty() { + entity::GenreReleaseEntity::insert_many(genre_releases) + .on_conflict(GENRE_RELEASE_CONFLICT.to_owned()) + .exec(&tx) + .await + .ignore_none()?; + } + // Save the image if available let cover = if let Some(cover_i) = import_rc.selected_cover { let cover = import_rc.covers.0.get(cover_i as usize).ok_or(eyre!( diff --git a/server/src/tasks/import/track.rs b/server/src/tasks/import/track.rs index 1f4ae5d..32280a4 100644 --- a/server/src/tasks/import/track.rs +++ b/server/src/tasks/import/track.rs @@ -14,9 +14,9 @@ use base::{ use entity::{ conflict::{ ARTIST_CONFLICT, ARTIST_CREDIT_CONFLICT, ARTIST_CREDIT_TRACK_CONFLICT, - ARTIST_TRACK_RELATION_CONFLICT, TRACK_CONFLICT, + ARTIST_TRACK_RELATION_CONFLICT, GENRE_CONFLICT, GENRE_TRACK_CONFLICT, TRACK_CONFLICT, }, - full::{ArtistInfo, GetArtistCredits}, + full::{ArtistInfo, GenreInfo, GetArtistCredits}, IgnoreNone, }; use tag::{sanitize_map, tag_to_string_map, tags_from_combination, Picture, PictureType}; @@ -174,6 +174,27 @@ impl crate::tasks::TaskTrait for Data { .await .ignore_none()?; } + let genres = dedup(full_track.get_genres()?.into_iter().cloned().collect()); + let genres: Vec<_> = genres.into_iter().map(|a| a.into_active_model()).collect(); + if !genres.is_empty() { + entity::GenreEntity::insert_many(genres) + .on_conflict(GENRE_CONFLICT.to_owned()) + .exec(&tx) + .await + .ignore_none()?; + } + let genre_tracks = dedup(full_track.get_track_genres().into_iter().cloned().collect()); + let genre_tracks: Vec<_> = genre_tracks + .into_iter() + .map(|a| a.into_active_model()) + .collect(); + if !genre_tracks.is_empty() { + entity::GenreTrackEntity::insert_many(genre_tracks) + .on_conflict(GENRE_TRACK_CONFLICT.to_owned()) + .exec(&tx) + .await + .ignore_none()?; + } Ok(tx.commit().await?) } }