diff options
Diffstat (limited to 'server/src/routes/api/mod.rs')
-rw-r--r-- | server/src/routes/api/mod.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/server/src/routes/api/mod.rs b/server/src/routes/api/mod.rs new file mode 100644 index 0000000..5f48873 --- /dev/null +++ b/server/src/routes/api/mod.rs @@ -0,0 +1,51 @@ +pub mod error; + +use std::path::PathBuf; + +use super::ui::account::{login_logic, LoginForm}; +use crate::{ + database::Database, + library::{Library, Node}, + routes::{api::error::ApiResult, ui::account::session::Session}, +}; +use anyhow::Context; +use rocket::{get, http::CookieJar, post, serde::json::Json, State}; +use serde_json::{json, Value}; + +#[get("/api/version")] +pub fn r_api_version() -> &'static str { + "1" +} + +#[post("/api/account/login", data = "<data>")] +pub fn r_api_account_login( + database: &State<Database>, + jar: &CookieJar, + data: Json<LoginForm>, +) -> ApiResult<Value> { + login_logic(jar, database, &data.username, &data.password)?; + Ok(json!({ "ok": true })) +} + +#[get("/api/library/<path..>")] +pub fn r_api_library_node( + _sess: Session, + path: PathBuf, + library: &State<Library>, +) -> ApiResult<Value> { + let node = library + .nested_path(&path) + .context("retrieving library node")?; + + match node.as_ref() { + Node::Directory(d) => Ok(json!({ + "identifier": d.identifier, + "info": d.info, + "children": d.children.iter().map(|c| c.identifier().to_string()).collect::<Vec<_>>() + })), + Node::Item(i) => Ok(json!({ + "identifier": i.identifier, + "info": i.info, + })), + } +} |