diff --git a/src/callers/queue/song.rs b/src/callers/queue/song.rs index b0744c1..11ee342 100644 --- a/src/callers/queue/song.rs +++ b/src/callers/queue/song.rs @@ -213,28 +213,21 @@ pub mod endpoint { Ok(res) => { println!("Result: {res:?}"); - println!("Downloading file"); - match lr.download(&file_path).await { - Ok(res) => { - if res.is_empty() { - println!("This should not be empty"); - } else { - println!("Size: {:?}", res.len()); - println!("Going to delete file"); + println!("Saving to db"); - match lr.delete(&file_path).await { - Ok(res) => { - println!("Result: {res:?}"); - println!("Deleted"); - } - Err(err) => { - eprintln!("Error: {err:?}"); - } - } - } - } + match repo::data::insert( + &pool, + &file_path, + &lr.config.bucket, + &lr.config.region, + &queued_song, + ) + .await + { + Ok(_id) => {} Err(err) => { eprintln!("Error: {err:?}"); + return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)); } } } diff --git a/src/repo/queue/data.rs b/src/repo/queue/data.rs new file mode 100644 index 0000000..007826d --- /dev/null +++ b/src/repo/queue/data.rs @@ -0,0 +1,78 @@ +use sqlx::Row; + +pub async fn insert( + pool: &sqlx::PgPool, + file_key: &str, + bucket: &str, + region: &str, + song_queue_id: &uuid::Uuid, +) -> Result { + match sqlx::query( + r#" + INSERT INTO "songQueueData" (file_key, bucket, region, song_queue_id) VALUES($1, $2, $3, $4) RETURNING id; + "#, + ) + .bind(file_key) + .bind(bucket) + .bind(region) + .bind(song_queue_id) + .fetch_one(pool).await { + Ok(row) => { + let id: uuid::Uuid = row.try_get("id")?; + Ok(id) + } + Err(_err) => Err(sqlx::Error::RowNotFound), + } +} + +pub async fn get( + pool: &sqlx::PgPool, + id: &uuid::Uuid, +) -> Result<(uuid::Uuid, String, String, String, uuid::Uuid), sqlx::Error> { + match sqlx::query( + r#" + SELECT id, file_key, bucket, region, song_queue_id FROM "songQueueData" + WHERE id = $1 + "#, + ) + .bind(id) + .fetch_one(pool) + .await + { + Ok(row) => { + let file_key: String = row.try_get("file_key")?; + let bucket: String = row.try_get("bucket")?; + let region: String = row.try_get("region")?; + let song_queue_id: uuid::Uuid = row.try_get("song_queue_id")?; + + Ok((*id, file_key, bucket, region, song_queue_id)) + } + Err(err) => Err(err), + } +} + +pub async fn get_with_song_queue_id( + pool: &sqlx::PgPool, + song_queue_id: &uuid::Uuid, +) -> Result<(uuid::Uuid, String, String, String, uuid::Uuid), sqlx::Error> { + match sqlx::query( + r#" + SELECT id, file_key, bucket, region, song_queue_id FROM "songQueueData" + WHERE song_queue_id = $1 + "#, + ) + .bind(song_queue_id) + .fetch_one(pool) + .await + { + Ok(row) => { + let id: uuid::Uuid = row.try_get("id")?; + let file_key: String = row.try_get("file_key")?; + let bucket: String = row.try_get("bucket")?; + let region: String = row.try_get("region")?; + + Ok((id, file_key, bucket, region, *song_queue_id)) + } + Err(err) => Err(err), + } +} diff --git a/src/repo/queue/mod.rs b/src/repo/queue/mod.rs index 67284cf..28bd3e4 100644 --- a/src/repo/queue/mod.rs +++ b/src/repo/queue/mod.rs @@ -1,3 +1,4 @@ pub mod coverart; +pub mod data; pub mod metadata; pub mod song;