Fetch next queued song #125
@@ -3,6 +3,7 @@ pub mod song;
|
|||||||
|
|
||||||
pub mod endpoints {
|
pub mod endpoints {
|
||||||
pub const QUEUESONG: &str = "/api/v2/song/queue";
|
pub const QUEUESONG: &str = "/api/v2/song/queue";
|
||||||
|
pub const QUEUESONGDATA: &str = "/api/v2/song/queue/{id}";
|
||||||
pub const NEXTQUEUESONG: &str = "/api/v2/song/queue/next";
|
pub const NEXTQUEUESONG: &str = "/api/v2/song/queue/next";
|
||||||
pub const QUEUEMETADATA: &str = "/api/v2/song/metadata/queue";
|
pub const QUEUEMETADATA: &str = "/api/v2/song/metadata/queue";
|
||||||
}
|
}
|
||||||
|
|||||||
+55
-1
@@ -122,10 +122,36 @@ mod song_queue {
|
|||||||
Err(_err) => Err(sqlx::Error::RowNotFound),
|
Err(_err) => Err(sqlx::Error::RowNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_data(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result<Vec<u8>, sqlx::Error> {
|
||||||
|
let result = sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT data FROM "songQueue"
|
||||||
|
WHERE id = $1;
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("Error inserting: {}", e);
|
||||||
|
});
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(row) => {
|
||||||
|
let data = row
|
||||||
|
.try_get("data")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap();
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
Err(_err) => Err(sqlx::Error::RowNotFound),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod endpoint {
|
pub mod endpoint {
|
||||||
use axum::{Json, http::StatusCode};
|
use axum::{Json, http::StatusCode, response::IntoResponse};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use crate::callers::song::song_queue;
|
use crate::callers::song::song_queue;
|
||||||
@@ -197,4 +223,32 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn download_flac(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||||
|
) -> (StatusCode, axum::response::Response) {
|
||||||
|
println!("Id: {:?}", id);
|
||||||
|
|
||||||
|
match song_queue::get_data(&pool, &id).await {
|
||||||
|
Ok(data) => {
|
||||||
|
let by = axum::body::Bytes::from(data);
|
||||||
|
let mut response = by.into_response();
|
||||||
|
let headers = response.headers_mut();
|
||||||
|
headers.insert(
|
||||||
|
axum::http::header::CONTENT_TYPE,
|
||||||
|
"audio/flac".parse().unwrap(),
|
||||||
|
);
|
||||||
|
headers.insert(
|
||||||
|
axum::http::header::CONTENT_DISPOSITION,
|
||||||
|
format!("attachment; filename=\"{}.flac\"", id)
|
||||||
|
.parse()
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
(StatusCode::OK, response)
|
||||||
|
}
|
||||||
|
Err(_err) => (StatusCode::BAD_REQUEST, axum::response::Response::default()),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+109
-11
@@ -69,6 +69,10 @@ pub mod init {
|
|||||||
crate::callers::endpoints::QUEUESONG,
|
crate::callers::endpoints::QUEUESONG,
|
||||||
post(crate::callers::song::endpoint::queue_song),
|
post(crate::callers::song::endpoint::queue_song),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
crate::callers::endpoints::QUEUESONGDATA,
|
||||||
|
get(crate::callers::song::endpoint::download_flac),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
crate::callers::endpoints::NEXTQUEUESONG,
|
crate::callers::endpoints::NEXTQUEUESONG,
|
||||||
get(crate::callers::song::endpoint::fetch_queue_song),
|
get(crate::callers::song::endpoint::fetch_queue_song),
|
||||||
@@ -227,13 +231,47 @@ mod tests {
|
|||||||
app.clone().oneshot(req).await
|
app.clone().oneshot(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn fetch_queue_req(
|
||||||
|
app: &axum::Router,
|
||||||
|
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||||
|
let fetch_req = axum::http::Request::builder()
|
||||||
|
.method(axum::http::Method::GET)
|
||||||
|
.uri(crate::callers::endpoints::NEXTQUEUESONG)
|
||||||
|
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||||
|
.body(axum::body::Body::empty())
|
||||||
|
.unwrap();
|
||||||
|
app.clone().oneshot(fetch_req).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fetch_queue_data_req(
|
||||||
|
app: &axum::Router,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||||
|
let raw_uri = String::from(crate::callers::endpoints::QUEUESONGDATA);
|
||||||
|
let end_index = raw_uri.len() - 4;
|
||||||
|
let mut uri: String = (&raw_uri[..end_index]).to_string();
|
||||||
|
uri += &id.to_string();
|
||||||
|
let req = axum::http::Request::builder()
|
||||||
|
.method(axum::http::Method::GET)
|
||||||
|
.uri(uri)
|
||||||
|
.header(axum::http::header::CONTENT_TYPE, "audio/flac")
|
||||||
|
.body(axum::body::Body::empty())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
app.clone().oneshot(req).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn resp_to_bytes(
|
||||||
|
response: axum::response::Response,
|
||||||
|
) -> Result<axum::body::Bytes, axum::Error> {
|
||||||
|
axum::body::to_bytes(response.into_body(), usize::MAX).await
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_resp_data<Data>(response: axum::response::Response) -> Data
|
pub async fn get_resp_data<Data>(response: axum::response::Response) -> Data
|
||||||
where
|
where
|
||||||
Data: for<'a> serde::Deserialize<'a>,
|
Data: for<'a> serde::Deserialize<'a>,
|
||||||
{
|
{
|
||||||
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
|
let body = resp_to_bytes(response).await.unwrap();
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
serde_json::from_slice(&body).unwrap()
|
serde_json::from_slice(&body).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,14 +337,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");
|
||||||
|
|
||||||
let fetch_req = axum::http::Request::builder()
|
match fetch_queue_req(&app).await {
|
||||||
.method(axum::http::Method::GET)
|
|
||||||
.uri(crate::callers::endpoints::NEXTQUEUESONG)
|
|
||||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
|
||||||
.body(axum::body::Body::empty())
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
match app.clone().oneshot(fetch_req).await {
|
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let resp = get_resp_data::<
|
let resp = get_resp_data::<
|
||||||
crate::callers::song::response::fetch_queue_song::Response,
|
crate::callers::song::response::fetch_queue_song::Response,
|
||||||
@@ -327,6 +358,73 @@ mod tests {
|
|||||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_song_fetch_queue_data() {
|
||||||
|
let tm_pool = db_mgr::get_pool().await.unwrap();
|
||||||
|
let db_name = db_mgr::generate_db_name().await;
|
||||||
|
|
||||||
|
match db_mgr::create_database(&tm_pool, &db_name).await {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Success");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||||
|
db::migrations(&pool).await;
|
||||||
|
|
||||||
|
let app = init::app(pool).await;
|
||||||
|
|
||||||
|
// Send request
|
||||||
|
match song_queue_req(&app).await {
|
||||||
|
Ok(response) => {
|
||||||
|
let resp =
|
||||||
|
get_resp_data::<crate::callers::song::response::Response>(response).await;
|
||||||
|
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||||
|
assert_eq!(false, resp.data[0].is_nil(), "Should not be empty");
|
||||||
|
|
||||||
|
match fetch_queue_req(&app).await {
|
||||||
|
Ok(response) => {
|
||||||
|
let resp = get_resp_data::<
|
||||||
|
crate::callers::song::response::fetch_queue_song::Response,
|
||||||
|
>(response)
|
||||||
|
.await;
|
||||||
|
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||||
|
let id = resp.data[0].id;
|
||||||
|
|
||||||
|
match fetch_queue_data_req(&app, &id).await {
|
||||||
|
Ok(response) => match resp_to_bytes(response).await {
|
||||||
|
Ok(bytes) => {
|
||||||
|
assert_eq!(
|
||||||
|
false,
|
||||||
|
bytes.is_empty(),
|
||||||
|
"Queued data should not be empty"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_song_metadata_queue() {
|
async fn test_song_metadata_queue() {
|
||||||
let tm_pool = db_mgr::get_pool().await.unwrap();
|
let tm_pool = db_mgr::get_pool().await.unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user