Code refactor
This commit is contained in:
+34
-24
@@ -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
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user