Skip to content

Commit

Permalink
populate genres when finalizing import
Browse files Browse the repository at this point in the history
  • Loading branch information
lucat1 committed Dec 9, 2023
1 parent ba5fb1f commit 1f8a733
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
32 changes: 22 additions & 10 deletions entity/src/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -259,12 +277,9 @@ pub trait GenreInfo {
impl GenreInfo for FullTrack {
fn get_genres(&self) -> Result<Vec<&Genre>> {
let genre_ids: HashSet<String> = 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
Expand All @@ -279,12 +294,9 @@ impl GenreInfo for FullTrack {
impl GenreInfo for FullRelease {
fn get_genres(&self) -> Result<Vec<&Genre>> {
let genre_ids: HashSet<String> = 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
Expand Down
34 changes: 31 additions & 3 deletions server/src/tasks/import/populate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -164,6 +165,33 @@ impl crate::tasks::TaskTrait for Data {
release_root
))?;

// save genres
let genres: Vec<entity::Genre> = 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<entity::GenreRelease> = 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!(
Expand Down
25 changes: 23 additions & 2 deletions server/src/tasks/import/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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?)
}
}

0 comments on commit 1f8a733

Please sign in to comment.