Album pages for users not signed in
This commit is contained in:
parent
22cee4a265
commit
3b6035dd71
@ -1,6 +1,5 @@
|
|||||||
use leptos::*;
|
use leptos::*;
|
||||||
use crate::models::Album;
|
use crate::models::Album;
|
||||||
use crate::models::Song;
|
|
||||||
use crate::songdata::SongData;
|
use crate::songdata::SongData;
|
||||||
|
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
@ -23,10 +22,11 @@ pub async fn get_album(id: i32) -> Result<Album, ServerFnError> {
|
|||||||
|
|
||||||
#[server(endpoint = "album/get_songs")]
|
#[server(endpoint = "album/get_songs")]
|
||||||
pub async fn get_songs(id: i32) -> Result<Vec<SongData>, ServerFnError> {
|
pub async fn get_songs(id: i32) -> Result<Vec<SongData>, 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();
|
let db_con = &mut get_db_conn();
|
||||||
// TODO: NEEDS SONG DATA QUERIES
|
// 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::<NoCustomError>::ServerError(format!("Error getting song data: {}", e)))?;
|
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting song data: {}", e)))?;
|
||||||
Ok(songdata)
|
Ok(songdata)
|
||||||
}
|
}
|
@ -561,12 +561,14 @@ impl Album {
|
|||||||
/// * `Result<Album, Box<dyn Error>>` - A result indicating success with the desired album, or an error
|
/// * `Result<Album, Box<dyn Error>>` - A result indicating success with the desired album, or an error
|
||||||
///
|
///
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
pub fn get_song_data(album_id: i32, user_like_dislike_id: i32, conn: &mut PgPooledConn) -> Result<Vec<SongData>, Box<dyn Error>> {
|
pub fn get_song_data(album_id: i32, user_like_dislike: Option<User>, conn: &mut PgPooledConn) -> Result<Vec<SongData>, Box<dyn Error>> {
|
||||||
use crate::schema::*;
|
use crate::schema::*;
|
||||||
use crate::database::get_db_conn;
|
use crate::database::get_db_conn;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
let songs: Vec<(Album, Option<Song>, Option<Artist>, Option<(i32, i32)>, Option<(i32, i32)>)> =
|
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<Song>, Option<Artist>, Option<(i32, i32)>, Option<(i32, i32)>)> =
|
||||||
albums::table
|
albums::table
|
||||||
.find(album_id)
|
.find(album_id)
|
||||||
.left_join(songs::table.on(albums::id.nullable().eq(songs::album_id)))
|
.left_join(songs::table.on(albums::id.nullable().eq(songs::album_id)))
|
||||||
@ -580,11 +582,31 @@ impl Album {
|
|||||||
song_likes::all_columns.nullable(),
|
song_likes::all_columns.nullable(),
|
||||||
song_dislikes::all_columns.nullable()
|
song_dislikes::all_columns.nullable()
|
||||||
))
|
))
|
||||||
|
.order(songs::track.asc())
|
||||||
|
.load(conn)?;
|
||||||
|
song_list
|
||||||
|
} else {
|
||||||
|
let song_list: Vec<(Album, Option<Song>, Option<Artist>)> =
|
||||||
|
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)?;
|
.load(conn)?;
|
||||||
|
|
||||||
let mut album_songs: HashMap<i32, SongData> = HashMap::with_capacity(songs.len());
|
let song_list: Vec<(Album, Option<Song>, Option<Artist>, Option<(i32, i32)>, Option<(i32, i32)>)> =
|
||||||
|
song_list.into_iter().map( |(album, song, artist)| (album, song, artist, None, None) ).collect();
|
||||||
|
song_list
|
||||||
|
};
|
||||||
|
|
||||||
for (album, song, artist, like, dislike) in songs {
|
let mut album_songs: HashMap<i32, SongData> = HashMap::with_capacity(song_list.len());
|
||||||
|
|
||||||
|
for (album, song, artist, like, dislike) in song_list {
|
||||||
if let Some(song) = song {
|
if let Some(song) = song {
|
||||||
let like_dislike = match (like, dislike) {
|
let like_dislike = match (like, dislike) {
|
||||||
(Some(_), Some(_)) => Some((true, true)),
|
(Some(_), Some(_)) => Some((true, true)),
|
||||||
|
@ -43,7 +43,7 @@ pub fn AlbumPage() -> impl IntoView {
|
|||||||
view! { <SongList songs=(*s).clone().into()/> }.into_view()
|
view! { <SongList songs=(*s).clone().into()/> }.into_view()
|
||||||
},
|
},
|
||||||
Some(Err(e)) => {
|
Some(Err(e)) => {
|
||||||
view! { <div>"Error loading albums: :e"</div> }.into_view()
|
view! { <div>{format!("Error loading albums: : {}",e)}</div> }.into_view()
|
||||||
},
|
},
|
||||||
None => {view! { }.into_view()}
|
None => {view! { }.into_view()}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user