From 4ee0ba59fd4a784836c1e65d85d88e2d161aa827 Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 20 Nov 2026 15:08:43 -0500 Subject: [PATCH] tsk-271: Adding more code --- src/callers/mod.rs | 4 ++ src/callers/playback.rs | 88 +++++++++++++++++++++++++++++++++++++++++ src/repo/mod.rs | 1 + src/repo/playback.rs | 50 +++++++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 src/callers/playback.rs create mode 100644 src/repo/playback.rs diff --git a/src/callers/mod.rs b/src/callers/mod.rs index b938401..bbc966a 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -1,4 +1,5 @@ pub mod coverart; +pub mod playback; pub mod queue; pub mod song; @@ -14,10 +15,13 @@ pub mod endpoints { pub const CREATECOVERART: &str = "/api/v2/coverart"; pub const GETCOVERART: &str = "/api/v2/coverart"; pub const DOWNLOADCOVERART: &str = "/api/v2/coverart/download/{id}"; + + pub const CREATEPLAYBACKQUEUE: &str = "/api/v2/playback/queue"; } pub mod response { pub const SUCCESSFUL: &str = "SUCCESSFUL"; + pub const INVALID: &str = "INVALID"; } /// Basic handler that responds with a static string diff --git a/src/callers/playback.rs b/src/callers/playback.rs new file mode 100644 index 0000000..dd144e2 --- /dev/null +++ b/src/callers/playback.rs @@ -0,0 +1,88 @@ + + + +pub mod queue { + +#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] +pub struct PlaybackQueue { + pub id: uuid::Uuid, + pub name: String, + pub device_type: String, + pub user_id: uuid::Uuid, + #[serde(with = "time::serde::rfc3339::option")] + pub date_created: Option +} + + pub mod request { + pub mod create_playback_queue { + use serde::{Deserialize, Serialize}; + + #[derive(Debug, Default, Deserialize, Serialize)] + pub struct CreatePlaybackQueueRequest { + pub name: String, + pub device_type: String, + pub user_id: uuid::Uuid + } + + impl CreatePlaybackQueueRequest { + pub fn is_valid(&self) -> bool { + !self.user_id.is_nil() && !self.name.is_empty() && !self.user_id.is_nil() + } + } + } + } + + pub mod response { + pub mod create_playback_queue { + use serde::{Deserialize, Serialize}; + + #[derive(Debug, Default, Deserialize, Serialize)] + pub struct CreatePlaybackQueueResponse { + pub message: String, + pub data: Vec, + } + } + } + + pub mod endpoint { + use axum::{http, Extension, Json}; + use http::StatusCode; + + use crate::repo::playback as playback_repo; + + pub async fn create_playback_queue( + Extension(pool): Extension, + Json(payload): Json, + ) -> ( + axum::http::StatusCode, + axum::Json, + + ) { + let mut response = super::response::create_playback_queue::CreatePlaybackQueueResponse::default(); + + if !payload.is_valid() { + response.message = super::super::super::response::INVALID.to_string(); + (StatusCode::BAD_REQUEST, Json(response)) + } else { + let res = match playback_repo::queue::queue_existence_search(&pool, &payload.device_type, &payload.user_id).await { + Ok(result) => { + result + } + Err(err) => { + eprintln!("Error: {err:?}"); + response.message = err.to_string(); + return (StatusCode::INTERNAL_SERVER_ERROR, Json(response)) + } + }; + + if res { + // Do not create plaback queue + } else { + } + + (StatusCode::OK, Json(response)) + } + } + + } +} diff --git a/src/repo/mod.rs b/src/repo/mod.rs index ef83d6e..2d2bd5a 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -1,3 +1,4 @@ pub mod coverart; +pub mod playback; pub mod queue; pub mod song; diff --git a/src/repo/playback.rs b/src/repo/playback.rs new file mode 100644 index 0000000..4ebf315 --- /dev/null +++ b/src/repo/playback.rs @@ -0,0 +1,50 @@ + +pub mod queue { + use sqlx::{PgPool, Row}; + // use sqlx::postgres::PgRow; + use sqlx::Error as SqlxError; + use crate::callers::playback::queue::PlaybackQueue; + + pub async fn create_queue(pool: &PgPool, playback_queue: &PlaybackQueue) -> Result<(time::OffsetDateTime, uuid::Uuid), SqlxError> { + + let query = r#" + INSERT INTO "playbackQueue" (name, device_type, user_id) + VALUES ($1, $2, $3) + RETURNING date_created, id; + "#; + + match sqlx::query(query) + .bind(&playback_queue.name) + .bind(&playback_queue.device_type) + .bind(&playback_queue.user_id) + .fetch_one(pool) + .await { + Ok(row) => { + let id = row.try_get("id").map_err(|_e| SqlxError::RowNotFound).unwrap(); + let date_created = row.try_get("date_created").map_err(|_e| SqlxError::RowNotFound).unwrap(); + + Ok((id, date_created)) + } + Err(err) => { + Err(err) + } + } + } + + pub async fn queue_existence_search(pool: &PgPool, device_type: &str, user_id: &uuid::Uuid) -> Result { + Ok(false) + } + + // TODO: For later + /* + async fn parse_row(row: &PgRow) -> Result { + Ok(PlayBackQueue { + id: row.try_get("id").map_err(|_e| SqlxError::RowNotFound).unwrap(), + name: row.try_get("name").map_err(|_e| SqlxError::RowNotFound).unwrap(), + device_type: row.try_get("device_type").map_err(|_e| SqlxError::RowNotFound).unwrap(), + user_id: row.try_get("user_id").map_err(|_e| SqlxError::RowNotFound).unwrap(), + date_created: row.try_get("date_created").map_err(|_e| SqlxError::RowNotFound).unwrap() + }) + } + */ +}