tsk-209: Refactored metadata repo code
This commit is contained in:
+6
-133
@@ -74,136 +74,7 @@ pub mod response {
|
|||||||
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<crate::callers::metadata::metadata_queue::MetadataQueue>,
|
pub data: Vec<crate::repo::queue::metadata::MetadataQueue>,
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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<uuid::Uuid, sqlx::Error> {
|
|
||||||
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<MetadataQueue, sqlx::Error> {
|
|
||||||
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<MetadataQueue, sqlx::Error> {
|
|
||||||
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),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -212,6 +83,8 @@ pub mod metadata_queue {
|
|||||||
pub mod endpoint {
|
pub mod endpoint {
|
||||||
use axum::{Json, http::StatusCode};
|
use axum::{Json, http::StatusCode};
|
||||||
|
|
||||||
|
use crate::repo::queue as repo_queue;
|
||||||
|
|
||||||
/// Endpoint to create queued metadata
|
/// Endpoint to create queued metadata
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
post,
|
post,
|
||||||
@@ -233,7 +106,7 @@ pub mod endpoint {
|
|||||||
let mut results: Vec<uuid::Uuid> = Vec::new();
|
let mut results: Vec<uuid::Uuid> = Vec::new();
|
||||||
let mut response = super::response::queue_metadata::Response::default();
|
let mut response = super::response::queue_metadata::Response::default();
|
||||||
let meta = payload.to_json_value().await;
|
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) => {
|
Ok(metadata_queue_id) => {
|
||||||
results.push(metadata_queue_id);
|
results.push(metadata_queue_id);
|
||||||
response.data = results;
|
response.data = results;
|
||||||
@@ -275,7 +148,7 @@ pub mod endpoint {
|
|||||||
Some(id) => {
|
Some(id) => {
|
||||||
println!("Something works {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) => {
|
Ok(item) => {
|
||||||
response.message = String::from("Successful");
|
response.message = String::from("Successful");
|
||||||
response.data.push(item);
|
response.data.push(item);
|
||||||
@@ -290,7 +163,7 @@ pub mod endpoint {
|
|||||||
_ => match params.song_queue_id {
|
_ => match params.song_queue_id {
|
||||||
Some(song_queue_id) => {
|
Some(song_queue_id) => {
|
||||||
println!("Song queue Id is probably not nil");
|
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) => {
|
Ok(item) => {
|
||||||
response.message = String::from("Successful");
|
response.message = String::from("Successful");
|
||||||
|
|||||||
@@ -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<uuid::Uuid, sqlx::Error> {
|
||||||
|
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<MetadataQueue, sqlx::Error> {
|
||||||
|
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<MetadataQueue, sqlx::Error> {
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1,2 @@
|
|||||||
|
pub mod metadata;
|
||||||
pub mod song;
|
pub mod song;
|
||||||
|
|||||||
Reference in New Issue
Block a user