From 3b6035dd7180eef9bfbead01543678efeab06f92 Mon Sep 17 00:00:00 2001 From: Aidan Westphal Date: Wed, 20 Nov 2024 02:12:09 +0000 Subject: [PATCH] Album pages for users not signed in --- src/api/album.rs | 6 ++-- src/models.rs | 64 ++++++++++++++++++++++++++++-------------- src/pages/albumpage.rs | 2 +- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/api/album.rs b/src/api/album.rs index 33a5299..ff1f6cf 100644 --- a/src/api/album.rs +++ b/src/api/album.rs @@ -1,6 +1,5 @@ use leptos::*; use crate::models::Album; -use crate::models::Song; use crate::songdata::SongData; use cfg_if::cfg_if; @@ -23,10 +22,11 @@ pub async fn get_album(id: i32) -> Result { #[server(endpoint = "album/get_songs")] pub async fn get_songs(id: i32) -> Result, ServerFnError> { - let user = get_user().await?; + use crate::auth::get_logged_in_user; + let user = get_logged_in_user().await?; let db_con = &mut get_db_conn(); // TODO: NEEDS SONG DATA QUERIES - let songdata = Album::get_song_data(id,user.id.unwrap(),db_con) + let songdata = Album::get_song_data(id,user,db_con) .map_err(|e| ServerFnError::::ServerError(format!("Error getting song data: {}", e)))?; Ok(songdata) } \ No newline at end of file diff --git a/src/models.rs b/src/models.rs index 4a34b86..08dd956 100644 --- a/src/models.rs +++ b/src/models.rs @@ -561,30 +561,52 @@ impl Album { /// * `Result>` - A result indicating success with the desired album, or an error /// #[cfg(feature = "ssr")] - pub fn get_song_data(album_id: i32, user_like_dislike_id: i32, conn: &mut PgPooledConn) -> Result, Box> { + pub fn get_song_data(album_id: i32, user_like_dislike: Option, conn: &mut PgPooledConn) -> Result, Box> { use crate::schema::*; use crate::database::get_db_conn; use std::collections::HashMap; - - let songs: Vec<(Album, Option, Option, Option<(i32, i32)>, Option<(i32, i32)>)> = - albums::table - .find(album_id) - .left_join(songs::table.on(albums::id.nullable().eq(songs::album_id))) - .left_join(song_artists::table.inner_join(artists::table).on(songs::id.eq(song_artists::song_id))) - .left_join(song_likes::table.on(songs::id.eq(song_likes::song_id).and(song_likes::user_id.eq(user_like_dislike_id)))) - .left_join(song_dislikes::table.on(songs::id.eq(song_dislikes::song_id).and(song_dislikes::user_id.eq(user_like_dislike_id)))) - .select(( - albums::all_columns, - songs::all_columns.nullable(), - artists::all_columns.nullable(), - song_likes::all_columns.nullable(), - song_dislikes::all_columns.nullable() - )) - .load(conn)?; - - let mut album_songs: HashMap = HashMap::with_capacity(songs.len()); - for (album, song, artist, like, dislike) in songs { + let song_list = if let Some(user_like_dislike) = user_like_dislike { + let user_like_dislike_id = user_like_dislike.id.unwrap(); + let song_list: Vec<(Album, Option, Option, Option<(i32, i32)>, Option<(i32, i32)>)> = + albums::table + .find(album_id) + .left_join(songs::table.on(albums::id.nullable().eq(songs::album_id))) + .left_join(song_artists::table.inner_join(artists::table).on(songs::id.eq(song_artists::song_id))) + .left_join(song_likes::table.on(songs::id.eq(song_likes::song_id).and(song_likes::user_id.eq(user_like_dislike_id)))) + .left_join(song_dislikes::table.on(songs::id.eq(song_dislikes::song_id).and(song_dislikes::user_id.eq(user_like_dislike_id)))) + .select(( + albums::all_columns, + songs::all_columns.nullable(), + artists::all_columns.nullable(), + song_likes::all_columns.nullable(), + song_dislikes::all_columns.nullable() + )) + .order(songs::track.asc()) + .load(conn)?; + song_list + } else { + let song_list: Vec<(Album, Option, Option)> = + albums::table + .find(album_id) + .left_join(songs::table.on(albums::id.nullable().eq(songs::album_id))) + .left_join(song_artists::table.inner_join(artists::table).on(songs::id.eq(song_artists::song_id))) + .select(( + albums::all_columns, + songs::all_columns.nullable(), + artists::all_columns.nullable() + )) + .order(songs::track.asc()) + .load(conn)?; + + let song_list: Vec<(Album, Option, Option, Option<(i32, i32)>, Option<(i32, i32)>)> = + song_list.into_iter().map( |(album, song, artist)| (album, song, artist, None, None) ).collect(); + song_list + }; + + let mut album_songs: HashMap = HashMap::with_capacity(song_list.len()); + + for (album, song, artist, like, dislike) in song_list { if let Some(song) = song { let like_dislike = match (like, dislike) { (Some(_), Some(_)) => Some((true, true)), @@ -612,7 +634,7 @@ impl Album { album_songs.insert(song.id.unwrap(), songdata); } } - + // Sort the songs by date let mut songdata: Vec = album_songs.into_values().collect(); songdata.sort_by(|a, b| b.track.cmp(&a.track)); diff --git a/src/pages/albumpage.rs b/src/pages/albumpage.rs index 392cc9b..162f870 100644 --- a/src/pages/albumpage.rs +++ b/src/pages/albumpage.rs @@ -43,7 +43,7 @@ pub fn AlbumPage() -> impl IntoView { view! { }.into_view() }, Some(Err(e)) => { - view! {
"Error loading albums: :e"
}.into_view() + view! {
{format!("Error loading albums: : {}",e)}
}.into_view() }, None => {view! { }.into_view()} }