Skip to content

Commit

Permalink
api/id3/orm: use song short inside full
Browse files Browse the repository at this point in the history
  • Loading branch information
vnghia committed Nov 28, 2024
1 parent c47eee3 commit e46aca6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
7 changes: 2 additions & 5 deletions nghe-api/src/id3/song/full.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use nghe_proc_macro::api_derive;
use uuid::Uuid;

use super::Song;
use super::Short;
use crate::id3::genre;

#[api_derive]
pub struct Full {
#[serde(flatten)]
pub song: Song,
pub album: String,
pub album_id: Uuid,
pub short: Short,
pub genres: genre::Genres,
}
19 changes: 5 additions & 14 deletions nghe-backend/src/orm/id3/song/full.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use diesel::dsl::sql;
use diesel::expression::SqlLiteral;
use diesel::prelude::*;
use diesel::sql_types;
use nghe_api::id3;
use o2o::o2o;
use uuid::Uuid;

use super::Song;
use super::short::Short;
use crate::orm::id3::genre;
use crate::Error;

Expand All @@ -15,13 +12,7 @@ use crate::Error;
pub struct Full {
#[into(~.try_into()?)]
#[diesel(embed)]
pub song: Song,
#[diesel(select_expression = sql("any_value(albums.name) album_name"))]
#[diesel(select_expression_type = SqlLiteral<sql_types::Text>)]
pub album: String,
#[diesel(select_expression = sql("any_value(albums.id) album_id"))]
#[diesel(select_expression_type = SqlLiteral<sql_types::Uuid>)]
pub album_id: Uuid,
pub short: Short,
#[into(~.into())]
#[diesel(embed)]
pub genres: genre::Genres,
Expand Down Expand Up @@ -103,9 +94,9 @@ mod test {

if allow {
let database_song = database_song.unwrap();
let database_artists: Vec<String> = database_song.song.artists.into();
assert_eq!(database_song.album, album.name);
assert_eq!(database_song.album_id, album_id);
let database_artists: Vec<String> = database_song.short.song.artists.into();
assert_eq!(database_song.short.album, album.name);
assert_eq!(database_song.short.album_id, album_id);
assert_eq!(database_artists, artists);
assert_eq!(database_song.genres.value.len(), n_genre);
} else {
Expand Down
6 changes: 3 additions & 3 deletions nghe-backend/src/route/browsing/get_song.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ mod test {
.unwrap()
.song;

assert_eq!(database_song.album, album.name);
assert_eq!(database_song.album_id, album_id);
assert_eq!(database_song.short.album, album.name);
assert_eq!(database_song.short.album_id, album_id);

let database_artists: Vec<_> =
database_song.song.artists.into_iter().map(|artist| artist.name).collect();
database_song.short.song.artists.into_iter().map(|artist| artist.name).collect();
assert_eq!(database_artists, artists);

let genres = database_song.genres.value;
Expand Down

0 comments on commit e46aca6

Please sign in to comment.