Updating song from queue #132
+20
-21
@@ -118,25 +118,26 @@ mod song_queue {
|
||||
pool: &sqlx::PgPool,
|
||||
data: &Vec<u8>,
|
||||
id: &uuid::Uuid,
|
||||
) -> Result<Vec<u8>, sqlx::Error> {
|
||||
) -> Result<Vec<u8>, sqlx::Error> {
|
||||
let result = sqlx::query(
|
||||
r#"
|
||||
UPDATE "songQueue" SET data = $1 WHERE id = $2 RETURNING data;
|
||||
"#
|
||||
)
|
||||
.bind(data)
|
||||
.bind(id)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
eprintln!("Error inserting: {:?}", e);
|
||||
});
|
||||
"#,
|
||||
)
|
||||
.bind(data)
|
||||
.bind(id)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
eprintln!("Error inserting: {:?}", e);
|
||||
});
|
||||
|
||||
match result {
|
||||
Ok(row) => {
|
||||
Ok(row.try_get("data").map_err(|_e| sqlx::Error::RowNotFound).unwrap())
|
||||
}
|
||||
Err(_) => Err(sqlx::Error::RowNotFound)
|
||||
Ok(row) => Ok(row
|
||||
.try_get("data")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap()),
|
||||
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -419,8 +420,10 @@ pub mod endpoint {
|
||||
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
mut multipart: axum::extract::Multipart,
|
||||
) -> (axum::http::StatusCode, axum::Json<super::response::update_song_queue::Response>) {
|
||||
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::update_song_queue::Response>,
|
||||
) {
|
||||
let mut response = super::response::update_song_queue::Response::default();
|
||||
|
||||
if let Some(field) = multipart.next_field().await.unwrap() {
|
||||
@@ -442,11 +445,7 @@ pub mod endpoint {
|
||||
// file.write_all(&data).unwrap();
|
||||
|
||||
let raw_data: Vec<u8> = data.to_vec();
|
||||
match song_queue::update(
|
||||
&pool,
|
||||
&raw_data,
|
||||
&id,
|
||||
).await {
|
||||
match song_queue::update(&pool, &raw_data, &id).await {
|
||||
Ok(queued_data) => {
|
||||
response.data.push(queued_data);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
|
||||
+30
-13
@@ -83,7 +83,7 @@ pub mod init {
|
||||
)
|
||||
.route(
|
||||
crate::callers::endpoints::QUEUESONGUPDATE,
|
||||
patch(crate::callers::song::endpoint::update_song_queue)
|
||||
patch(crate::callers::song::endpoint::update_song_queue),
|
||||
)
|
||||
.route(
|
||||
crate::callers::endpoints::QUEUEMETADATA,
|
||||
@@ -1124,7 +1124,8 @@ mod tests {
|
||||
|
||||
// let new_file = String::from("tests/Machine_gun/new_file.flac");
|
||||
|
||||
let temp_file = tempfile::tempdir().expect("Could not create test directory");
|
||||
let temp_file = tempfile::tempdir()
|
||||
.expect("Could not create test directory");
|
||||
let test_dir = String::from(temp_file.path().to_str().unwrap());
|
||||
let new_file = format!("{}/new_file.flac", test_dir);
|
||||
|
||||
@@ -1138,28 +1139,44 @@ mod tests {
|
||||
let content_type = form.content_type();
|
||||
let body = MultipartBody::from(form);
|
||||
|
||||
let raw_uri = String::from(crate::callers::endpoints::QUEUESONGUPDATE);
|
||||
let raw_uri =
|
||||
String::from(crate::callers::endpoints::QUEUESONGUPDATE);
|
||||
let end_index = raw_uri.len() - 5;
|
||||
// let mut uri: String = (&raw_uri[..end_index]).to_string();
|
||||
// uri += &id.to_string();
|
||||
|
||||
let uri = format!("{}/{}", (&raw_uri[..end_index]).to_string(), id.to_string());
|
||||
let uri = format!(
|
||||
"{}/{}",
|
||||
(&raw_uri[..end_index]).to_string(),
|
||||
id.to_string()
|
||||
);
|
||||
|
||||
// app.clone().oneshot(req).await
|
||||
match app.clone().oneshot(
|
||||
axum::http::Request::builder()
|
||||
.method(axum::http::Method::PATCH)
|
||||
.uri(uri)
|
||||
.header(axum::http::header::CONTENT_TYPE, content_type)
|
||||
.body(axum::body::Body::from_stream(body))
|
||||
.unwrap()
|
||||
).await {
|
||||
match app
|
||||
.clone()
|
||||
.oneshot(
|
||||
axum::http::Request::builder()
|
||||
.method(axum::http::Method::PATCH)
|
||||
.uri(uri)
|
||||
.header(
|
||||
axum::http::header::CONTENT_TYPE,
|
||||
content_type,
|
||||
)
|
||||
.body(axum::body::Body::from_stream(body))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
let resp = get_resp_data::<
|
||||
crate::callers::song::response::update_song_queue::Response,
|
||||
>(response)
|
||||
.await;
|
||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||
assert_eq!(
|
||||
false,
|
||||
resp.data.is_empty(),
|
||||
"Should not be empty"
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
|
||||
Reference in New Issue
Block a user