Saving info

This commit is contained in:
2026-06-13 15:21:48 -04:00
parent 6a45bae1a9
commit a46b4321cb
2 changed files with 133 additions and 6 deletions
+129 -1
View File
@@ -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<String>,
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<textsender_models::contact::Contact>,
pub data: Vec<uuid::Uuid>,
}
}
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<sqlx::PgPool>,
axum::Json(payload): axum::Json<super::request::create_metadata::Request>,
) -> (
axum::http::StatusCode,
axum::Json<super::response::create_metadata::Response>,
) {
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}}"
*/
+4 -5
View File
@@ -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;