Skip to content

Commit

Permalink
getsong endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Nils Fahle committed Jun 5, 2023
1 parent d7fd314 commit 7b7bc6c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Binary file added .DS_Store
Binary file not shown.
20 changes: 16 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rocket::{
};
use sqlx::{mysql::MySqlPoolOptions, FromRow, MySql, Pool};

#[derive(Deserialize)]
#[derive(Deserialize, Serialize, FromRow)]
#[serde(crate = "rocket::serde")]
struct Song {
uuid: String,
Expand Down Expand Up @@ -39,8 +39,6 @@ struct QueuePostPayload {
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
struct QueueUpdatePayload {
uuid: String,
key: String,
queueid: i32
}

Expand Down Expand Up @@ -85,6 +83,15 @@ impl Song {

Ok(())
}

pub async fn get_song(id: String, pool: &Pool<MySql>) -> Result<Self, sqlx::Error> {
let song = sqlx::query_as::<MySql, Self>("SELECT * FROM song_data WHERE UUID = ?")
.bind(id)
.fetch_one(pool)
.await?;

Ok(song)
}
}

impl QueueSong {
Expand Down Expand Up @@ -209,6 +216,11 @@ async fn verify_access_key(uuid: &str, api_key: &str, pool: &State<Pool<MySql>>)
Ok(())
}

#[get("/getsong.php?<uuid>")]
async fn get_song(pool: &State<Pool<MySql>>, uuid: &str) -> Result<Json<Song>, Status> {
Song::get_song(uuid.to_string(), pool).await.map_or(Err(Status::InternalServerError), |song| Ok(Json(song)))
}

#[get("/queue.php?<uuid>")]
async fn get_queue(pool: &State<Pool<MySql>>, uuid: &str) -> Result<Json<Vec<QueueSong>>, Status> {
QueueSong::get_queue(uuid.to_string(), pool).await.map_or(Err(Status::InternalServerError), |queue| Ok(Json(queue)))
Expand Down Expand Up @@ -290,7 +302,7 @@ async fn main() -> Result<(), rocket::Error> {


rocket::build()
.mount("/v2", routes![get_queue, add_to_queue, set_queue_song_played, clear_queue, set_telemetry, set_song])
.mount("/v2", routes![get_queue, add_to_queue, set_queue_song_played, clear_queue, set_telemetry, get_song, set_song])
.manage(pool)
.launch()
.await?;
Expand Down

0 comments on commit 7b7bc6c

Please sign in to comment.