diff --git a/Cargo.lock b/Cargo.lock index 260afd4..9d1de58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "addr2line" version = "0.13.0" @@ -1879,7 +1881,7 @@ checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" [[package]] name = "stl" -version = "2.3.0" +version = "2.4.0" dependencies = [ "bincode", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 288b2bc..c0c5b47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "stl" description = "studielogg aka scuffed toggl" -version = "2.3.0" +version = "2.4.0" authors = ["Joakim Hulthe "] license = "MPL-2.0" edition = "2018" diff --git a/src/database/category.rs b/src/database/category.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/database/mod.rs b/src/database/mod.rs index 93559e2..cc9b4da 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1,4 +1,5 @@ pub mod unversioned; +pub mod util; pub mod v1; pub mod v2; diff --git a/src/database/util/categories.rs b/src/database/util/categories.rs new file mode 100644 index 0000000..5540f39 --- /dev/null +++ b/src/database/util/categories.rs @@ -0,0 +1,10 @@ +use std::collections::HashMap; +use crate::database::v2; + +pub fn get_category() -> () { + todo!() +} + +pub fn get_all_categories() -> Result Result, StatusJson> { + Ok(match tree.get(serialize(key)?)? { + Some(raw) => Some(deserialize(&raw)?), + None => None, + }) +} + +pub fn get_all_categories( + tree: &sled::Tree, +) -> Result, StatusJson> { + Ok(tree + .iter() + .map(|result| { + result.map(|(k, v)| deserialize(&k).and_then(|k| deserialize(&v).map(|v| (k, v)))) + }) + .collect::, _>>()??) +} diff --git a/src/database/util/mod.rs b/src/database/util/mod.rs new file mode 100644 index 0000000..7995330 --- /dev/null +++ b/src/database/util/mod.rs @@ -0,0 +1 @@ +pub mod category; diff --git a/src/main.rs b/src/main.rs index 35527c4..730ca0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,13 @@ async fn main() -> io::Result<()> { routes::pages::session_edit, routes::pages::stats::single_stats, routes::pages::stats::all_stats, + ], + ) + .mount( + "/api", + rocket::routes![ routes::api::edit_session, + routes::api::get_sessions, routes::api::create_category, routes::api::start_session, routes::api::end_session, diff --git a/src/routes/api.rs b/src/routes/api.rs index d787b85..5b3b56c 100644 --- a/src/routes/api.rs +++ b/src/routes/api.rs @@ -1,5 +1,6 @@ use crate::auth::Authorized; use crate::database::latest::trees::{categories, sessions}; +use crate::database::util::category::get_all_categories; use crate::routes::pages; use crate::status_json::StatusJson; use bincode::{deserialize, serialize}; @@ -7,9 +8,20 @@ use chrono::{Duration, Local, NaiveDateTime, TimeZone}; use rocket::http::Status; use rocket::request::{Form, FromForm}; use rocket::response::Redirect; -use rocket::{post, uri, State}; +use rocket::{get, post, uri, State}; +use rocket_contrib::json::Json; use rocket_contrib::uuid::Uuid; use sled::Transactional; +use std::collections::HashMap; + +#[get("/sessions")] +pub fn get_sessions( + _auth: Authorized, + db: State<'_, sled::Db>, +) -> Result>, StatusJson> { + let categories_tree = db.open_tree(categories::NAME)?; + Ok(Json(get_all_categories(&categories_tree)?)) +} #[derive(FromForm)] pub struct NewCategory { diff --git a/templates/edit_session.hbs b/templates/edit_session.hbs index 5e1403c..a2940f4 100644 --- a/templates/edit_session.hbs +++ b/templates/edit_session.hbs @@ -8,7 +8,7 @@ tillbaka

-
+ Started: @@ -20,7 +20,7 @@

-
+
diff --git a/templates/index.hbs b/templates/index.hbs index 47482a3..35c8e18 100644 --- a/templates/index.hbs +++ b/templates/index.hbs @@ -12,10 +12,10 @@ // Get the corresponding route to activate/inactivate the category let url; if(active) { - url = "/category/" + id + "/end_session"; + url = "/api/category/" + id + "/end_session"; cl.remove(toggled_class); } else { - url = "/category/" + id + "/start_session"; + url = "/api/category/" + id + "/start_session"; cl.add(toggled_class); } @@ -40,7 +40,7 @@ > {{this.1.name}} {{#if this.1.started}} -
+
{{/if}} diff --git a/templates/login.hbs b/templates/login.hbs index 7465618..5cf8e37 100644 --- a/templates/login.hbs +++ b/templates/login.hbs @@ -4,7 +4,7 @@

stl

Logga in

-
+