Code refactor

This commit is contained in:
kdeng00
2025-04-25 22:54:30 -04:00
parent 6bd3a8210e
commit 38c1b3baeb
2 changed files with 36 additions and 25 deletions
+34 -24
View File
@@ -82,9 +82,7 @@ mod song_queue {
}
}
pub async fn get_most_recent_and_update(
pool: &sqlx::PgPool,
) -> Result<SongQueue, sqlx::Error> {
pub async fn get_most_recent_and_update(pool: &sqlx::PgPool) -> Result<SongQueue, sqlx::Error> {
let result = sqlx::query(
r#"
UPDATE "songQueue"
@@ -98,27 +96,35 @@ mod song_queue {
)
RETURNING *;
"#,
)
.bind(status::PROCESSING)
.bind(status::PENDING)
.fetch_one(pool)
.await
.map_err(|e| {
eprintln!("Error inserting: {}", e);
});
)
.bind(status::PROCESSING)
.bind(status::PENDING)
.fetch_one(pool)
.await
.map_err(|e| {
eprintln!("Error inserting: {}", e);
});
match result {
Ok(row) => {
Ok(SongQueue{
id: row.try_get("id").map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
filename: row.try_get("filename").map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
status: row.try_get("status").map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
data: row.try_get("data").map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
})
}
Err(err) => {
Err(sqlx::Error::RowNotFound)
}
Ok(row) => Ok(SongQueue {
id: row
.try_get("id")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(),
filename: row
.try_get("filename")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(),
status: row
.try_get("status")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(),
data: row
.try_get("data")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(),
}),
Err(err) => Err(sqlx::Error::RowNotFound),
}
}
}
@@ -176,8 +182,12 @@ pub mod endpoint {
(StatusCode::OK, Json(response))
}
pub async fn fetch_queue_song(axum::Extension(pool): axum::Extension<sqlx::PgPool>,
) -> (StatusCode, Json<super::response::fetch_queue_song::Response>) {
pub async fn fetch_queue_song(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
) -> (
StatusCode,
Json<super::response::fetch_queue_song::Response>,
) {
let mut response = super::response::fetch_queue_song::Response::default();
match song_queue::get_most_recent_and_update(&pool).await {
+2 -1
View File
@@ -69,7 +69,8 @@ pub mod init {
crate::callers::endpoints::QUEUESONG,
post(crate::callers::song::endpoint::queue_song),
)
.route(crate::callers::endpoints::NEXTQUEUESONG,
.route(
crate::callers::endpoints::NEXTQUEUESONG,
get(crate::callers::song::endpoint::fetch_queue_song),
)
.route(