rave/src/rest/get_album_list.rs

98 lines
2.7 KiB
Rust

use poem::web::{Data, Query};
use serde::Deserialize;
use crate::{
authentication::Authentication,
random_types::SortType,
subsonic::{Child, Error, SubsonicResponse},
utils::{self, middleware::DbConn},
};
#[poem::handler]
pub async fn get_album_list(
Data(conn): Data<&DbConn>,
auth: Authentication,
Query(params): Query<GetAlbumListParams>,
) -> SubsonicResponse {
let u = utils::verify_user(conn.clone(), auth).await;
match u {
Ok(_) => {}
Err(e) => return e,
}
let _params = match params.verify() {
Ok(p) => p,
Err(e) => return e,
};
let album_list = vec![
Child {
id: "al-11".to_string(),
parent: Some(1),
title: "Example".to_string(),
artist: Some("Example".to_string()),
is_dir: true,
..Default::default()
},
Child {
id: "al-12".to_string(),
parent: Some(1),
title: "Example 2".to_string(),
artist: Some("Example 2".to_string()),
is_dir: true,
..Default::default()
},
];
SubsonicResponse::new_album_list(album_list)
}
#[derive(Debug, Clone, Deserialize)]
pub struct GetAlbumListParams {
#[serde(rename = "type")]
pub r#type: SortType,
#[serde(default = "default_size")]
pub size: i32,
#[serde(default)]
pub offset: i32,
#[serde(default)]
pub from_year: Option<i32>,
#[serde(default)]
pub to_year: Option<i32>,
#[serde(default)]
pub genre: Option<String>,
#[serde(default)]
pub music_folder_id: Option<i32>,
}
impl GetAlbumListParams {
#[allow(clippy::result_large_err)]
pub fn verify(self) -> Result<Self, SubsonicResponse> {
if self.r#type == SortType::ByYear {
if self.from_year.is_none() || self.to_year.is_none() {
return Err(SubsonicResponse::new_error(
Error::RequiredParameterMissing(Some(
"Missing required parameter: fromYear or toYear".to_string(),
)),
));
}
} else if self.r#type == SortType::ByGenre && self.genre.is_none() {
return Err(SubsonicResponse::new_error(
Error::RequiredParameterMissing(Some(
"Missing required parameter: genre".to_string(),
)),
));
} else if self.size > 500 || self.size < 1 {
return Err(SubsonicResponse::new_error(Error::Generic(Some(
"size must be between 1 and 500".to_string(),
))));
}
Ok(self)
}
}
const fn default_size() -> i32 {
10
}