From 43d84bcd302bb165dd65483331a478221db7e007 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Apr 2025 19:29:24 -0400 Subject: [PATCH] Added endpoint to get metadata --- src/callers/metadata.rs | 97 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index c16546d..abb0f0f 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -36,6 +36,14 @@ pub mod request { }) } } + + pub mod fetch_metadata { + #[derive(serde::Deserialize)] + pub struct Params { + pub id: Option, + pub song_queue_id: Option, + } + } } pub mod response { @@ -46,9 +54,19 @@ pub mod response { pub message: String, pub data: Vec, } + + pub mod fetch_metadata { + use serde::{Deserialize, Serialize}; + + #[derive(Default, Deserialize, Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } } -mod metadata_queue { +pub mod metadata_queue { use sqlx::Row; #[derive(Debug, serde::Serialize, sqlx::FromRow)] @@ -56,6 +74,14 @@ mod metadata_queue { pub id: uuid::Uuid, } + #[derive(Debug, serde::Deserialize, serde::Serialize, sqlx::FromRow)] + pub struct MetadataQueue { + pub id: uuid::Uuid, + pub metadata: String, + pub created_at: time::OffsetDateTime, + pub song_queue_id: uuid::Uuid, + } + pub async fn insert( pool: &sqlx::PgPool, metadata: &serde_json::Value, @@ -85,6 +111,47 @@ mod metadata_queue { Err(_err) => Err(sqlx::Error::RowNotFound), } } + + pub async fn get_with_song_queue_id( + pool: &sqlx::PgPool, + song_queue_id: &uuid::Uuid, + ) -> Result { + let result = sqlx::query( + r#" + SELECT * FROM "metadataQueue" WHERE song_queue_id = $1; + "#, + ) + .bind(song_queue_id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting: {}", e); + }); + + match result { + Ok(row) => { + Ok(MetadataQueue{ + id: row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + metadata: row + .try_get("metadata") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + created_at: row + .try_get("created_at") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + song_queue_id: row + .try_get("song_queue_id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + }) + } + Err(_err) => Err(sqlx::Error::RowNotFound), + } + } } pub mod endpoint { @@ -115,4 +182,32 @@ pub mod endpoint { } } } + + + pub async fn fetch_metadata( + axum::Extension(pool): axum::Extension, + axum::extract::Query(params): axum::extract::Query + ) -> ( + StatusCode, + Json, + ) { + let mut response = super::response::fetch_metadata::Response::default(); + let song_queue_id: uuid::Uuid = match params.song_queue_id { + Some(id) => id, + None => uuid::Uuid::nil(), + }; + // TODO: Make sure id works as well + + match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await { + Ok(item) => { + response.message = String::from("Successful"); + response.data.push(item); + (StatusCode::OK, Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (StatusCode::BAD_REQUEST, Json(response)) + } + } + } }