-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend/bookmarks: add api/handler for get_playqueue
- Loading branch information
Showing
4 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod get_playqueue; | ||
pub mod save_playqueue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
} |