Get coverart #163

Merged
kdeng00 merged 7 commits from get_coverart into v0.2 2025-07-26 14:12:03 -04:00
2 changed files with 31 additions and 21 deletions
Showing only changes of commit 84c0bc2d08 - Show all commits
+28 -18
View File
@@ -111,7 +111,7 @@ pub mod response {
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct Response {
pub message: String,
pub data: Vec<icarus_models::coverart::CoverArt>
pub data: Vec<icarus_models::coverart::CoverArt>,
}
}
}
@@ -337,7 +337,10 @@ pub mod cov_db {
}
}
pub async fn get_coverart(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result<icarus_models::coverart::CoverArt, sqlx::Error> {
pub async fn get_coverart(
pool: &sqlx::PgPool,
id: &uuid::Uuid,
) -> Result<icarus_models::coverart::CoverArt, sqlx::Error> {
let result = sqlx::query(
r#"
SELECT id, title, path, song_id FROM "coverart" WHERE id = $1;
@@ -360,9 +363,15 @@ pub mod cov_db {
.try_get("title")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(),
path: row.try_get("path").map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
path: row
.try_get("path")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(),
data: Vec::new(),
song_id: row.try_get("song_id").map_err(|_e| sqlx::Error::RowNotFound).unwrap()
song_id: row
.try_get("song_id")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(),
}),
Err(_) => Err(sqlx::Error::RowNotFound),
}
@@ -609,24 +618,25 @@ pub mod endpoint {
pub async fn get_coverart(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::extract::Query(params): axum::extract::Query<super::request::get_coverart::Params>
) -> (axum::http::StatusCode, axum::Json<super::response::get_coverart::Response>) {
axum::extract::Query(params): axum::extract::Query<super::request::get_coverart::Params>,
) -> (
axum::http::StatusCode,
axum::Json<super::response::get_coverart::Response>,
) {
let mut response = super::response::get_coverart::Response::default();
match params.id {
Some(id) => {
match super::cov_db::get_coverart(&pool, &id).await {
Ok(coverart) => {
response.data.push(coverart);
response.message = String::from(super::super::response::SUCCESSFUL);
(axum::http::StatusCode::OK, axum::Json(response))
}
Err(err) => {
response.message = err.to_string();
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
Some(id) => match super::cov_db::get_coverart(&pool, &id).await {
Ok(coverart) => {
response.data.push(coverart);
response.message = String::from(super::super::response::SUCCESSFUL);
(axum::http::StatusCode::OK, axum::Json(response))
}
}
Err(err) => {
response.message = err.to_string();
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
},
None => {
response.message = String::from("Invalid parameters");
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
+3 -3
View File
@@ -121,8 +121,8 @@ pub mod init {
)
.route(
crate::callers::endpoints::GETCOVERART,
get(crate::callers::coverart::endpoint::get_coverart)
)
get(crate::callers::coverart::endpoint::get_coverart),
)
}
pub async fn app() -> axum::Router {
@@ -1902,7 +1902,7 @@ mod tests {
let app = super::init::app(pool).await;
let mut id = uuid::Uuid::nil();
match uuid::Uuid::parse_str("996122cd-5ae9-4013-9934-60768d3006ed") {
Ok(val) => {
id = val;