Coverart queue fetch data (#131)

* Added coverart queue data fetch endpoint

* Added test

* Code formatting
This commit was merged in pull request #131.
This commit is contained in:
KD
2025-05-22 15:03:57 -04:00
committed by GitHub
parent 7d27aed314
commit b2a4faec76
3 changed files with 227 additions and 0 deletions
+114
View File
@@ -21,6 +21,14 @@ pub mod request {
pub song_queue_id: Option<uuid::Uuid>,
}
}
pub mod fetch_coverart_with_data {
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Params {
pub id: Option<uuid::Uuid>,
pub song_queue_id: Option<uuid::Uuid>,
}
}
}
pub mod response {
@@ -51,6 +59,14 @@ pub mod response {
pub data: Vec<super::super::CoverArtQueue>,
}
}
pub mod fetch_coverart_with_data {
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct Response {
pub message: String,
pub data: Vec<Vec<u8>>,
}
}
}
pub mod db {
@@ -163,6 +179,56 @@ pub mod db {
Err(_) => Err(sqlx::Error::RowNotFound),
}
}
pub async fn get_coverart_queue_data_with_id(
pool: &sqlx::PgPool,
id: &uuid::Uuid,
) -> Result<Vec<u8>, sqlx::Error> {
let result = sqlx::query(
r#"
SELECT data FROM "coverartQueue" WHERE id = $1;
"#,
)
.bind(id)
.fetch_one(pool)
.await
.map_err(|e| {
eprintln!("Error querying data: {:?}", e);
});
match result {
Ok(row) => Ok(row
.try_get("data")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap()),
Err(_) => Err(sqlx::Error::RowNotFound),
}
}
pub async fn get_coverart_queue_data_with_song_queue_id(
pool: &sqlx::PgPool,
song_queue_id: &uuid::Uuid,
) -> Result<Vec<u8>, sqlx::Error> {
let result = sqlx::query(
r#"
SELECT data FROM "coverartQueue" WHERE song_queue_id = $1;
"#,
)
.bind(song_queue_id)
.fetch_one(pool)
.await
.map_err(|e| {
eprintln!("Error querying data: {:?}", e);
});
match result {
Ok(row) => Ok(row
.try_get("data")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap()),
Err(_) => Err(sqlx::Error::RowNotFound),
}
}
}
pub mod endpoint {
@@ -284,4 +350,52 @@ pub mod endpoint {
},
}
}
pub async fn fetch_coverart_with_data(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::extract::Query(params): axum::extract::Query<
super::request::fetch_coverart_with_data::Params,
>,
) -> (
axum::http::StatusCode,
axum::Json<super::response::fetch_coverart_with_data::Response>,
) {
let mut response = super::response::fetch_coverart_with_data::Response::default();
match params.id {
Some(id) => match super::db::get_coverart_queue_data_with_id(&pool, &id).await {
Ok(cover_art_queue) => {
response.message = String::from("Successful");
response.data.push(cover_art_queue);
(axum::http::StatusCode::OK, axum::Json(response))
}
Err(err) => {
response.message = err.to_string();
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
},
_ => match params.song_queue_id {
Some(song_queue_id) => match super::db::get_coverart_queue_data_with_song_queue_id(
&pool,
&song_queue_id,
)
.await
{
Ok(cover_art_queue) => {
response.message = String::from("Successful");
response.data.push(cover_art_queue);
(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("No valid id provided");
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
},
}
}
}
+1
View File
@@ -8,5 +8,6 @@ pub mod endpoints {
pub const NEXTQUEUESONG: &str = "/api/v2/song/queue/next";
pub const QUEUEMETADATA: &str = "/api/v2/song/metadata/queue";
pub const QUEUECOVERART: &str = "/api/v2/coverart/queue";
pub const QUEUECOVERARTDATA: &str = "/api/v2/coverart/queue/data";
pub const QUEUECOVERARTLINK: &str = "/api/v2/coverart/queue/link";
}
+112
View File
@@ -93,6 +93,10 @@ pub mod init {
crate::callers::endpoints::QUEUECOVERART,
post(crate::callers::coverart::endpoint::queue),
)
.route(
crate::callers::endpoints::QUEUECOVERARTDATA,
get(crate::callers::coverart::endpoint::fetch_coverart_with_data),
)
.route(
crate::callers::endpoints::QUEUECOVERART,
get(crate::callers::coverart::endpoint::fetch_coverart_no_data),
@@ -958,4 +962,112 @@ mod tests {
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
}
#[tokio::test]
async fn test_fetch_coverart_queue_with_data() {
let tm_pool = db_mgr::get_pool().await.unwrap();
let db_name = db_mgr::generate_db_name().await;
match db_mgr::create_database(&tm_pool, &db_name).await {
Ok(_) => {
println!("Success");
}
Err(err) => {
assert!(false, "Error: {:?}", err);
}
}
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
db::migrations(&pool).await;
let app = init::app(pool).await;
match song_queue_req(&app).await {
Ok(response) => {
let resp =
get_resp_data::<crate::callers::coverart::response::Response>(response).await;
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
let song_queue_id = resp.data[0];
assert_eq!(false, song_queue_id.is_nil(), "Should not be empty");
// Send request
match upload_coverart_queue_req(&app).await {
Ok(response) => {
let resp =
get_resp_data::<crate::callers::coverart::response::Response>(response)
.await;
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
let coverart_id = resp.data[0];
assert_eq!(false, coverart_id.is_nil(), "Should not be empty");
match coverart_queue_song_queue_link_req(&app, &coverart_id, &song_queue_id)
.await
{
Ok(response) => {
let resp = get_resp_data::<
crate::callers::coverart::response::link::Response,
>(response)
.await;
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
let resp_coverart_id = resp.data[0].coverart_id;
let resp_song_queue_id = resp.data[0].song_queue_id;
assert_eq!(false, resp_coverart_id.is_nil(), "Should not be empty");
assert_eq!(
false,
resp_song_queue_id.is_nil(),
"Should not be empty"
);
let uri = format!(
"{}?id={}",
crate::callers::endpoints::QUEUECOVERARTDATA,
resp_coverart_id
);
match app
.clone()
.oneshot(
axum::http::Request::builder()
.method(axum::http::Method::GET)
.uri(uri)
.header(axum::http::header::CONTENT_TYPE, "image/jpeg")
.body(axum::body::Body::empty())
.unwrap(),
)
.await
{
Ok(response) => {
let resp = get_resp_data::<
crate::callers::coverart::response::fetch_coverart_with_data::Response,
>(response)
.await;
assert_eq!(
false,
resp.data.is_empty(),
"Should not be empty"
);
}
Err(err) => {
assert!(false, "Error: {:?}", err);
}
}
}
Err(err) => {
assert!(false, "Error: {:?}", err);
}
}
}
Err(err) => {
assert!(false, "Error: {:?}", err);
}
}
}
Err(err) => {
assert!(false, "Error: {:?}", err);
}
}
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
}
}