diff --git a/src/api/album.rs b/src/api/album.rs new file mode 100644 index 0000000..82f62c9 --- /dev/null +++ b/src/api/album.rs @@ -0,0 +1,37 @@ +use leptos::*; +use crate::models::Album; +use crate::models::Song; +use crate::songdata::SongData; + +use cfg_if::cfg_if; + +cfg_if! { + if #[cfg(feature = "ssr")] { + use leptos::server_fn::error::NoCustomError; + use crate::database::get_db_conn; + } +} + +#[server(endpoint = "album/get")] +pub async fn get_album(id: Option) -> Result { + let db_con = &mut get_db_conn(); + let album = Album::get_album(id,db_con) + .map_err(|e| ServerFnError::::ServerError(format!("Error getting album: {}", e)))?; + Ok(album) +} + +#[server(endpoint = "album/get_songs")] +pub async fn get_songs(album: Option) -> Result, ServerFnError> { + let db_con = &mut get_db_conn(); + let songs = album.get_songs(db_con) + .map_err(|e| ServerFnError::::ServerError(format!("Error getting album: {}", e)))?; + Ok(songs) +} + +// #[server(endpoint = "album/get_song_list")] +// pub async fn get_song_list(album: Option) -> Result, ServerFnError> { +// songs = get_songs(album)?; + +// let mut song_data_list = Vec::new(); +// // TODO: NEEDS SONG DATA QUERIES +// } diff --git a/src/api/mod.rs b/src/api/mod.rs index c287a07..e9ee6c9 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,3 +1,4 @@ pub mod history; pub mod profile; pub mod songs; +pub mod album; diff --git a/src/app.rs b/src/app.rs index ed8b252..689e9cd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -7,7 +7,7 @@ use leptos_meta::*; use leptos_router::*; use crate::pages::login::*; use crate::pages::signup::*; -use crate::pages::album::*; +use crate::pages::albumpage::*; use crate::error_template::{AppError, ErrorTemplate}; @@ -43,7 +43,7 @@ pub fn App() -> impl IntoView { - + // diff --git a/src/models.rs b/src/models.rs index b2534dc..e1a8a63 100644 --- a/src/models.rs +++ b/src/models.rs @@ -528,22 +528,28 @@ impl Album { Ok(my_songs) } -} -#[server(endpoint = "get_album")] -pub async fn get_album(a_id: i32) -> Result,ServerFnError> { - use crate::schema::songs::dsl::*; - use crate::schema::song_artists::dsl::*; - - let conn = get_db_conn(); - - let songs = songs - .inner_join(song_artists) - .filter(album_id.eq(a_id)) - .select(songs::all_columns()) - .load(conn)?; + /// Obtain an album from its albumid + /// # Arguments + /// + /// * `album_id` - The id of the album to select + /// * `conn` - A mutable reference to a database connection + /// + /// # Returns + /// + /// * `Result>` - A result indicating success with the desired album, or an error + /// + #[cfg(feature = "ssr")] + pub fn get_album(album_id: i32, conn: &mut PgPooledConn) -> Result> { + use crate::schema::albums::dsl::*; + use crate::database::get_db_conn; - Ok(songs.into()) + let album = albums + .find(album_id) + .first(conn)?; + + Ok(album) + } } /// Model for a song diff --git a/src/pages.rs b/src/pages.rs index c1e787a..057a60a 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -1,3 +1,3 @@ pub mod login; pub mod signup; -pub mod album; \ No newline at end of file +pub mod albumpage; \ No newline at end of file diff --git a/src/pages/album.rs b/src/pages/albumpage.rs similarity index 92% rename from src/pages/album.rs rename to src/pages/albumpage.rs index bbe3f35..915d9f4 100644 --- a/src/pages/album.rs +++ b/src/pages/albumpage.rs @@ -1,6 +1,5 @@ use leptos::leptos_dom::*; use leptos::*; -use leptos_icons::*; use leptos_router::*; use crate::models::*; use crate::components::song_list::*; @@ -11,6 +10,7 @@ struct AlbumParams { id: i32 } +/* #[component] pub fn AlbumPage() -> impl IntoView { let params = use_params::(); @@ -43,7 +43,7 @@ pub fn AlbumPage() -> impl IntoView { view! { }.into_view() }, Some(Err(e)) => { - view! {
"Error loading albums"
}.into_view() + view! {
"Error loading albums: :e"
}.into_view() }, None => {view! { }.into_view()} } @@ -51,4 +51,5 @@ pub fn AlbumPage() -> impl IntoView { }} } -} \ No newline at end of file +} +*/