Added callers moulde (#120)
* Added callers moulde * Code formatting * Added uuid dependency * Added queue_song endpoint * Added endpoint to router * Added dependencies for postgres * Added migrations directory * Added more code * Added migrations * Removed migration * Docker fixes * Updated gitignore * Updated env sample file * Endpoint url fix * Updated env sample * Updated docker file to copy migrations * Might undo later * Updated env sample and docker compose * Updated cargo dependencies * Saving changes * Workflow fix * Will need to take another look at the dependencies again * Added code * Test is not finished yet * Warning fix for now * Updated workflow to setup database * Workflow fix * Workflow fix attempt * Warning fix * Warning fix * Updated workflow * Added test files Might change later * Added more crates * Added more test code Not finished * Formatting * Close, but far * I believe it is now working * Formatting * Added more crates * Code cleanup * Code reformatting * Code cleanup * Crate cleanup * Cleanup
This commit was merged in pull request #120.
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
pub mod request {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default, Deserialize, Serialize)]
|
||||
pub struct Request {
|
||||
pub message: String,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default, Deserialize, Serialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
mod song_queue {
|
||||
use sqlx::Row;
|
||||
|
||||
#[derive(Debug, serde::Serialize, sqlx::FromRow)]
|
||||
pub struct InsertedData {
|
||||
pub id: uuid::Uuid,
|
||||
}
|
||||
|
||||
pub async fn insert(
|
||||
pool: &sqlx::PgPool,
|
||||
data: &Vec<u8>,
|
||||
filename: &String,
|
||||
) -> Result<uuid::Uuid, sqlx::Error> {
|
||||
let result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO "songQueue" (data, filename) VALUES($1, $2) RETURNING id;
|
||||
"#,
|
||||
)
|
||||
.bind(data)
|
||||
.bind(filename)
|
||||
.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 mod endpoint {
|
||||
use axum::{Json, http::StatusCode};
|
||||
use std::io::Write;
|
||||
|
||||
use crate::callers::song::song_queue;
|
||||
|
||||
pub async fn queue_song(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
mut multipart: axum::extract::Multipart,
|
||||
) -> (StatusCode, Json<super::response::Response>) {
|
||||
let mut results: Vec<uuid::Uuid> = Vec::new();
|
||||
let mut response = super::response::Response::default();
|
||||
|
||||
while let Some(field) = multipart.next_field().await.unwrap() {
|
||||
let name = field.name().unwrap().to_string();
|
||||
let file_name = field.file_name().unwrap().to_string();
|
||||
let content_type = field.content_type().unwrap().to_string();
|
||||
let data = field.bytes().await.unwrap();
|
||||
|
||||
println!(
|
||||
"Received file '{}' (name = '{}', content-type = '{}', size = {})",
|
||||
file_name,
|
||||
name,
|
||||
content_type,
|
||||
data.len()
|
||||
);
|
||||
|
||||
// Save the file to disk
|
||||
let mut file = std::fs::File::create(&file_name).unwrap();
|
||||
file.write_all(&data).unwrap();
|
||||
|
||||
let raw_data: Vec<u8> = data.to_vec();
|
||||
let queue_repo = song_queue::insert(&pool, &raw_data, &file_name)
|
||||
.await
|
||||
.unwrap();
|
||||
results.push(queue_repo);
|
||||
}
|
||||
|
||||
response.data = results;
|
||||
response.message = if response.data.is_empty() {
|
||||
String::from("Error")
|
||||
} else {
|
||||
String::from("Success")
|
||||
};
|
||||
|
||||
(StatusCode::OK, Json(response))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user