Some refactoring
This commit is contained in:
103
src/routes/api.rs
Normal file
103
src/routes/api.rs
Normal file
@ -0,0 +1,103 @@
|
||||
use crate::database::v1::trees::{categories, past_sessions};
|
||||
use crate::status_json::StatusJson;
|
||||
use bincode::{deserialize, serialize};
|
||||
use chrono::{Duration, Utc};
|
||||
use rocket::http::Status;
|
||||
use rocket::request::{Form, FromForm};
|
||||
use rocket::{post, State};
|
||||
use sled::Transactional;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct NewCategory {
|
||||
name: String,
|
||||
color: String,
|
||||
}
|
||||
|
||||
#[post("/create_category", data = "<category>")]
|
||||
pub fn create_category(
|
||||
category: Form<NewCategory>,
|
||||
db: State<'_, sled::Db>,
|
||||
) -> Result<StatusJson, StatusJson> {
|
||||
let category = category.into_inner();
|
||||
|
||||
let categories_tree = db.open_tree(categories::NAME)?;
|
||||
categories_tree.insert(
|
||||
serialize(&Uuid::new_v4())?,
|
||||
serialize(&categories::V {
|
||||
name: category.name,
|
||||
color: category.color,
|
||||
started: None,
|
||||
})?,
|
||||
)?;
|
||||
|
||||
Ok(Status::Ok.into())
|
||||
}
|
||||
|
||||
#[post("/set_category/<category_uuid>/active")]
|
||||
pub fn activate_category(
|
||||
category_uuid: String,
|
||||
db: State<'_, sled::Db>,
|
||||
) -> Result<StatusJson, StatusJson> {
|
||||
toggle_category(category_uuid, true, db)
|
||||
}
|
||||
|
||||
#[post("/set_category/<category_uuid>/inactive")]
|
||||
pub fn deactivate_category(
|
||||
category_uuid: String,
|
||||
db: State<'_, sled::Db>,
|
||||
) -> Result<StatusJson, StatusJson> {
|
||||
toggle_category(category_uuid, false, db)
|
||||
}
|
||||
|
||||
pub fn toggle_category(
|
||||
category_uuid: String,
|
||||
set_active: bool,
|
||||
db: State<'_, sled::Db>,
|
||||
) -> Result<StatusJson, StatusJson> {
|
||||
let category_uuid = Uuid::parse_str(&category_uuid)?;
|
||||
let category_uuid_s = sled::IVec::from(serialize(&category_uuid)?);
|
||||
|
||||
let categories_tree = db.open_tree(categories::NAME)?;
|
||||
let past_sessions_tree = db.open_tree(past_sessions::NAME)?;
|
||||
|
||||
Ok((&categories_tree, &past_sessions_tree).transaction(
|
||||
|(tx_categories, tx_past_sessions)| {
|
||||
match tx_categories.get(&category_uuid_s)? {
|
||||
None => return Ok(Err(Status::NotFound)),
|
||||
Some(data) => {
|
||||
let mut category: categories::V = deserialize(&data).unwrap();
|
||||
let now = Utc::now().naive_utc();
|
||||
|
||||
match (set_active, category.started.take()) {
|
||||
(false, Some(started)) => {
|
||||
// only save sessions longer than 5 minutes
|
||||
let duration = now - started;
|
||||
if duration > Duration::minutes(5) {
|
||||
let session_uuid = serialize(&Uuid::new_v4()).unwrap();
|
||||
let past_session = past_sessions::V {
|
||||
category: category_uuid,
|
||||
started,
|
||||
ended: now,
|
||||
};
|
||||
tx_past_sessions
|
||||
.insert(session_uuid, serialize(&past_session).unwrap())?;
|
||||
}
|
||||
}
|
||||
(true, None) => {
|
||||
category.started = Some(now);
|
||||
}
|
||||
_ => {
|
||||
// Category is already in the correct state
|
||||
return Ok(Ok(Status::Ok.into()));
|
||||
}
|
||||
}
|
||||
|
||||
tx_categories.insert(&category_uuid_s, serialize(&category).unwrap())?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Ok(Status::Ok.into()))
|
||||
},
|
||||
)??)
|
||||
}
|
||||
Reference in New Issue
Block a user