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) + } + } }