Add delete buttons to history page
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
use crate::database::v1::trees::{categories, past_sessions};
|
||||
use crate::database::latest::trees::{categories, past_sessions};
|
||||
use crate::routes::pages;
|
||||
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 rocket::response::Redirect;
|
||||
use rocket::{post, uri, State};
|
||||
use sled::Transactional;
|
||||
use uuid::Uuid;
|
||||
|
||||
@ -34,23 +36,23 @@ pub fn create_category(
|
||||
Ok(Status::Ok.into())
|
||||
}
|
||||
|
||||
#[post("/set_category/<category_uuid>/active")]
|
||||
pub fn activate_category(
|
||||
#[post("/category/<category_uuid>/start_session")]
|
||||
pub fn start_session(
|
||||
category_uuid: String,
|
||||
db: State<'_, sled::Db>,
|
||||
) -> Result<StatusJson, StatusJson> {
|
||||
toggle_category(category_uuid, true, db)
|
||||
toggle_category_session(category_uuid, true, db)
|
||||
}
|
||||
|
||||
#[post("/set_category/<category_uuid>/inactive")]
|
||||
pub fn deactivate_category(
|
||||
#[post("/category/<category_uuid>/end_session")]
|
||||
pub fn end_session(
|
||||
category_uuid: String,
|
||||
db: State<'_, sled::Db>,
|
||||
) -> Result<StatusJson, StatusJson> {
|
||||
toggle_category(category_uuid, false, db)
|
||||
toggle_category_session(category_uuid, false, db)
|
||||
}
|
||||
|
||||
pub fn toggle_category(
|
||||
pub fn toggle_category_session(
|
||||
category_uuid: String,
|
||||
set_active: bool,
|
||||
db: State<'_, sled::Db>,
|
||||
@ -101,3 +103,30 @@ pub fn toggle_category(
|
||||
},
|
||||
)??)
|
||||
}
|
||||
|
||||
#[post("/session/<session_uuid>/delete")]
|
||||
pub fn delete_session(
|
||||
session_uuid: String,
|
||||
db: State<'_, sled::Db>,
|
||||
) -> Result<Redirect, StatusJson> {
|
||||
let session_uuid = Uuid::parse_str(&session_uuid)?;
|
||||
let session_uuid_s = sled::IVec::from(serialize(&session_uuid)?);
|
||||
|
||||
let past_sessions_tree = db.open_tree(past_sessions::NAME)?;
|
||||
|
||||
match past_sessions_tree.remove(session_uuid_s)? {
|
||||
Some(_) => Ok(Redirect::to(uri!(pages::history))),
|
||||
None => Err(Status::NotFound.into()),
|
||||
}
|
||||
|
||||
// TODO: mark as deleted instead of removing
|
||||
// Ok(past_sessions_tree.transaction(|tx_past_sessions| {
|
||||
// match tx_past_sessions.get(&session_uuid_s)? {
|
||||
// None => return Ok(Err(Status::NotFound)),
|
||||
// Some(data) => {
|
||||
// let mut past_session: past_sessions::V = deserialize(&data).unwrap();
|
||||
// }
|
||||
// }
|
||||
// Ok(Ok(Redirect::to(uri!(pages::history))))
|
||||
// })??)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user