Reorganizing metadata queue code (#144)

* Reorganizing metadata queue code

* Function name change

* Test payload changes

* function parameter name change
This commit was merged in pull request #144.
This commit is contained in:
KD
2025-06-18 19:47:41 -04:00
committed by GitHub
parent fa792be9cc
commit fbec3305b1
2 changed files with 70 additions and 62 deletions
+50 -44
View File
@@ -1,40 +1,43 @@
// TODO: Explicitly make this module target queueing a song's metadata // TODO: Explicitly make this module target queueing a song's metadata
pub mod request { pub mod request {
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Deserialize, Serialize, sqlx::FromRow)] pub mod queue_metadata {
pub struct Request { use serde::{Deserialize, Serialize};
pub id: uuid::Uuid,
pub album: String,
pub album_artist: String,
pub artist: String,
pub disc: i32,
pub disc_count: i32,
pub duration: i64,
pub genre: String,
pub title: String,
pub track: i32,
pub track_count: i32,
pub year: i32,
}
impl Request { #[derive(Debug, Default, Deserialize, Serialize, sqlx::FromRow)]
pub async fn to_json_value(&self) -> serde_json::Value { pub struct Request {
serde_json::json!( pub song_queue_id: uuid::Uuid,
{ pub album: String,
"id": &self.id, pub album_artist: String,
"album": &self.album, pub artist: String,
"album_artist": &self.album_artist, pub disc: i32,
"genre": &self.genre, pub disc_count: i32,
"year": &self.year, pub duration: i64,
"track_count": &self.track_count, pub genre: String,
"disc_count": &self.disc_count, pub title: String,
"title": &self.title, pub track: i32,
"artist": &self.artist, pub track_count: i32,
"disc": &self.disc, pub year: i32,
"track": &self.track, }
"duration": &self.duration,
}) impl Request {
pub async fn to_json_value(&self) -> serde_json::Value {
serde_json::json!(
{
"song_queue_id": &self.song_queue_id,
"album": &self.album,
"album_artist": &self.album_artist,
"genre": &self.genre,
"year": &self.year,
"track_count": &self.track_count,
"disc_count": &self.disc_count,
"title": &self.title,
"artist": &self.artist,
"disc": &self.disc,
"track": &self.track,
"duration": &self.duration,
})
}
} }
} }
@@ -50,12 +53,15 @@ pub mod request {
} }
pub mod response { pub mod response {
use serde::{Deserialize, Serialize};
#[derive(Default, Deserialize, Serialize)] pub mod queue_metadata {
pub struct Response { use serde::{Deserialize, Serialize};
pub message: String,
pub data: Vec<uuid::Uuid>, #[derive(Default, Deserialize, Serialize)]
pub struct Response {
pub message: String,
pub data: Vec<uuid::Uuid>,
}
} }
pub mod fetch_metadata { pub mod fetch_metadata {
@@ -203,14 +209,14 @@ pub mod endpoint {
pub async fn queue_metadata( pub async fn queue_metadata(
axum::Extension(pool): axum::Extension<sqlx::PgPool>, axum::Extension(pool): axum::Extension<sqlx::PgPool>,
Json(payload): Json<super::request::Request>, Json(payload): Json<super::request::queue_metadata::Request>,
) -> (StatusCode, Json<super::response::Response>) { ) -> (StatusCode, Json<super::response::queue_metadata::Response>) {
let mut results: Vec<uuid::Uuid> = Vec::new(); let mut results: Vec<uuid::Uuid> = Vec::new();
let mut response = super::response::Response::default(); let mut response = super::response::queue_metadata::Response::default();
let meta = payload.to_json_value().await; let meta = payload.to_json_value().await;
match super::metadata_queue::insert(&pool, &meta, &payload.id).await { match super::metadata_queue::insert(&pool, &meta, &payload.song_queue_id).await {
Ok(id) => { Ok(metadata_queue_id) => {
results.push(id); results.push(metadata_queue_id);
response.data = results; response.data = results;
response.message = if response.data.is_empty() { response.message = if response.data.is_empty() {
String::from("Error") String::from("Error")
+20 -18
View File
@@ -325,9 +325,9 @@ mod tests {
async fn queue_metadata_req( async fn queue_metadata_req(
app: &axum::Router, app: &axum::Router,
id: &uuid::Uuid, song_queue_id: &uuid::Uuid,
) -> Result<axum::response::Response, std::convert::Infallible> { ) -> Result<axum::response::Response, std::convert::Infallible> {
let payload = payload_data(&id).await; let payload = payload_data::queue_metadata_payload_data(&song_queue_id).await;
let req = axum::http::Request::builder() let req = axum::http::Request::builder()
.method(axum::http::Method::POST) .method(axum::http::Method::POST)
@@ -600,22 +600,24 @@ mod tests {
serde_json::from_slice(&body).unwrap() serde_json::from_slice(&body).unwrap()
} }
pub async fn payload_data(id: &uuid::Uuid) -> serde_json::Value { pub mod payload_data {
serde_json::json!( pub async fn queue_metadata_payload_data(song_queue_id: &uuid::Uuid) -> serde_json::Value {
{ serde_json::json!(
"id": id, {
"album" : "Machine Gun: The FillMore East First Show", "song_queue_id": song_queue_id,
"album_artist" : "Jimi Hendrix", "album" : "Machine Gun: The FillMore East First Show",
"artist" : "Jimi Hendrix", "album_artist" : "Jimi Hendrix",
"disc" : 1, "artist" : "Jimi Hendrix",
"disc_count" : 1, "disc" : 1,
"duration" : 330, "disc_count" : 1,
"genre" : "Psychadelic Rock", "duration" : 330,
"title" : "Power of Soul", "genre" : "Psychadelic Rock",
"track" : 1, "title" : "Power of Soul",
"track_count" : 11, "track" : 1,
"year" : 2016 "track_count" : 11,
}) "year" : 2016
})
}
} }
#[tokio::test] #[tokio::test]