mecomp_daemon/services/
mod.rs

1use log::warn;
2use mecomp_storage::{
3    db::schemas::{
4        album::{Album, TABLE_NAME as ALBUM_TABLE_NAME},
5        artist::{Artist, TABLE_NAME as ARTIST_TABLE_NAME},
6        collection::{Collection, TABLE_NAME as COLLECTION_TABLE_NAME},
7        dynamic::{DynamicPlaylist, TABLE_NAME as DYNAMIC_PLAYLIST_TABLE_NAME},
8        playlist::{Playlist, TABLE_NAME as PLAYLIST_TABLE_NAME},
9        song::{Song, TABLE_NAME as SONG_TABLE_NAME},
10        RecordId,
11    },
12    errors::{Error, StorageResult},
13};
14use one_or_many::OneOrMany;
15use surrealdb::{Connection, Surreal};
16
17pub mod library;
18#[cfg(feature = "analysis")]
19pub mod radio;
20
21/// Get the songs associated with every thing in the list.
22///
23/// This function will go through the list of things and get the songs associated with each thing.
24///
25/// It will then remove duplicates from the list of songs.
26///
27/// # Errors
28///
29/// This function will return an error if there is an issue reading the songs from the database.
30#[inline]
31pub async fn get_songs_from_things<C: Connection>(
32    db: &Surreal<C>,
33    things: &[RecordId],
34) -> StorageResult<OneOrMany<Song>> {
35    // go through the list, and get songs for each thing (depending on what it is)
36    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    // remove duplicates
57    songs.dedup_by_key(|song| song.id.clone());
58
59    Ok(songs)
60}