Skip to content

Commit

Permalink
backend/bookmarks: add api/handler for get_playqueue
Browse files Browse the repository at this point in the history
  • Loading branch information
vnghia committed Nov 28, 2024
1 parent 2ce7e71 commit f2b50dd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
23 changes: 23 additions & 0 deletions nghe-api/src/bookmarks/get_playqueue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use nghe_proc_macro::api_derive;
use uuid::Uuid;

use crate::id3;

#[api_derive]
#[endpoint(path = "getPlayQueue")]
pub struct Request {}

#[api_derive]
#[derive(Default)]
pub struct Playqueue {
pub entry: Vec<id3::song::Short>,
pub current: Option<Uuid>,
pub position: Option<u64>,
}

#[api_derive]
#[derive(Default)]
pub struct Response {
#[serde(rename = "playQueue")]
pub playqueue: Playqueue,
}
1 change: 1 addition & 0 deletions nghe-api/src/bookmarks/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod get_playqueue;
pub mod save_playqueue;
49 changes: 49 additions & 0 deletions nghe-backend/src/route/bookmarks/get_playqueue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, SelectableHelper};
use diesel_async::RunQueryDsl;
use futures_lite::{stream, StreamExt as _};
use nghe_api::bookmarks::get_playqueue::Playqueue;
pub use nghe_api::bookmarks::get_playqueue::{Request, Response};
use nghe_proc_macro::handler;
use uuid::Uuid;

use crate::database::Database;
use crate::orm::{id3, playqueues, songs};
use crate::Error;

#[handler]
pub async fn handler(
database: &Database,
user_id: Uuid,
request: Request,
) -> Result<Response, Error> {
Ok(
if let Some(data) = playqueues::table
.filter(playqueues::user_id.eq(user_id))
.select(playqueues::Data::as_select())
.get_result(&mut database.get().await?)
.await
.optional()?
{
let entry = stream::iter(data.ids)
.then(async |id| {
id3::song::short::query::with_user_id(user_id)
.filter(songs::id.eq(id))
.get_result(&mut database.get().await?)
.await?
.try_into()
})
.try_collect()
.await?;

Response {
playqueue: Playqueue {
entry,
current: data.current,
position: data.position.map(i64::try_into).transpose()?,
},
}
} else {
Response::default()
},
)
}
3 changes: 2 additions & 1 deletion nghe-backend/src/route/bookmarks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod get_playqueue;
mod save_playqueue;

nghe_proc_macro::build_router! {
modules = [save_playqueue],
modules = [get_playqueue, save_playqueue],
}

0 comments on commit f2b50dd

Please sign in to comment.