Add server function to get song from song id

This commit is contained in:
Connor Wittman 2024-04-12 23:38:35 -04:00
parent ece6d19fc3
commit 9327ec19f5

View File

@ -1,5 +1,6 @@
use leptos::*; use leptos::*;
use crate::models::Artist; use crate::models::Artist;
use crate::models::Song;
use cfg_if::cfg_if; use cfg_if::cfg_if;
@ -33,3 +34,26 @@ pub async fn get_artists(song_id_arg: Option<i32>) -> Result<Vec<Artist>, Server
Ok(my_artists) 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<i32>) -> Result<Song, ServerFnError> {
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<Song> = 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)
}