synthwave/src/route/mod.rs

64 lines
1.3 KiB
Rust
Raw Normal View History

2024-10-04 12:38:35 +00:00
use std::str::FromStr;
2024-08-01 14:42:58 +00:00
use rocket::{
get,
response::{status::BadRequest, Redirect},
uri,
};
2024-07-24 09:07:24 +00:00
use serde_json::json;
2024-08-12 23:52:16 +00:00
pub mod admin;
2024-07-24 09:07:24 +00:00
pub mod album;
2024-07-26 12:14:08 +00:00
pub mod artist;
2024-08-16 23:25:41 +00:00
pub mod event;
2024-08-11 19:56:55 +00:00
pub mod playlist;
2024-08-17 08:45:10 +00:00
pub mod search;
2024-07-24 09:07:24 +00:00
pub mod track;
pub mod user;
2024-08-01 14:36:54 +00:00
// todo : rework api
2024-10-04 12:38:35 +00:00
/// A trait to generate a Model API representation in JSON format.
pub trait ToAPI: Sized {
/// Generate public API JSON
fn api(&self) -> impl std::future::Future<Output = serde_json::Value>;
}
/// Converts a slice of items implementing the `ToAPI` trait into a `Vec` of JSON values.
pub async fn vec_to_api(items: &[impl ToAPI]) -> Vec<serde_json::Value> {
let mut ret = Vec::with_capacity(items.len());
for e in items {
ret.push(e.api().await);
}
ret
}
pub fn to_uuid(id: &str) -> Result<uuid::Uuid, ApiError> {
uuid::Uuid::from_str(id).map_err(|_| no_uuid_error())
}
2024-07-24 09:07:24 +00:00
type ApiError = BadRequest<serde_json::Value>;
type FallibleApiResponse = Result<serde_json::Value, ApiError>;
2024-10-04 12:38:35 +00:00
pub fn no_uuid_error() -> ApiError {
api_error("No valid UUID")
}
2024-07-24 09:07:24 +00:00
pub fn api_error(msg: &str) -> ApiError {
BadRequest(json!({
"error": msg
}))
}
2024-08-01 14:36:54 +00:00
2024-08-01 14:42:58 +00:00
#[get("/")]
2024-08-12 16:48:33 +00:00
pub fn index_redir() -> Redirect {
2024-08-01 14:42:58 +00:00
Redirect::to(uri!("/web"))
}
2024-08-07 19:56:09 +00:00
#[get("/manifest.json")]
2024-08-12 16:48:33 +00:00
pub fn manifest_redir() -> Redirect {
2024-08-07 19:56:09 +00:00
Redirect::to(uri!("/web/manifest.json"))
}