Link coverart with song queue #128

Merged
kdeng00 merged 7 commits from link_coverart_with_song_queue into v0.2 2025-05-19 21:54:50 -04:00
3 changed files with 190 additions and 19 deletions
+73
View File
@@ -1,9 +1,34 @@
pub mod request {
pub mod link {
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct Request {
pub coverart_id: uuid::Uuid,
pub song_queue_id: uuid::Uuid,
}
}
}
pub mod response { pub mod response {
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)] #[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct Response { pub struct Response {
pub message: String, pub message: String,
pub data: Vec<uuid::Uuid>, pub data: Vec<uuid::Uuid>,
} }
pub mod link {
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Id {
pub coverart_id: uuid::Uuid,
pub song_queue_id: uuid::Uuid,
}
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct Response {
pub message: String,
pub data: Vec<Id>,
}
}
} }
mod db { mod db {
@@ -33,6 +58,27 @@ mod db {
Err(_err) => Err(sqlx::Error::RowNotFound), Err(_err) => Err(sqlx::Error::RowNotFound),
} }
} }
pub async fn update(
pool: &sqlx::PgPool,
coverart_id: &uuid::Uuid,
song_queue_id: &uuid::Uuid,
) -> Result<i32, sqlx::Error> {
let result = sqlx::query(
r#"
UPDATE "coverartQueue" SET song_queue_id = $1 WHERE id = $2;
"#,
)
.bind(song_queue_id)
.bind(coverart_id)
.execute(pool)
.await;
match result {
Ok(_) => Ok(0),
Err(_err) => Err(sqlx::Error::RowNotFound),
}
}
} }
pub mod endpoint { pub mod endpoint {
@@ -80,4 +126,31 @@ pub mod endpoint {
} }
} }
} }
pub async fn link(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::Json(payload): axum::Json<super::request::link::Request>,
) -> (
axum::http::StatusCode,
axum::Json<super::response::link::Response>,
) {
let mut response = super::response::link::Response::default();
let id = payload.coverart_id;
let song_id = payload.song_queue_id;
match super::db::update(&pool, &id, &song_id).await {
Ok(_o) => {
response.data.push(super::response::link::Id {
song_queue_id: song_id,
coverart_id: id,
});
(axum::http::StatusCode::OK, axum::Json(response))
}
Err(err) => {
response.message = err.to_string();
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
}
}
} }
+1
View File
@@ -8,4 +8,5 @@ pub mod endpoints {
pub const NEXTQUEUESONG: &str = "/api/v2/song/queue/next"; pub const NEXTQUEUESONG: &str = "/api/v2/song/queue/next";
pub const QUEUEMETADATA: &str = "/api/v2/song/metadata/queue"; pub const QUEUEMETADATA: &str = "/api/v2/song/metadata/queue";
pub const QUEUECOVERART: &str = "/api/v2/coverart/queue"; pub const QUEUECOVERART: &str = "/api/v2/coverart/queue";
pub const QUEUECOVERARTLINK: &str = "/api/v2/coverart/queue/link";
} }
+116 -19
View File
@@ -58,7 +58,7 @@ async fn main() {
} }
pub mod init { pub mod init {
use axum::routing::{get, post}; use axum::routing::{get, patch, post};
use std::time::Duration; use std::time::Duration;
use tower_http::timeout::TimeoutLayer; use tower_http::timeout::TimeoutLayer;
@@ -89,6 +89,10 @@ pub mod init {
crate::callers::endpoints::QUEUECOVERART, crate::callers::endpoints::QUEUECOVERART,
post(crate::callers::coverart::endpoint::queue), post(crate::callers::coverart::endpoint::queue),
) )
.route(
crate::callers::endpoints::QUEUECOVERARTLINK,
patch(crate::callers::coverart::endpoint::link),
)
} }
pub async fn app() -> axum::Router { pub async fn app() -> axum::Router {
@@ -269,6 +273,27 @@ mod tests {
app.clone().oneshot(req).await app.clone().oneshot(req).await
} }
async fn upload_coverart_queue_req(
app: &axum::Router,
) -> Result<axum::response::Response, std::convert::Infallible> {
let mut form = MultipartForm::default();
let _ = form.add_file("jpg", "tests/Machine_gun/160809_machinegun.jpg");
// Create request
let content_type = form.content_type();
let body = MultipartBody::from(form);
let req = axum::http::Request::builder()
.method(axum::http::Method::POST)
.uri(crate::callers::endpoints::QUEUECOVERART)
.header(axum::http::header::CONTENT_TYPE, content_type)
.body(axum::body::Body::from_stream(body))
.unwrap();
// Send request
app.clone().oneshot(req).await
}
pub async fn resp_to_bytes( pub async fn resp_to_bytes(
response: axum::response::Response, response: axum::response::Response,
) -> Result<axum::body::Bytes, axum::Error> { ) -> Result<axum::body::Bytes, axum::Error> {
@@ -614,26 +639,9 @@ mod tests {
db::migrations(&pool).await; db::migrations(&pool).await;
let app = init::app(pool).await; let app = init::app(pool).await;
let mut form = MultipartForm::default();
let _ = form.add_file("jpg", "tests/Machine_gun/160809_machinegun.jpg");
// Create request
let content_type = form.content_type();
let body = MultipartBody::from(form);
// Send request // Send request
match app match upload_coverart_queue_req(&app).await {
.clone()
.oneshot(
axum::http::Request::builder()
.method(axum::http::Method::POST)
.uri(crate::callers::endpoints::QUEUECOVERART)
.header(axum::http::header::CONTENT_TYPE, content_type)
.body(axum::body::Body::from_stream(body))
.unwrap(),
)
.await
{
Ok(response) => { Ok(response) => {
let resp = let resp =
get_resp_data::<crate::callers::coverart::response::Response>(response).await; get_resp_data::<crate::callers::coverart::response::Response>(response).await;
@@ -648,4 +656,93 @@ mod tests {
let _ = db_mgr::drop_database(&tm_pool, &db_name).await; let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
} }
#[tokio::test]
async fn test_song_coverart_queue_link() {
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");
let payload = serde_json::json!(
{
"song_queue_id": song_queue_id,
"coverart_id" : coverart_id,
});
match app
.clone()
.oneshot(
axum::http::Request::builder()
.method(axum::http::Method::PATCH)
.uri(crate::callers::endpoints::QUEUECOVERARTLINK)
.header(axum::http::header::CONTENT_TYPE, "application/json")
.body(axum::body::Body::from(payload.to_string()))
.unwrap(),
)
.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"
);
}
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;
}
} }