Use db_type for Album

This commit is contained in:
2025-05-05 00:53:31 +00:00
parent 3f43ef2d20
commit a67bd37d11
10 changed files with 20 additions and 56 deletions

View File

@ -41,7 +41,7 @@ pub async fn get_album(id: i32) -> Result<Option<frontend::Album>, ServerFnError
.unwrap_or("/assets/images/placeholders/MusicPlaceholder.svg".to_string());
let album = frontend::Album {
id: album.id.unwrap(),
id: album.id,
title: album.title,
artists,
release_date: album.release_date,

View File

@ -28,7 +28,7 @@ pub async fn add_album(
release_date: Option<String>,
image_path: Option<String>,
) -> Result<(), ServerFnError> {
use crate::models::backend::Album;
use crate::models::backend::NewAlbum;
use crate::schema::albums::{self};
use leptos::server_fn::error::NoCustomError;
@ -46,8 +46,7 @@ pub async fn add_album(
let image_path_arg = image_path.filter(|image_path| !image_path.is_empty());
let new_album = Album {
id: None,
let new_album = NewAlbum {
title: album_title,
release_date: parsed_release_date,
image_path: image_path_arg,

View File

@ -11,7 +11,6 @@ cfg_if! {
use crate::util::database::get_db_conn;
use diesel::prelude::*;
use std::collections::HashMap;
use server_fn::error::NoCustomError;
use crate::models::backend::Album;
}
}
@ -226,15 +225,11 @@ pub async fn albums_by_artist(
.load(db)?;
for (album, artist) in album_artists {
let album_id = album.id.ok_or(ServerFnError::ServerError::<NoCustomError>(
"Album id not found in database".to_string(),
))?;
if let Some(stored_album) = albums_map.get_mut(&album_id) {
if let Some(stored_album) = albums_map.get_mut(&album.id) {
stored_album.artists.push(artist);
} else {
let albumdata = frontend::Album {
id: album_id,
id: album.id,
title: album.title,
artists: vec![artist],
release_date: album.release_date,
@ -243,7 +238,7 @@ pub async fn albums_by_artist(
.unwrap_or("/assets/images/placeholders/MusicPlaceholder.svg".to_string()),
};
albums_map.insert(album_id, albumdata);
albums_map.insert(album.id, albumdata);
}
}

View File

@ -78,13 +78,11 @@ pub async fn search_albums(
.load(&mut db_conn)?;
for (album, artist, score) in album_artists {
let album_id = album.id.unwrap();
if let Some((stored_album, _score)) = albums_map.get_mut(&album_id) {
if let Some((stored_album, _score)) = albums_map.get_mut(&album.id) {
stored_album.artists.push(artist);
} else {
let albumdata = frontend::Album {
id: album_id,
id: album.id,
title: album.title,
artists: vec![artist],
release_date: album.release_date,
@ -93,7 +91,7 @@ pub async fn search_albums(
.unwrap_or("/assets/images/placeholders/MusicPlaceholder.svg".to_string()),
};
albums_map.insert(album_id, (albumdata, score));
albums_map.insert(album.id, (albumdata, score));
}
}

View File

@ -204,14 +204,8 @@ pub fn SongAlbum(album: Option<Album>) -> impl IntoView {
album.as_ref().map(|album| {
view! {
<span>
{
if let Some(id) = album.id {
Either::Left(view! { <a class="hover:underline active:text-controls-active"
href={format!("/album/{id}")}>{album.title.clone()}</a> })
} else {
Either::Right(view! { <span>{album.title.clone()}</span> })
}
}
<a class="hover:underline active:text-controls-active"
href={format!("/album/{}", album.id)}>{album.title.clone()}</a>
</span>
}
})

View File

@ -88,7 +88,7 @@ pub fn Upload(open: RwSignal<bool>) -> impl IntoView {
albums
.into_iter()
.map(|(album, _score)| Album {
id: Some(album.id),
id: album.id,
title: album.title,
release_date: album.release_date,
image_path: Some(album.image_path),
@ -259,11 +259,7 @@ pub fn Album(
) -> impl IntoView {
//Converts album title to album id to upload a song
let add_album = move |_| {
let value_str = match album.id {
Some(v) => v.to_string(),
None => String::from("None"),
};
set_albums.update(|value| *value = value_str);
set_albums.update(|value| *value = album.id.to_string());
set_filtered.update(|value| *value = vec![]);
};
view! {

View File

@ -1,26 +1,14 @@
use chrono::NaiveDate;
use libretunes_macro::db_type;
use serde::{Deserialize, Serialize};
use cfg_if::cfg_if;
cfg_if! {
if #[cfg(feature = "ssr")] {
use diesel::prelude::*;
}
}
/// Model for an album
#[cfg_attr(
feature = "ssr",
derive(Queryable, Selectable, Insertable, Identifiable)
)]
#[cfg_attr(feature = "ssr", diesel(table_name = crate::schema::albums))]
#[cfg_attr(feature = "ssr", diesel(check_for_backend(diesel::pg::Pg)))]
#[db_type(crate::schema::albums)]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Album {
/// A unique id for the album
#[cfg_attr(feature = "ssr", diesel(deserialize_as = i32))]
pub id: Option<i32>,
#[omit_new]
pub id: i32,
/// The album's title
pub title: String,
/// The album's release date

View File

@ -12,6 +12,7 @@ pub mod song;
pub mod user;
pub use album::Album;
pub use album::NewAlbum;
pub use artist::Artist;
pub use history_entry::HistoryEntry;
pub use playlist::Playlist;

View File

@ -1,6 +1,6 @@
use chrono::{NaiveDate, NaiveDateTime};
use serde::{Deserialize, Serialize};
use libretunes_macro::db_type;
use serde::{Deserialize, Serialize};
#[db_type(crate::schema::songs)]
#[derive(Clone, Serialize, Deserialize)]

View File

@ -46,14 +46,7 @@ impl TryInto<backend::Song> for Song {
Ok(backend::Song {
id: self.id,
title: self.title,
album_id: self
.album
.map(|album| {
album
.id
.ok_or("Album id must be present (Some) to convert to Song")
})
.transpose()?,
album_id: self.album.map(|album| album.id),
track: self.track,
duration: self.duration,
release_date: self.release_date,