From 9327ec19f5efcd7a783019d652ca74ceb6274a91 Mon Sep 17 00:00:00 2001 From: ecco257 <72117210+ecco257@users.noreply.github.com> Date: Fri, 12 Apr 2024 23:38:35 -0400 Subject: [PATCH] Add server function to get song from song id --- src/api/songs.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/api/songs.rs b/src/api/songs.rs index 440cb29..6ccf359 100644 --- a/src/api/songs.rs +++ b/src/api/songs.rs @@ -1,5 +1,6 @@ use leptos::*; use crate::models::Artist; +use crate::models::Song; use cfg_if::cfg_if; @@ -32,4 +33,27 @@ pub async fn get_artists(song_id_arg: Option) -> Result, Server .load(&mut get_db_conn())?; Ok(my_artists) +} + +/// Gets the song associated with a song id +/// +/// # Arguments +/// `song_id_arg` - The id of the Song to get the song for +/// +/// # Returns +/// A Result containing a Song if the operation was successful, or an error if the operation failed +#[server(endpoint = "songs/get-song")] +pub async fn get_song(song_id_arg: Option) -> Result { + use crate::schema::songs::dsl::*; + + let my_id = song_id_arg.ok_or(ServerFnError::ServerError("Song id must be present (Some) to get Song".to_string()))?; + + let mut my_song_vec: Vec = songs + .filter(id.eq(my_id)) + .limit(1) + .load(&mut get_db_conn())?; + + let my_song = my_song_vec.pop().ok_or(ServerFnError::ServerError("Song not found".to_string()))?; + + Ok(my_song) } \ No newline at end of file