diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index 72f5d10..6dd627f 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -74,136 +74,7 @@ pub mod response { #[derive(Default, Deserialize, Serialize, utoipa::ToSchema)] pub struct Response { pub message: String, - pub data: Vec, - } - } -} - -pub mod metadata_queue { - use sqlx::Row; - - #[derive(Debug, serde::Serialize, sqlx::FromRow)] - pub struct InsertedData { - pub id: uuid::Uuid, - } - - #[derive(Debug, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema)] - pub struct MetadataQueue { - pub id: uuid::Uuid, - pub metadata: serde_json::Value, - #[serde(with = "time::serde::rfc3339")] - pub created_at: time::OffsetDateTime, - pub song_queue_id: uuid::Uuid, - } - - pub async fn insert( - pool: &sqlx::PgPool, - metadata: &serde_json::Value, - song_queue_id: &uuid::Uuid, - ) -> Result { - let result = sqlx::query( - r#" - INSERT INTO "metadataQueue" (metadata, song_queue_id) VALUES($1, $2) RETURNING id; - "#, - ) - .bind(metadata) - .bind(song_queue_id) - .fetch_one(pool) - .await - .map_err(|e| { - eprintln!("Error inserting: {e}"); - }); - - match result { - Ok(row) => { - let id: uuid::Uuid = row - .try_get("id") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(); - Ok(id) - } - Err(_err) => Err(sqlx::Error::RowNotFound), - } - } - - pub async fn get_with_song_queue_id( - pool: &sqlx::PgPool, - song_queue_id: &uuid::Uuid, - ) -> Result { - let result = sqlx::query( - r#" - SELECT * FROM "metadataQueue" WHERE song_queue_id = $1; - "#, - ) - .bind(song_queue_id) - .fetch_one(pool) - .await - .map_err(|e| { - eprintln!("Error inserting: {e}"); - }); - - match result { - Ok(row) => Ok(MetadataQueue { - id: row - .try_get("id") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - metadata: row - .try_get("metadata") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - created_at: row - .try_get("created_at") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - song_queue_id: row - .try_get("song_queue_id") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - }), - Err(_err) => Err(sqlx::Error::RowNotFound), - } - } - - pub async fn get_with_id( - pool: &sqlx::PgPool, - id: &uuid::Uuid, - ) -> Result { - let result = sqlx::query( - r#" - SELECT id, metadata, created_at, song_queue_id FROM "metadataQueue" WHERE id = $1; - "#, - ) - .bind(id) - .fetch_one(pool) - .await - .map_err(|e| { - eprintln!("Error inserting: {e}"); - }); - - match result { - Ok(row) => { - let data: serde_json::Value = row - .try_get("metadata") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(); - Ok(MetadataQueue { - id: row - .try_get("id") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - metadata: data, - created_at: row - .try_get("created_at") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - song_queue_id: row - .try_get("song_queue_id") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - }) - } - Err(_err) => Err(sqlx::Error::RowNotFound), + pub data: Vec, } } } @@ -212,6 +83,8 @@ pub mod metadata_queue { pub mod endpoint { use axum::{Json, http::StatusCode}; + use crate::repo::queue as repo_queue; + /// Endpoint to create queued metadata #[utoipa::path( post, @@ -233,7 +106,7 @@ pub mod endpoint { let mut results: Vec = Vec::new(); let mut response = super::response::queue_metadata::Response::default(); let meta = payload.to_json_value().await; - match super::metadata_queue::insert(&pool, &meta, &payload.song_queue_id).await { + match repo_queue::metadata::insert(&pool, &meta, &payload.song_queue_id).await { Ok(metadata_queue_id) => { results.push(metadata_queue_id); response.data = results; @@ -275,7 +148,7 @@ pub mod endpoint { Some(id) => { println!("Something works {id}"); - match super::metadata_queue::get_with_id(&pool, &id).await { + match repo_queue::metadata::get_with_id(&pool, &id).await { Ok(item) => { response.message = String::from("Successful"); response.data.push(item); @@ -290,7 +163,7 @@ pub mod endpoint { _ => match params.song_queue_id { Some(song_queue_id) => { println!("Song queue Id is probably not nil"); - match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await + match repo_queue::metadata::get_with_song_queue_id(&pool, &song_queue_id).await { Ok(item) => { response.message = String::from("Successful"); diff --git a/src/repo/queue/metadata.rs b/src/repo/queue/metadata.rs new file mode 100644 index 0000000..921a762 --- /dev/null +++ b/src/repo/queue/metadata.rs @@ -0,0 +1,126 @@ +use sqlx::Row; + +#[derive(Debug, serde::Serialize, sqlx::FromRow)] +pub struct InsertedData { + pub id: uuid::Uuid, +} + +#[derive(Debug, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema)] +pub struct MetadataQueue { + pub id: uuid::Uuid, + pub metadata: serde_json::Value, + #[serde(with = "time::serde::rfc3339")] + pub created_at: time::OffsetDateTime, + pub song_queue_id: uuid::Uuid, +} + +pub async fn insert( + pool: &sqlx::PgPool, + metadata: &serde_json::Value, + song_queue_id: &uuid::Uuid, +) -> Result { + let result = sqlx::query( + r#" + INSERT INTO "metadataQueue" (metadata, song_queue_id) VALUES($1, $2) RETURNING id; + "#, + ) + .bind(metadata) + .bind(song_queue_id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting: {e}"); + }); + + match result { + Ok(row) => { + let id: uuid::Uuid = row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(); + Ok(id) + } + Err(_err) => Err(sqlx::Error::RowNotFound), + } +} + +pub async fn get_with_song_queue_id( + pool: &sqlx::PgPool, + song_queue_id: &uuid::Uuid, +) -> Result { + let result = sqlx::query( + r#" + SELECT * FROM "metadataQueue" WHERE song_queue_id = $1; + "#, + ) + .bind(song_queue_id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting: {e}"); + }); + + match result { + Ok(row) => Ok(MetadataQueue { + id: row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + metadata: row + .try_get("metadata") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + created_at: row + .try_get("created_at") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + song_queue_id: row + .try_get("song_queue_id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + }), + Err(_err) => Err(sqlx::Error::RowNotFound), + } +} + +pub async fn get_with_id( + pool: &sqlx::PgPool, + id: &uuid::Uuid, +) -> Result { + let result = sqlx::query( + r#" + SELECT id, metadata, created_at, song_queue_id FROM "metadataQueue" WHERE id = $1; + "#, + ) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting: {e}"); + }); + + match result { + Ok(row) => { + let data: serde_json::Value = row + .try_get("metadata") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(); + Ok(MetadataQueue { + id: row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + metadata: data, + created_at: row + .try_get("created_at") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + song_queue_id: row + .try_get("song_queue_id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + }) + } + Err(_err) => Err(sqlx::Error::RowNotFound), + } +} diff --git a/src/repo/queue/mod.rs b/src/repo/queue/mod.rs index 76ff890..c9a5203 100644 --- a/src/repo/queue/mod.rs +++ b/src/repo/queue/mod.rs @@ -1 +1,2 @@ +pub mod metadata; pub mod song;