Added endpoint to get metadata

This commit is contained in:
kdeng00
2025-04-26 19:29:24 -04:00
parent d2302f44fd
commit 43d84bcd30
+96 -1
View File
@@ -36,6 +36,14 @@ pub mod request {
}) })
} }
} }
pub mod fetch_metadata {
#[derive(serde::Deserialize)]
pub struct Params {
pub id: Option<uuid::Uuid>,
pub song_queue_id: Option<uuid::Uuid>,
}
}
} }
pub mod response { pub mod response {
@@ -46,9 +54,19 @@ pub mod response {
pub message: String, pub message: String,
pub data: Vec<uuid::Uuid>, pub data: Vec<uuid::Uuid>,
} }
pub mod fetch_metadata {
use serde::{Deserialize, Serialize};
#[derive(Default, Deserialize, Serialize)]
pub struct Response {
pub message: String,
pub data: Vec<crate::callers::metadata::metadata_queue::MetadataQueue>,
}
}
} }
mod metadata_queue { pub mod metadata_queue {
use sqlx::Row; use sqlx::Row;
#[derive(Debug, serde::Serialize, sqlx::FromRow)] #[derive(Debug, serde::Serialize, sqlx::FromRow)]
@@ -56,6 +74,14 @@ mod metadata_queue {
pub id: uuid::Uuid, 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( pub async fn insert(
pool: &sqlx::PgPool, pool: &sqlx::PgPool,
metadata: &serde_json::Value, metadata: &serde_json::Value,
@@ -85,6 +111,47 @@ mod metadata_queue {
Err(_err) => Err(sqlx::Error::RowNotFound), Err(_err) => Err(sqlx::Error::RowNotFound),
} }
} }
pub async fn get_with_song_queue_id(
pool: &sqlx::PgPool,
song_queue_id: &uuid::Uuid,
) -> Result<MetadataQueue, sqlx::Error> {
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 { pub mod endpoint {
@@ -115,4 +182,32 @@ pub mod endpoint {
} }
} }
} }
pub async fn fetch_metadata(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::extract::Query(params): axum::extract::Query<super::request::fetch_metadata::Params>
) -> (
StatusCode,
Json<super::response::fetch_metadata::Response>,
) {
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))
}
}
}
} }