Code formatting

This commit is contained in:
kdeng00
2025-05-21 19:15:33 -04:00
parent dbb6a60682
commit 0854522135
2 changed files with 108 additions and 69 deletions
+67 -49
View File
@@ -56,7 +56,6 @@ pub mod response {
pub mod db { pub mod db {
use sqlx::Row; use sqlx::Row;
pub async fn insert(pool: &sqlx::PgPool, data: &Vec<u8>) -> Result<uuid::Uuid, sqlx::Error> { pub async fn insert(pool: &sqlx::PgPool, data: &Vec<u8>) -> Result<uuid::Uuid, sqlx::Error> {
let result = sqlx::query( let result = sqlx::query(
r#" r#"
@@ -103,51 +102,65 @@ pub mod db {
} }
} }
pub async fn get_coverart_queue_with_id(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result<super::CoverArtQueue, sqlx::Error> { pub async fn get_coverart_queue_with_id(
pool: &sqlx::PgPool,
id: &uuid::Uuid,
) -> Result<super::CoverArtQueue, sqlx::Error> {
let result = sqlx::query( let result = sqlx::query(
r#" r#"
SELECT id, song_queue_id FROM "coverartQueue" WHERE id = $1; SELECT id, song_queue_id FROM "coverartQueue" WHERE id = $1;
"# "#,
) )
.bind(id) .bind(id)
.fetch_one(pool) .fetch_one(pool)
.await .await
.map_err(|e| { .map_err(|e| {
eprintln!("Error querying data: {:?}", e); eprintln!("Error querying data: {:?}", e);
}); });
match result { match result {
Ok(row) => { Ok(row) => Ok(super::CoverArtQueue {
Ok(super::CoverArtQueue { id: row
id: row.try_get("id").map_err(|_e| sqlx::Error::RowNotFound).unwrap(), .try_get("id")
song_queue_id: row.try_get("song_queue_id").map_err(|_e| sqlx::Error::RowNotFound).unwrap(), .map_err(|_e| sqlx::Error::RowNotFound)
}) .unwrap(),
} song_queue_id: row
Err(_) => Err(sqlx::Error::RowNotFound) .try_get("song_queue_id")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(),
}),
Err(_) => Err(sqlx::Error::RowNotFound),
} }
} }
pub async fn get_coverart_queue_with_song_queue_id(pool: &sqlx::PgPool, song_queue_id: &uuid::Uuid) -> Result<super::CoverArtQueue, sqlx::Error> { pub async fn get_coverart_queue_with_song_queue_id(
pool: &sqlx::PgPool,
song_queue_id: &uuid::Uuid,
) -> Result<super::CoverArtQueue, sqlx::Error> {
let result = sqlx::query( let result = sqlx::query(
r#" r#"
SELECT id, song_queue_id FROM "coverartQueue" WHERE song_queue_id = $1; SELECT id, song_queue_id FROM "coverartQueue" WHERE song_queue_id = $1;
"# "#,
) )
.bind(song_queue_id) .bind(song_queue_id)
.fetch_one(pool) .fetch_one(pool)
.await .await
.map_err(|e| { .map_err(|e| {
eprintln!("Error querying data: {:?}", e); eprintln!("Error querying data: {:?}", e);
}); });
match result { match result {
Ok(row) => { Ok(row) => Ok(super::CoverArtQueue {
Ok(super::CoverArtQueue { id: row
id: row.try_get("id").map_err(|_e| sqlx::Error::RowNotFound).unwrap(), .try_get("id")
song_queue_id: row.try_get("song_queue_id").map_err(|_e| sqlx::Error::RowNotFound).unwrap(), .map_err(|_e| sqlx::Error::RowNotFound)
}) .unwrap(),
} song_queue_id: row
Err(_) => Err(sqlx::Error::RowNotFound) .try_get("song_queue_id")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(),
}),
Err(_) => Err(sqlx::Error::RowNotFound),
} }
} }
} }
@@ -227,27 +240,32 @@ pub mod endpoint {
pub async fn fetch_coverart_no_data( pub async fn fetch_coverart_no_data(
axum::Extension(pool): axum::Extension<sqlx::PgPool>, axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::extract::Query(params): axum::extract::Query<super::request::fetch_coverart_no_data::Params>, axum::extract::Query(params): axum::extract::Query<
) -> (axum::http::StatusCode, axum::Json<super::response::fetch_coverart_no_data::Response>) { super::request::fetch_coverart_no_data::Params,
>,
) -> (
axum::http::StatusCode,
axum::Json<super::response::fetch_coverart_no_data::Response>,
) {
let mut response = super::response::fetch_coverart_no_data::Response::default(); let mut response = super::response::fetch_coverart_no_data::Response::default();
match params.id { match params.id {
Some(id) => { Some(id) => match super::db::get_coverart_queue_with_id(&pool, &id).await {
match super::db::get_coverart_queue_with_id(&pool, &id).await { Ok(cover_art_queue) => {
Ok(cover_art_queue) => { response.message = String::from("Successful");
response.message = String::from("Successful"); response.data.push(cover_art_queue);
response.data.push(cover_art_queue); (axum::http::StatusCode::OK, axum::Json(response))
(axum::http::StatusCode::OK, axum::Json(response))
}
Err(err) => {
response.message = err.to_string();
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
} }
} Err(err) => {
response.message = err.to_string();
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
},
_ => match params.song_queue_id { _ => match params.song_queue_id {
Some(song_queue_id) => { Some(song_queue_id) => {
match super::db::get_coverart_queue_with_song_queue_id(&pool, &song_queue_id).await { match super::db::get_coverart_queue_with_song_queue_id(&pool, &song_queue_id)
.await
{
Ok(cover_art_queue) => { Ok(cover_art_queue) => {
response.message = String::from("Successful"); response.message = String::from("Successful");
response.data.push(cover_art_queue); response.data.push(cover_art_queue);
@@ -263,7 +281,7 @@ pub mod endpoint {
response.message = String::from("No valid id provided"); response.message = String::from("No valid id provided");
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) (axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
} }
} },
} }
} }
} }
+41 -20
View File
@@ -95,8 +95,8 @@ pub mod init {
) )
.route( .route(
crate::callers::endpoints::QUEUECOVERART, crate::callers::endpoints::QUEUECOVERART,
get(crate::callers::coverart::endpoint::fetch_coverart_no_data) get(crate::callers::coverart::endpoint::fetch_coverart_no_data),
) )
.route( .route(
crate::callers::endpoints::QUEUECOVERARTLINK, crate::callers::endpoints::QUEUECOVERARTLINK,
patch(crate::callers::coverart::endpoint::link), patch(crate::callers::coverart::endpoint::link),
@@ -302,18 +302,22 @@ mod tests {
app.clone().oneshot(req).await app.clone().oneshot(req).await
} }
async fn coverart_queue_song_queue_link_req(app: &axum::Router, coverart_id: &uuid::Uuid, song_queue_id: &uuid::Uuid) -> Result<axum::response::Response, std::convert::Infallible> { async fn coverart_queue_song_queue_link_req(
app: &axum::Router,
coverart_id: &uuid::Uuid,
song_queue_id: &uuid::Uuid,
) -> Result<axum::response::Response, std::convert::Infallible> {
let payload = serde_json::json!( let payload = serde_json::json!(
{ {
"song_queue_id": song_queue_id, "song_queue_id": song_queue_id,
"coverart_id" : coverart_id, "coverart_id" : coverart_id,
}); });
let req = axum::http::Request::builder() let req = axum::http::Request::builder()
.method(axum::http::Method::PATCH) .method(axum::http::Method::PATCH)
.uri(crate::callers::endpoints::QUEUECOVERARTLINK) .uri(crate::callers::endpoints::QUEUECOVERARTLINK)
.header(axum::http::header::CONTENT_TYPE, "application/json") .header(axum::http::header::CONTENT_TYPE, "application/json")
.body(axum::body::Body::from(payload.to_string())) .body(axum::body::Body::from(payload.to_string()))
.unwrap(); .unwrap();
app.clone().oneshot(req).await app.clone().oneshot(req).await
} }
@@ -718,7 +722,8 @@ mod tests {
let coverart_id = resp.data[0]; let coverart_id = resp.data[0];
assert_eq!(false, coverart_id.is_nil(), "Should not be empty"); 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 match coverart_queue_song_queue_link_req(&app, &coverart_id, &song_queue_id)
.await
{ {
Ok(response) => { Ok(response) => {
let resp = get_resp_data::< let resp = get_resp_data::<
@@ -880,7 +885,8 @@ mod tests {
let coverart_id = resp.data[0]; let coverart_id = resp.data[0];
assert_eq!(false, coverart_id.is_nil(), "Should not be empty"); 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 match coverart_queue_song_queue_link_req(&app, &coverart_id, &song_queue_id)
.await
{ {
Ok(response) => { Ok(response) => {
let resp = get_resp_data::< let resp = get_resp_data::<
@@ -898,22 +904,37 @@ mod tests {
"Should not be empty" "Should not be empty"
); );
let uri = format!("{}?id={}", crate::callers::endpoints::QUEUECOVERART, resp_coverart_id); let uri = format!(
"{}?id={}",
crate::callers::endpoints::QUEUECOVERART,
resp_coverart_id
);
match app.clone().oneshot( match app
axum::http::Request::builder() .clone()
.method(axum::http::Method::GET) .oneshot(
.uri(uri) axum::http::Request::builder()
.header(axum::http::header::CONTENT_TYPE, "application/json") .method(axum::http::Method::GET)
.body(axum::body::Body::empty()) .uri(uri)
.unwrap() .header(
).await { axum::http::header::CONTENT_TYPE,
"application/json",
)
.body(axum::body::Body::empty())
.unwrap(),
)
.await
{
Ok(response) => { Ok(response) => {
let resp = get_resp_data::< let resp = get_resp_data::<
crate::callers::coverart::response::fetch_coverart_no_data::Response, crate::callers::coverart::response::fetch_coverart_no_data::Response,
>(response) >(response)
.await; .await;
assert_eq!(false, resp.data.is_empty(), "Should not be empty"); assert_eq!(
false,
resp.data.is_empty(),
"Should not be empty"
);
} }
Err(err) => { Err(err) => {
assert!(false, "Error: {:?}", err); assert!(false, "Error: {:?}", err);