use log::warn;
use mecomp_storage::{
db::schemas::{
RecordId,
album::{Album, TABLE_NAME as ALBUM_TABLE_NAME},
artist::{Artist, TABLE_NAME as ARTIST_TABLE_NAME},
collection::{Collection, TABLE_NAME as COLLECTION_TABLE_NAME},
dynamic::{DynamicPlaylist, TABLE_NAME as DYNAMIC_PLAYLIST_TABLE_NAME},
playlist::{Playlist, TABLE_NAME as PLAYLIST_TABLE_NAME},
song::{Song, TABLE_NAME as SONG_TABLE_NAME},
},
errors::{Error, StorageResult},
};
use one_or_many::OneOrMany;
use surrealdb::{Connection, Surreal};
pub mod backup;
pub mod library;
pub mod radio;
#[inline]
pub(crate) async fn get_songs_from_things<C: Connection>(
db: &Surreal<C>,
things: &[RecordId],
) -> StorageResult<OneOrMany<Song>> {
let mut songs: OneOrMany<Song> = OneOrMany::None;
for thing in things {
let thing = thing.clone();
match thing.tb.as_str() {
ALBUM_TABLE_NAME => songs.extend(Album::read_songs(db, thing.into()).await?),
ARTIST_TABLE_NAME => songs.extend(Artist::read_songs(db, thing.into()).await?),
COLLECTION_TABLE_NAME => songs.extend(Collection::read_songs(db, thing.into()).await?),
PLAYLIST_TABLE_NAME => songs.extend(Playlist::read_songs(db, thing.into()).await?),
SONG_TABLE_NAME => {
songs.push(Song::read(db, thing.into()).await?.ok_or(Error::NotFound)?);
}
DYNAMIC_PLAYLIST_TABLE_NAME => {
if let Some(new_songs) = DynamicPlaylist::run_query_by_id(db, thing.into()).await? {
songs.extend(new_songs);
}
}
_ => warn!("Unknown thing type: {}", thing.tb),
}
}
songs.dedup_by_key(|song| song.id.clone());
Ok(songs)
}