From ad037472836a32248a9d173bb6ea96e20548dd3a Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Jul 2025 12:27:40 -0400 Subject: [PATCH] Added initial code to get coverart endpoint --- src/callers/coverart.rs | 72 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 8bbcd49..2d1a4bf 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -45,6 +45,13 @@ pub mod request { pub coverart_queue_id: uuid::Uuid, } } + + pub mod get_coverart { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Params { + pub id: Option, + } + } } pub mod response { @@ -99,6 +106,14 @@ pub mod response { pub data: Vec, } } + + pub mod get_coverart { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec + } + } } pub mod db { @@ -321,6 +336,36 @@ pub mod cov_db { Err(_err) => Err(sqlx::Error::RowNotFound), } } + + pub async fn get_coverart(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result { + let result = sqlx::query( + r#" + SELECT id, title, path, song_id FROM "coverart" WHERE id = $1; + "#, + ) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error querying data: {e:?}"); + }); + + match result { + Ok(row) => Ok(icarus_models::coverart::CoverArt { + id: row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + title: row + .try_get("title") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + path: row.try_get("path").map_err(|_e| sqlx::Error::RowNotFound).unwrap(), + song_id: row.try_get("song_id").map_err(|_e| sqlx::Error::RowNotFound).unwrap() + }), + Err(_) => Err(sqlx::Error::RowNotFound), + } + } } pub mod endpoint { @@ -560,4 +605,31 @@ pub mod endpoint { } } } + + pub async fn get_coverart( + axum::Extension(pool): axum::Extension, + axum::extract::Query(params): axum::extract::Query + ) -> (axum::http::StatusCode, axum::Json) { + let mut response = super::response::get_coverart::Response::default(); + + match params.id { + Some(id) => { + match super::cov_db::get_coverart(&pool, &id).await { + Ok(coverart) => { + response.data.push(coverart); + response.message = String::from(super::super::response::SUCCESSFUL); + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } + } + None => { + response.message = String::from("Invalid parameters"); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } + } }