Update song queue status #129

Merged
kdeng00 merged 7 commits from update_song_queue_status into v0.2 2025-05-20 20:42:34 -04:00
2 changed files with 232 additions and 10 deletions
+139 -10
View File
@@ -5,6 +5,14 @@ pub mod request {
pub struct Request { pub struct Request {
pub message: String, pub message: String,
} }
pub mod update_status {
#[derive(Default, serde::Deserialize, serde::Serialize)]
pub struct Request {
pub id: uuid::Uuid,
pub status: String,
}
}
} }
pub mod response { pub mod response {
@@ -25,18 +33,35 @@ pub mod response {
pub data: Vec<crate::callers::song::song_queue::SongQueue>, pub data: Vec<crate::callers::song::song_queue::SongQueue>,
} }
} }
pub mod update_status {
#[derive(serde::Deserialize, serde::Serialize)]
pub struct ChangedStatus {
pub old_status: String,
pub new_status: String,
}
#[derive(Default, serde::Deserialize, serde::Serialize)]
pub struct Response {
pub message: String,
pub data: Vec<ChangedStatus>,
}
}
}
pub mod status {
pub const PENDING: &str = "pending";
pub const PROCESSING: &str = "processing";
pub const DONE: &str = "done";
pub async fn is_valid(status: &str) -> bool {
status == PENDING || status == PROCESSING || status == DONE
}
} }
mod song_queue { mod song_queue {
use sqlx::Row; use sqlx::Row;
pub mod status {
pub const PENDING: &str = "pending";
// Will be used later on
pub const PROCESSING: &str = "processing";
pub const _DONE: &str = "done";
}
#[derive(Debug, serde::Serialize, sqlx::FromRow)] #[derive(Debug, serde::Serialize, sqlx::FromRow)]
pub struct InsertedData { pub struct InsertedData {
pub id: uuid::Uuid, pub id: uuid::Uuid,
@@ -96,8 +121,8 @@ mod song_queue {
RETURNING id, filename, status; RETURNING id, filename, status;
"#, "#,
) )
.bind(status::PROCESSING) .bind(super::status::PROCESSING)
.bind(status::PENDING) .bind(super::status::PENDING)
.fetch_one(pool) .fetch_one(pool)
.await .await
.map_err(|e| { .map_err(|e| {
@@ -123,6 +148,58 @@ mod song_queue {
} }
} }
pub async fn get_status_of_song_queue(
pool: &sqlx::PgPool,
id: &uuid::Uuid,
) -> Result<String, sqlx::Error> {
let result = sqlx::query(
r#"
SELECT id, status FROM "songQueue" WHERE id = $1
"#,
)
.bind(id)
.fetch_one(pool)
.await
.map_err(|e| {
eprintln!("Error selecting: {:?}", e);
});
match result {
Ok(row) => Ok(row
.try_get("status")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap()),
Err(_err) => Err(sqlx::Error::RowNotFound),
}
}
pub async fn update_song_queue_status(
pool: &sqlx::PgPool,
status: &String,
id: &uuid::Uuid,
) -> Result<String, sqlx::Error> {
let result = sqlx::query(
r#"
UPDATE "songQueue" SET status = $1 WHERE id = $2 RETURNING status;
"#,
)
.bind(status)
.bind(id)
.fetch_one(pool)
.await
.map_err(|e| {
eprintln!("Error updating record {:?}", e);
});
match result {
Ok(row) => Ok(row
.try_get("status")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap()),
Err(_) => Err(sqlx::Error::RowNotFound),
}
}
pub async fn get_data(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result<Vec<u8>, sqlx::Error> { pub async fn get_data(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result<Vec<u8>, sqlx::Error> {
let result = sqlx::query( let result = sqlx::query(
r#" r#"
@@ -186,7 +263,7 @@ pub mod endpoint {
&pool, &pool,
&raw_data, &raw_data,
&file_name, &file_name,
&song_queue::status::PENDING.to_string(), &super::status::PENDING.to_string(),
) )
.await .await
.unwrap(); .unwrap();
@@ -251,4 +328,56 @@ pub mod endpoint {
Err(_err) => (StatusCode::BAD_REQUEST, axum::response::Response::default()), Err(_err) => (StatusCode::BAD_REQUEST, axum::response::Response::default()),
} }
} }
pub async fn update_song_queue_status(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::Json(payload): axum::Json<super::request::update_status::Request>,
) -> (
axum::http::StatusCode,
axum::Json<super::response::update_status::Response>,
) {
let mut response = super::response::update_status::Response::default();
if super::status::is_valid(&payload.status).await {
let id = payload.id;
if !id.is_nil() {
match super::song_queue::get_status_of_song_queue(&pool, &id).await {
Ok(old) => {
match super::song_queue::update_song_queue_status(
&pool,
&payload.status,
&id,
)
.await
{
Ok(new) => {
response.message = String::from("Successful");
response
.data
.push(super::response::update_status::ChangedStatus {
old_status: old,
new_status: new,
});
(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))
}
}
} else {
response.message = String::from("Id is nil");
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
} else {
response.message = String::from("Status not valid");
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
}
} }
+93
View File
@@ -69,6 +69,10 @@ pub mod init {
crate::callers::endpoints::QUEUESONG, crate::callers::endpoints::QUEUESONG,
post(crate::callers::song::endpoint::queue_song), post(crate::callers::song::endpoint::queue_song),
) )
.route(
crate::callers::endpoints::QUEUESONG,
patch(crate::callers::song::endpoint::update_song_queue_status),
)
.route( .route(
crate::callers::endpoints::QUEUESONGDATA, crate::callers::endpoints::QUEUESONGDATA,
get(crate::callers::song::endpoint::download_flac), get(crate::callers::song::endpoint::download_flac),
@@ -745,4 +749,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_queue_update_status() {
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;
// Send request
match song_queue_req(&app).await {
Ok(response) => {
let resp =
get_resp_data::<crate::callers::song::response::Response>(response).await;
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
match fetch_queue_req(&app).await {
Ok(response) => {
let resp = get_resp_data::<
crate::callers::song::response::fetch_queue_song::Response,
>(response)
.await;
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
let old = &resp.data[0].status;
let done = crate::callers::song::status::DONE;
let payload = serde_json::json!({
"id": &resp.data[0].id,
"status": done,
});
match app
.clone()
.oneshot(
axum::http::Request::builder()
.method(axum::http::Method::PATCH)
.uri(crate::callers::endpoints::QUEUESONG)
.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::song::response::update_status::Response,
>(response)
.await;
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
let changed_status = &resp.data[0];
assert_eq!(
*old, changed_status.old_status,
"Old status does not match"
);
assert_eq!(
done, changed_status.new_status,
"New status does not match"
);
}
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;
}
} }