From 13015e0fe34d48d617c78ded386d1ebbf0e96f44 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 15 Aug 2026 11:36:31 -0400 Subject: [PATCH] Changing --- src/callers/coverart.rs | 41 ++++++--------------- src/repo/coverart.rs | 81 +++++++++++++++++++++++++---------------- 2 files changed, 62 insertions(+), 60 deletions(-) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 03ad137..93fae74 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -127,41 +127,24 @@ pub mod endpoint { coverart.title = song.album.clone(); coverart.file_type = file_type.file_type; coverart.data = data.raw_data; + coverart.file_key = new_file_key; - match coverart.save_to_filesystem() { - Ok(_) => { - match repo::coverart::create( - &pool, - &coverart, - &payload.song_id, - ) - .await - { - Ok(id) => { - coverart.song_id = payload.song_id; - coverart.id = id; - println!("Cover Art created"); + match repo::coverart::create(&pool, &coverart, &payload.song_id) + .await + { + Ok(id) => { + coverart.song_id = payload.song_id; + coverart.id = id; + println!("Cover Art created"); - response.message = String::from("Successful"); - response.data.push(coverart); + response.message = String::from("Successful"); + response.data.push(coverart); - (axum::http::StatusCode::OK, axum::Json(response)) - } - Err(err) => { - response.message = err.to_string(); - ( - axum::http::StatusCode::BAD_REQUEST, - axum::Json(response), - ) - } - } + (axum::http::StatusCode::OK, axum::Json(response)) } Err(err) => { response.message = err.to_string(); - ( - axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response), - ) + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } } diff --git a/src/repo/coverart.rs b/src/repo/coverart.rs index 09e7b6f..e997d27 100644 --- a/src/repo/coverart.rs +++ b/src/repo/coverart.rs @@ -7,13 +7,14 @@ pub async fn create( ) -> Result { let result = sqlx::query( r#" - INSERT INTO "coverart" (title, directory, filename, file_type, song_id) VALUES($1, $2, $3, $4, $5) RETURNING id; + INSERT INTO "coverart" (title, directory, filename, file_type, file_key, song_id) VALUES($1, $2, $3, $4, $5, $6) RETURNING id; "#, ) .bind(&coverart.title) .bind(&coverart.directory) .bind(&coverart.filename) .bind(&coverart.file_type) + .bind(&coverart.file_key) .bind(song_id) .fetch_one(pool) .await @@ -39,7 +40,7 @@ pub async fn get_coverart( ) -> Result { let result = sqlx::query( r#" - SELECT id, title, directory, filename, file_type, song_id FROM "coverart" WHERE id = $1; + SELECT id, title, directory, filename, file_type, file_key, song_id FROM "coverart" WHERE id = $1; "#, ) .bind(id) @@ -71,6 +72,10 @@ pub async fn get_coverart( .try_get("file_type") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), + file_key: row + .try_get("file_key") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), song_id: row .try_get("song_id") .map_err(|_e| sqlx::Error::RowNotFound) @@ -87,7 +92,7 @@ pub async fn get_coverart_with_song_id( ) -> Result { let result = sqlx::query( r#" - SELECT id, title, directory, filename, file_type, song_id FROM "coverart" WHERE song_id = $1; + SELECT id, title, directory, filename, file_type, file_key, song_id FROM "coverart" WHERE song_id = $1; "#, ) .bind(song_id) @@ -119,6 +124,10 @@ pub async fn get_coverart_with_song_id( .try_get("file_type") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), + file_key: row + .try_get("file_key") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), data: Vec::new(), song_id: row .try_get("song_id") @@ -137,7 +146,7 @@ pub async fn delete_coverart( r#" DELETE FROM "coverart" WHERE id = $1 - RETURNING id, title, directory, filename, file_type, song_id + RETURNING id, title, directory, filename, file_type, file_key, song_id "#, ) .bind(id) @@ -148,33 +157,43 @@ pub async fn delete_coverart( }); match result { - Ok(row) => Ok(simodels::coverart::CoverArt { - id: row - .try_get("id") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - title: row - .try_get("title") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - directory: row - .try_get("directory") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - filename: row - .try_get("filename") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - file_type: row - .try_get("file_type") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - song_id: row - .try_get("song_id") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - data: Vec::new(), - }), + Ok(row) => parse_row(&row).await, Err(_err) => Err(sqlx::Error::RowNotFound), } } + +async fn parse_row( + row: &sqlx::postgres::PgRow, +) -> Result { + Ok(simodels::coverart::CoverArt { + id: row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + title: row + .try_get("title") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + directory: row + .try_get("directory") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + filename: row + .try_get("filename") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + file_type: row + .try_get("file_type") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + file_key: row + .try_get("file_key") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + song_id: row + .try_get("song_id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + data: Vec::new(), + }) +}