mecomp_daemon/services/
mod.rs1use log::warn;
2use mecomp_storage::{
3 db::schemas::{
4 RecordId,
5 album::{Album, TABLE_NAME as ALBUM_TABLE_NAME},
6 artist::{Artist, TABLE_NAME as ARTIST_TABLE_NAME},
7 collection::{Collection, TABLE_NAME as COLLECTION_TABLE_NAME},
8 dynamic::{DynamicPlaylist, TABLE_NAME as DYNAMIC_PLAYLIST_TABLE_NAME},
9 playlist::{Playlist, TABLE_NAME as PLAYLIST_TABLE_NAME},
10 song::{Song, TABLE_NAME as SONG_TABLE_NAME},
11 },
12 errors::{Error, StorageResult},
13};
14use one_or_many::OneOrMany;
15use surrealdb::{Connection, Surreal};
16
17pub mod backup;
18pub mod library;
19pub mod radio;
20
21#[inline]
31pub(crate) async fn get_songs_from_things<C: Connection>(
32 db: &Surreal<C>,
33 things: &[RecordId],
34) -> StorageResult<OneOrMany<Song>> {
35 let mut songs: OneOrMany<Song> = OneOrMany::None;
37 for thing in things {
38 let thing = thing.clone();
39 match thing.tb.as_str() {
40 ALBUM_TABLE_NAME => songs.extend(Album::read_songs(db, thing.into()).await?),
41 ARTIST_TABLE_NAME => songs.extend(Artist::read_songs(db, thing.into()).await?),
42 COLLECTION_TABLE_NAME => songs.extend(Collection::read_songs(db, thing.into()).await?),
43 PLAYLIST_TABLE_NAME => songs.extend(Playlist::read_songs(db, thing.into()).await?),
44 SONG_TABLE_NAME => {
45 songs.push(Song::read(db, thing.into()).await?.ok_or(Error::NotFound)?);
46 }
47 DYNAMIC_PLAYLIST_TABLE_NAME => {
48 if let Some(new_songs) = DynamicPlaylist::run_query_by_id(db, thing.into()).await? {
49 songs.extend(new_songs);
50 }
51 }
52 _ => warn!("Unknown thing type: {}", thing.tb),
53 }
54 }
55
56 songs.dedup_by_key(|song| song.id.clone());
58
59 Ok(songs)
60}