From 082e6b9269c12b91b4d14e36efbf1479be9441a7 Mon Sep 17 00:00:00 2001 From: ecco257 <72117210+ecco257@users.noreply.github.com> Date: Fri, 5 Apr 2024 23:47:08 -0400 Subject: [PATCH] Add albums server functions to fetch album from album id --- src/api/albums.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/api/albums.rs diff --git a/src/api/albums.rs b/src/api/albums.rs new file mode 100644 index 0000000..99149a2 --- /dev/null +++ b/src/api/albums.rs @@ -0,0 +1,36 @@ +use leptos::*; +use crate::models::Album; + + +use cfg_if::cfg_if; + +cfg_if! { +if #[cfg(feature = "ssr")] { + use crate::database::get_db_conn; + use diesel::prelude::*; +} +} + +/// Gets the Album associated with an album id +/// +/// # Arguments +/// `album_id_arg` The id of the Album to get the album for +/// +/// # Returns +/// A Result containing an Album if the operation was successful, or an error if the operation failed +#[server(endpoint = "albums/get-album")] +pub async fn get_album(album_id_arg: Option) -> Result { + + use crate::schema::albums::dsl::*; + + let my_id = album_id_arg.ok_or(ServerFnError::ServerError("Album id must be present (Some) to get Album".to_string()))?; + + let mut my_album_vec: Vec = albums + .filter(id.eq(my_id)) + .limit(1) + .load(&mut get_db_conn())?; + + let my_album = my_album_vec.pop().ok_or(ServerFnError::ServerError("Album not found".to_string()))?; + + Ok(my_album) +} \ No newline at end of file