From ffad799f72d3453cab04be7ef1e109ddfad0f5d8 Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Tue, 23 Jul 2024 23:30:15 -0400 Subject: [PATCH] Add function to get song album object --- src/models.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/models.rs b/src/models.rs index caa52ed..d3e4104 100644 --- a/src/models.rs +++ b/src/models.rs @@ -313,4 +313,30 @@ impl Song { Ok(my_artists) } + + /// Get the album for this song from the database + /// + /// # Arguments + /// + /// * `conn` - A mutable reference to a database connection + /// + /// # Returns + /// + /// * `Result, Box>` - A result indicating success with an album, or None if + /// the song does not have an album, or an error + /// + #[cfg(feature = "ssr")] + pub fn get_album(self: &Self, conn: &mut PgPooledConn) -> Result, Box> { + use crate::schema::albums::dsl::*; + + if let Some(album_id) = self.album_id { + let my_album = albums + .filter(id.eq(album_id)) + .first::(conn)?; + + Ok(Some(my_album)) + } else { + Ok(None) + } + } }