diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 0000000..baa40a6 --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1,2 @@ +pub mod songs; +pub mod albums; \ No newline at end of file diff --git a/src/api/songs.rs b/src/api/songs.rs new file mode 100644 index 0000000..440cb29 --- /dev/null +++ b/src/api/songs.rs @@ -0,0 +1,35 @@ +use leptos::*; +use crate::models::Artist; + + +use cfg_if::cfg_if; + +cfg_if! { +if #[cfg(feature = "ssr")] { + use crate::database::get_db_conn; + use diesel::prelude::*; +} +} + +/// Gets a Vector of Artists associated with a Song +/// +/// # Arguments +/// `song_id_arg` - The id of the Song to get the Artists for +/// +/// # Returns +/// A Result containing a Vector of Artists if the operation was successful, or an error if the operation failed +#[server(endpoint = "songs/get-artists")] +pub async fn get_artists(song_id_arg: Option) -> Result, ServerFnError> { + use crate::schema::artists::dsl::*; + use crate::schema::song_artists::dsl::*; + + let my_id = song_id_arg.ok_or(ServerFnError::ServerError("Song id must be present (Some) to get artists".to_string()))?; + + let my_artists = artists + .inner_join(song_artists) + .filter(song_id.eq(my_id)) + .select(artists::all_columns()) + .load(&mut get_db_conn())?; + + Ok(my_artists) +} \ No newline at end of file