From a46b4321cb60693536ffd7b1697a3a54d0fd2f3c Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 13 Jun 2026 15:21:48 -0400 Subject: [PATCH] Saving info --- src/caller/mod.rs | 130 +++++++++++++++++++++++++++++++++++++++++++++- src/config/mod.rs | 9 ++-- 2 files changed, 133 insertions(+), 6 deletions(-) diff --git a/src/caller/mod.rs b/src/caller/mod.rs index 5dd76e5..7594512 100644 --- a/src/caller/mod.rs +++ b/src/caller/mod.rs @@ -13,4 +13,132 @@ pub async fn root() -> &'static str { "Hello, World!" } -pub mod contact {} +pub mod contact { + pub mod request { + use serde::{Deserialize, Serialize}; + use utoipa::ToSchema; + + #[derive(Debug, Deserialize, Serialize, ToSchema)] + pub struct AddContactRequest { + pub phone_number: String, + pub first_name: String, + pub last_name: String, + pub nickname: Option, + pub user_id: uuid::Uuid, + } + } + + pub mod response { + use serde::{Deserialize, Serialize}; + use utoipa::ToSchema; + + #[derive(Debug, Default, Deserialize, Serialize, ToSchema)] + pub struct AddContactResponse { + pub message: String, + // TODO: Should be this. Update code in textsender_models + // pub data: Vec, + pub data: Vec, + } + } + + pub mod endpoint { + use axum::{Json, response::IntoResponse}; + + use crate::repo; + use crate::repo::queue as repo_queue; + + // TODO: Change the name of this endpoint. Including the function name and path + /// Endpoint to create song + #[utoipa::path( + post, + path = super::super::queue::endpoints::QUEUEMETADATA, + request_body( + content = super::request::create_metadata::Request, + description = "Data needed to create the song and save it to the filesystem", + content_type = "application/json" + ), + responses( + (status = 200, description = "Song created", body = super::response::create_metadata::Response), + (status = 400, description = "Error", body = super::response::create_metadata::Response), + (status = 505, description = "Error creating song", body = super::response::create_metadata::Response) + ) + )] + pub async fn create_metadata( + axum::Extension(pool): axum::Extension, + axum::Json(payload): axum::Json, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::create_metadata::Response::default(); + + if payload.is_valid() { + let mut song = payload.to_song(); + song.filename = icarus_models::song::generate_filename( + icarus_models::types::MusicType::FlacExtension, + true, + ) + .unwrap(); + song.directory = icarus_envy::environment::get_root_directory().await.value; + + match repo_queue::song::get_data(&pool, &payload.song_queue_id).await { + Ok(data) => { + song.data = data; + let dir = std::path::Path::new(&song.directory); + if !dir.exists() { + println!("Creating directory"); + match std::fs::create_dir_all(dir) { + Ok(_) => { + println!("Successfully created directory"); + } + Err(err) => { + eprintln!("Error: Unable to create the directory {err:?}"); + } + } + } + + match song.save_to_filesystem() { + Ok(_) => match repo::song::insert(&pool, &song).await { + Ok((date_created, id)) => { + song.id = id; + song.date_created = Some(date_created); + response.message = String::from("Successful"); + response.data.push(song); + + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = + format!("{:?} song {:?}", err.to_string(), song); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + }, + Err(err) => { + response.message = err.to_string(); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) + } + } + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } + } else { + response.message = String::from("Request body is not valid"); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } + } +} + +/* +"phone_number": "+16303831708", +"first_name": "Bob", +"last_name": "De-buildor", +"nickname": "Bob", +"user_id": "{{user_id}}" +*/ diff --git a/src/config/mod.rs b/src/config/mod.rs index a768a75..8d58c8a 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -14,11 +14,10 @@ pub mod init { use tower_http::timeout::TimeoutLayer; use utoipa::OpenApi; - use crate::callers::coverart as coverart_caller; - use crate::callers::queue::coverart as coverart_queue_callers; - use crate::callers::queue::metadata as metadata_queue_caller; - use crate::callers::queue::song as song_queue_callers; - use crate::callers::song as song_caller; + use crate::caller::contact as contact_caller; + use contact_caller::endpoint as contact_endpoints; + use contact_caller::response as contact_responses; + use coverart_caller::endpoint as coverart_endpoints; use coverart_caller::response as coverart_responses; use coverart_queue_callers::endpoint as coverart_queue_endpoints;