API Endpoints for Album Queries
This commit is contained in:
parent
df01bafbd1
commit
fe1b76e6e4
37
src/api/album.rs
Normal file
37
src/api/album.rs
Normal file
@ -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<i32>) -> Result<Album, ServerFnError> {
|
||||
let db_con = &mut get_db_conn();
|
||||
let album = Album::get_album(id,db_con)
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting album: {}", e)))?;
|
||||
Ok(album)
|
||||
}
|
||||
|
||||
#[server(endpoint = "album/get_songs")]
|
||||
pub async fn get_songs(album: Option<Album>) -> Result<Vec<Song>, ServerFnError> {
|
||||
let db_con = &mut get_db_conn();
|
||||
let songs = album.get_songs(db_con)
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting album: {}", e)))?;
|
||||
Ok(songs)
|
||||
}
|
||||
|
||||
// #[server(endpoint = "album/get_song_list")]
|
||||
// pub async fn get_song_list(album: Option<Album>) -> Result<Vec<Song>, ServerFnError> {
|
||||
// songs = get_songs(album)?;
|
||||
|
||||
// let mut song_data_list = Vec::new();
|
||||
// // TODO: NEEDS SONG DATA QUERIES
|
||||
// }
|
@ -1,3 +1,4 @@
|
||||
pub mod history;
|
||||
pub mod profile;
|
||||
pub mod songs;
|
||||
pub mod album;
|
||||
|
@ -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 {
|
||||
<Route path="" view=Dashboard />
|
||||
<Route path="dashboard" view=Dashboard />
|
||||
<Route path="search" view=Search />
|
||||
<Route path="album/:id" view=AlbumPage />
|
||||
//<Route path="album/:id" view=AlbumPage />
|
||||
</Route>
|
||||
<Route path="/login" view=Login />
|
||||
<Route path="/signup" view=Signup />
|
||||
|
@ -528,22 +528,28 @@ impl Album {
|
||||
|
||||
Ok(my_songs)
|
||||
}
|
||||
}
|
||||
|
||||
#[server(endpoint = "get_album")]
|
||||
pub async fn get_album(a_id: i32) -> Result<Vec<SongData>,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<Album, Box<dyn Error>>` - 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<Album, Box<dyn Error>> {
|
||||
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
|
||||
|
@ -1,3 +1,3 @@
|
||||
pub mod login;
|
||||
pub mod signup;
|
||||
pub mod album;
|
||||
pub mod albumpage;
|
@ -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::<AlbumParams>();
|
||||
@ -43,7 +43,7 @@ pub fn AlbumPage() -> impl IntoView {
|
||||
view! { <SongList songs=s.clone().into()/> }.into_view()
|
||||
},
|
||||
Some(Err(e)) => {
|
||||
view! { <div>"Error loading albums"</div> }.into_view()
|
||||
view! { <div>"Error loading albums: :e"</div> }.into_view()
|
||||
},
|
||||
None => {view! { }.into_view()}
|
||||
}
|
||||
@ -51,4 +51,5 @@ pub fn AlbumPage() -> impl IntoView {
|
||||
}}
|
||||
</Suspense>
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
Loading…
x
Reference in New Issue
Block a user