Saving changes
This commit is contained in:
+17
-66
@@ -26,6 +26,11 @@ pub mod contact {
|
|||||||
pub nickname: Option<String>,
|
pub nickname: Option<String>,
|
||||||
pub user_id: uuid::Uuid,
|
pub user_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AddContactRequest {
|
||||||
|
pub fn is_valid(&self) -> bool {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod response {
|
pub mod response {
|
||||||
@@ -45,88 +50,34 @@ pub mod contact {
|
|||||||
use axum::{Json, response::IntoResponse};
|
use axum::{Json, response::IntoResponse};
|
||||||
|
|
||||||
use crate::repo;
|
use crate::repo;
|
||||||
use crate::repo::queue as repo_queue;
|
use crate::repo::contact as contact_repo;
|
||||||
|
|
||||||
// TODO: Change the name of this endpoint. Including the function name and path
|
/// Endpoint to create Contact
|
||||||
/// Endpoint to create song
|
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
post,
|
post,
|
||||||
path = super::super::queue::endpoints::QUEUEMETADATA,
|
path = super::super::endpoints::ADD_CONTACT,
|
||||||
request_body(
|
request_body(
|
||||||
content = super::request::create_metadata::Request,
|
content = super::request::AddContactRequest,
|
||||||
description = "Data needed to create the song and save it to the filesystem",
|
description = "Data needed to create a Contact",
|
||||||
content_type = "application/json"
|
content_type = "application/json"
|
||||||
),
|
),
|
||||||
responses(
|
responses(
|
||||||
(status = 200, description = "Song created", body = super::response::create_metadata::Response),
|
(status = 201, description = "Contact created", body = super::response::AddContactResponse),
|
||||||
(status = 400, description = "Error", body = super::response::create_metadata::Response),
|
(status = 400, description = "Error", body = super::response::AddContactResponse),
|
||||||
(status = 505, description = "Error creating song", body = super::response::create_metadata::Response)
|
(status = 500, description = "Error creating Contact", body = super::response::AddContactResponse)
|
||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
pub async fn create_metadata(
|
pub async fn create_metadata(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::Json(payload): axum::Json<super::request::create_metadata::Request>,
|
axum::Json(payload): axum::Json<super::request::AddContactRequest>,
|
||||||
) -> (
|
) -> (
|
||||||
axum::http::StatusCode,
|
axum::http::StatusCode,
|
||||||
axum::Json<super::response::create_metadata::Response>,
|
axum::Json<super::response::AddContactResponse>,
|
||||||
) {
|
) {
|
||||||
let mut response = super::response::create_metadata::Response::default();
|
let mut response = super::response::AddContactResponse::default();
|
||||||
|
|
||||||
if payload.is_valid() {
|
if payload.is_valid() {
|
||||||
let mut song = payload.to_song();
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
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 {
|
} else {
|
||||||
response.message = String::from("Request body is not valid");
|
response.message = String::from("Request body is not valid");
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
|||||||
Reference in New Issue
Block a user