Create metadata #133
+40
-25
@@ -35,10 +35,19 @@ pub mod request {
|
|||||||
|
|
||||||
impl Request {
|
impl Request {
|
||||||
pub fn is_valid(&self) -> bool {
|
pub fn is_valid(&self) -> bool {
|
||||||
!self.title.is_empty() || !self.artist.is_empty() || !self.album_artist.is_empty()
|
!self.title.is_empty()
|
||||||
|| !self.album.is_empty() || !self.genre.is_empty() || !self.date.is_empty()
|
|| !self.artist.is_empty()
|
||||||
|| self.track > 0 || self.disc > 0 || self.track_count > 0 || self.disc_count > 0
|
|| !self.album_artist.is_empty()
|
||||||
|| self.duration > 0 || !self.audio_type.is_empty() || !self.user_id.is_nil()
|
|| !self.album.is_empty()
|
||||||
|
|| !self.genre.is_empty()
|
||||||
|
|| !self.date.is_empty()
|
||||||
|
|| self.track > 0
|
||||||
|
|| self.disc > 0
|
||||||
|
|| self.track_count > 0
|
||||||
|
|| self.disc_count > 0
|
||||||
|
|| self.duration > 0
|
||||||
|
|| !self.audio_type.is_empty()
|
||||||
|
|| !self.user_id.is_nil()
|
||||||
|| !self.song_queue_id.is_nil()
|
|| !self.song_queue_id.is_nil()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +123,7 @@ pub mod response {
|
|||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<icarus_models::song::Song>
|
pub data: Vec<icarus_models::song::Song>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -135,7 +144,10 @@ mod song_db {
|
|||||||
use sqlx::Row;
|
use sqlx::Row;
|
||||||
|
|
||||||
// TODO: Change first parameter of return value from string to a time type
|
// TODO: Change first parameter of return value from string to a time type
|
||||||
pub async fn insert(pool: &sqlx::PgPool, song: &icarus_models::song::Song) -> Result<(String, uuid::Uuid), sqlx::Error> {
|
pub async fn insert(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
song: &icarus_models::song::Song,
|
||||||
|
) -> Result<(String, uuid::Uuid), sqlx::Error> {
|
||||||
let result = sqlx::query(
|
let result = sqlx::query(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO "song" (title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, filename, directory, user_id)
|
INSERT INTO "song" (title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, filename, directory, user_id)
|
||||||
@@ -169,14 +181,15 @@ mod song_db {
|
|||||||
.try_get("id")
|
.try_get("id")
|
||||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let date_created_time: time::OffsetDateTime = row.try_get("date_created").map_err(|_e| sqlx::Error::RowNotFound).unwrap();
|
let date_created_time: time::OffsetDateTime = row
|
||||||
|
.try_get("date_created")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap();
|
||||||
let date_created = date_created_time.to_string();
|
let date_created = date_created_time.to_string();
|
||||||
|
|
||||||
Ok((date_created, id))
|
Ok((date_created, id))
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||||
Err(sqlx::Error::RowNotFound)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -575,12 +588,16 @@ pub mod endpoint {
|
|||||||
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::create_metadata::Request>,
|
||||||
) -> (axum::http::StatusCode, axum::Json<super::response::create_metadata::Response>) {
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<super::response::create_metadata::Response>,
|
||||||
|
) {
|
||||||
let mut response = super::response::create_metadata::Response::default();
|
let mut response = super::response::create_metadata::Response::default();
|
||||||
|
|
||||||
if payload.is_valid() {
|
if payload.is_valid() {
|
||||||
let mut song = payload.to_song();
|
let mut song = payload.to_song();
|
||||||
song.filename = song.generate_filename(icarus_models::types::MusicTypes::FlacExtension, true);
|
song.filename =
|
||||||
|
song.generate_filename(icarus_models::types::MusicTypes::FlacExtension, true);
|
||||||
song.directory = crate::environment::get_root_directory().await.unwrap();
|
song.directory = crate::environment::get_root_directory().await.unwrap();
|
||||||
|
|
||||||
match song_queue::get_data(&pool, &payload.song_queue_id).await {
|
match song_queue::get_data(&pool, &payload.song_queue_id).await {
|
||||||
@@ -593,21 +610,19 @@ pub mod endpoint {
|
|||||||
file.write_all(&song.data).unwrap();
|
file.write_all(&song.data).unwrap();
|
||||||
|
|
||||||
match song.song_path() {
|
match song.song_path() {
|
||||||
Ok(_) => {
|
Ok(_) => match super::song_db::insert(&pool, &song).await {
|
||||||
match super::song_db::insert(&pool, &song).await {
|
Ok((date_created, id)) => {
|
||||||
Ok((date_created, id)) => {
|
song.id = id;
|
||||||
song.id = id;
|
song.date_created = date_created;
|
||||||
song.date_created = date_created;
|
response.data.push(song);
|
||||||
response.data.push(song);
|
|
||||||
|
|
||||||
(axum::http::StatusCode::OK, axum::Json(response))
|
(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 = format!("{:?} song {:?}", err.to_string(), song);
|
||||||
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
}
|
||||||
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
response.message = err.to_string();
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
pub async fn get_db_url() -> String {
|
pub async fn get_db_url() -> String {
|
||||||
dotenvy::dotenv().ok();
|
dotenvy::dotenv().ok();
|
||||||
std::env::var(crate::keys::DBURL).expect(crate::keys::error::ERROR)
|
std::env::var(crate::keys::DBURL).expect(crate::keys::error::ERROR)
|
||||||
|
|||||||
+48
-33
@@ -99,7 +99,7 @@ pub mod init {
|
|||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
crate::callers::endpoints::CREATESONG,
|
crate::callers::endpoints::CREATESONG,
|
||||||
post(crate::callers::song::endpoint::create_metadata)
|
post(crate::callers::song::endpoint::create_metadata),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,15 +269,18 @@ mod tests {
|
|||||||
app.clone().oneshot(fetch_req).await
|
app.clone().oneshot(fetch_req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_metadata_queue_req(app: &axum::Router, id: &uuid::Uuid) -> Result<axum::response::Response, std::convert::Infallible> {
|
async fn fetch_metadata_queue_req(
|
||||||
|
app: &axum::Router,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||||
let uri = format!("{}?id={}", crate::callers::endpoints::QUEUEMETADATA, id);
|
let uri = format!("{}?id={}", crate::callers::endpoints::QUEUEMETADATA, id);
|
||||||
|
|
||||||
let req = axum::http::Request::builder()
|
let req = axum::http::Request::builder()
|
||||||
.method(axum::http::Method::GET)
|
.method(axum::http::Method::GET)
|
||||||
.uri(uri)
|
.uri(uri)
|
||||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||||
.body(axum::body::Body::empty())
|
.body(axum::body::Body::empty())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
app.clone().oneshot(req).await
|
app.clone().oneshot(req).await
|
||||||
}
|
}
|
||||||
@@ -321,14 +324,18 @@ mod tests {
|
|||||||
app.clone().oneshot(req).await
|
app.clone().oneshot(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn queue_metadata_req(app: &axum::Router, id: &uuid::Uuid) -> Result<axum::response::Response, std::convert::Infallible> {
|
async fn queue_metadata_req(
|
||||||
|
app: &axum::Router,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||||
let payload = payload_data(&id).await;
|
let payload = payload_data(&id).await;
|
||||||
|
|
||||||
let req = axum::http::Request::builder()
|
let req = axum::http::Request::builder()
|
||||||
.method(axum::http::Method::POST)
|
.method(axum::http::Method::POST)
|
||||||
.uri(crate::callers::endpoints::QUEUEMETADATA)
|
.uri(crate::callers::endpoints::QUEUEMETADATA)
|
||||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||||
.body(axum::body::Body::from(payload.to_string())).unwrap();
|
.body(axum::body::Body::from(payload.to_string()))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
app.clone().oneshot(req).await
|
app.clone().oneshot(req).await
|
||||||
}
|
}
|
||||||
@@ -562,8 +569,7 @@ mod tests {
|
|||||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||||
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
||||||
|
|
||||||
match queue_metadata_req(&app, &resp.data[0]).await
|
match queue_metadata_req(&app, &resp.data[0]).await {
|
||||||
{
|
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let resp =
|
let resp =
|
||||||
get_resp_data::<crate::callers::song::response::Response>(response)
|
get_resp_data::<crate::callers::song::response::Response>(response)
|
||||||
@@ -610,8 +616,7 @@ mod tests {
|
|||||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||||
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
||||||
|
|
||||||
match queue_metadata_req(&app, &resp.data[0]).await
|
match queue_metadata_req(&app, &resp.data[0]).await {
|
||||||
{
|
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let resp =
|
let resp =
|
||||||
get_resp_data::<crate::callers::song::response::Response>(response)
|
get_resp_data::<crate::callers::song::response::Response>(response)
|
||||||
@@ -620,8 +625,7 @@ mod tests {
|
|||||||
|
|
||||||
let id = resp.data[0];
|
let id = resp.data[0];
|
||||||
|
|
||||||
match fetch_metadata_queue_req(&app, &id).await
|
match fetch_metadata_queue_req(&app, &id).await {
|
||||||
{
|
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let resp = get_resp_data::<
|
let resp = get_resp_data::<
|
||||||
crate::callers::metadata::response::fetch_metadata::Response,
|
crate::callers::metadata::response::fetch_metadata::Response,
|
||||||
@@ -1215,8 +1219,7 @@ mod tests {
|
|||||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||||
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
||||||
|
|
||||||
match queue_metadata_req(&app, &resp.data[0]).await
|
match queue_metadata_req(&app, &resp.data[0]).await {
|
||||||
{
|
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let resp =
|
let resp =
|
||||||
get_resp_data::<crate::callers::song::response::Response>(response)
|
get_resp_data::<crate::callers::song::response::Response>(response)
|
||||||
@@ -1225,8 +1228,7 @@ mod tests {
|
|||||||
|
|
||||||
let id = resp.data[0];
|
let id = resp.data[0];
|
||||||
|
|
||||||
match fetch_metadata_queue_req(&app, &id).await
|
match fetch_metadata_queue_req(&app, &id).await {
|
||||||
{
|
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let resp = get_resp_data::<
|
let resp = get_resp_data::<
|
||||||
crate::callers::metadata::response::fetch_metadata::Response,
|
crate::callers::metadata::response::fetch_metadata::Response,
|
||||||
@@ -1235,7 +1237,6 @@ mod tests {
|
|||||||
assert_eq!(false, resp.data.is_empty(), "Data should not be empty");
|
assert_eq!(false, resp.data.is_empty(), "Data should not be empty");
|
||||||
let song_q_id = resp.data[0].song_queue_id;
|
let song_q_id = resp.data[0].song_queue_id;
|
||||||
|
|
||||||
|
|
||||||
let payload = serde_json::json!({
|
let payload = serde_json::json!({
|
||||||
"title": "Power of Soul",
|
"title": "Power of Soul",
|
||||||
"artist": "Jimmi Hendrix",
|
"artist": "Jimmi Hendrix",
|
||||||
@@ -1255,23 +1256,37 @@ mod tests {
|
|||||||
|
|
||||||
eprintln!("Payload: {:?}", payload);
|
eprintln!("Payload: {:?}", payload);
|
||||||
|
|
||||||
match app.clone().oneshot(
|
match app
|
||||||
axum::http::Request::builder()
|
.clone()
|
||||||
.method(axum::http::Method::POST)
|
.oneshot(
|
||||||
.uri(crate::callers::endpoints::CREATESONG)
|
axum::http::Request::builder()
|
||||||
.header(
|
.method(axum::http::Method::POST)
|
||||||
axum::http::header::CONTENT_TYPE,
|
.uri(crate::callers::endpoints::CREATESONG)
|
||||||
"application/json"
|
.header(
|
||||||
)
|
axum::http::header::CONTENT_TYPE,
|
||||||
.body(axum::body::Body::from(payload.to_string()))
|
"application/json",
|
||||||
.unwrap()
|
)
|
||||||
).await {
|
.body(axum::body::Body::from(payload.to_string()))
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let resp = get_resp_data::<crate::callers::song::response::create_metadata::Response>(response).await;
|
let resp = get_resp_data::<crate::callers::song::response::create_metadata::Response>(response).await;
|
||||||
assert_eq!(false, resp.data.is_empty(), "No songs found, Response {:?}", resp);
|
assert_eq!(
|
||||||
|
false,
|
||||||
|
resp.data.is_empty(),
|
||||||
|
"No songs found, Response {:?}",
|
||||||
|
resp
|
||||||
|
);
|
||||||
let song = &resp.data[0];
|
let song = &resp.data[0];
|
||||||
let song_id = song.id;
|
let song_id = song.id;
|
||||||
assert_eq!(false, song_id.is_nil(), "Song id should not be nil {:?}", song);
|
assert_eq!(
|
||||||
|
false,
|
||||||
|
song_id.is_nil(),
|
||||||
|
"Song id should not be nil {:?}",
|
||||||
|
song
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
assert!(false, "Error: {:?}", err);
|
assert!(false, "Error: {:?}", err);
|
||||||
|
|||||||
Reference in New Issue
Block a user