Add songs server endpoint to get artists using song id

This commit is contained in:
Connor Wittman 2024-04-05 23:43:44 -04:00
parent 6bed5e5c55
commit e1d3bb4099
2 changed files with 37 additions and 0 deletions

2
src/api/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod songs;
pub mod albums;

35
src/api/songs.rs Normal file
View File

@ -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<i32>) -> Result<Vec<Artist>, 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)
}