Update song queue status (#129)
* Changes to status definition * Added endpoint to update song_queue status * endpoint return fix * Endpoint now available * Added test * Code formatting * Clippy warning fix
This commit was merged in pull request #129.
This commit is contained in:
+139
-10
@@ -5,6 +5,14 @@ pub mod request {
|
||||
pub struct Request {
|
||||
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 {
|
||||
@@ -25,18 +33,35 @@ pub mod response {
|
||||
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 {
|
||||
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)]
|
||||
pub struct InsertedData {
|
||||
pub id: uuid::Uuid,
|
||||
@@ -96,8 +121,8 @@ mod song_queue {
|
||||
RETURNING id, filename, status;
|
||||
"#,
|
||||
)
|
||||
.bind(status::PROCESSING)
|
||||
.bind(status::PENDING)
|
||||
.bind(super::status::PROCESSING)
|
||||
.bind(super::status::PENDING)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.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> {
|
||||
let result = sqlx::query(
|
||||
r#"
|
||||
@@ -186,7 +263,7 @@ pub mod endpoint {
|
||||
&pool,
|
||||
&raw_data,
|
||||
&file_name,
|
||||
&song_queue::status::PENDING.to_string(),
|
||||
&super::status::PENDING.to_string(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -251,4 +328,56 @@ pub mod endpoint {
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user