mecomp_daemon/services/
mod.rs

1use 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;
19#[cfg(feature = "analysis")]
20pub mod radio;
21
22/// Get the songs associated with every thing in the list.
23///
24/// This function will go through the list of things and get the songs associated with each thing.
25///
26/// It will then remove duplicates from the list of songs.
27///
28/// # Errors
29///
30/// This function will return an error if there is an issue reading the songs from the database.
31#[inline]
32pub async fn get_songs_from_things<C: Connection>(
33    db: &Surreal<C>,
34    things: &[RecordId],
35) -> StorageResult<OneOrMany<Song>> {
36    // go through the list, and get songs for each thing (depending on what it is)
37    let mut songs: OneOrMany<Song> = OneOrMany::None;
38    for thing in things {
39        let thing = thing.clone();
40        match thing.tb.as_str() {
41            ALBUM_TABLE_NAME => songs.extend(Album::read_songs(db, thing.into()).await?),
42            ARTIST_TABLE_NAME => songs.extend(Artist::read_songs(db, thing.into()).await?),
43            COLLECTION_TABLE_NAME => songs.extend(Collection::read_songs(db, thing.into()).await?),
44            PLAYLIST_TABLE_NAME => songs.extend(Playlist::read_songs(db, thing.into()).await?),
45            SONG_TABLE_NAME => {
46                songs.push(Song::read(db, thing.into()).await?.ok_or(Error::NotFound)?);
47            }
48            DYNAMIC_PLAYLIST_TABLE_NAME => {
49                if let Some(new_songs) = DynamicPlaylist::run_query_by_id(db, thing.into()).await? {
50                    songs.extend(new_songs);
51                }
52            }
53            _ => warn!("Unknown thing type: {}", thing.tb),
54        }
55    }
56
57    // remove duplicates
58    songs.dedup_by_key(|song| song.id.clone());
59
60    Ok(songs)
61}