wip 2
This commit is contained in:
@ -71,5 +71,10 @@ pub mod trees {
|
||||
})
|
||||
.collect::<Result<Result<_, _>, _>>()??)
|
||||
}
|
||||
|
||||
pub fn put(tree: &sled::Tree, key: &K, val: &V) -> Result<(), StatusJson> {
|
||||
tree.insert(serialize(key)?, serialize(val)?)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,6 +78,8 @@ async fn main() -> io::Result<()> {
|
||||
routes::pages::bump_session,
|
||||
routes::pages::stats::single_stats,
|
||||
routes::pages::stats::all_stats,
|
||||
routes::pages::dailies::dailies,
|
||||
routes::pages::dailies::new_daily,
|
||||
routes::pages::weeks::weeks,
|
||||
],
|
||||
)
|
||||
|
||||
@ -1,17 +1,58 @@
|
||||
use rocket::{get, response::content::RawHtml, State};
|
||||
use rocket::{
|
||||
form::Form,
|
||||
get, post,
|
||||
response::{content::RawHtml, Redirect},
|
||||
uri, FromForm, State,
|
||||
};
|
||||
use rocket_dyn_templates::Template;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
auth::Authorized, database::latest::trees::daily::V as Daily, status_json::StatusJson,
|
||||
auth::Authorized,
|
||||
database::latest::trees::daily::{self, V as Daily},
|
||||
status_json::StatusJson,
|
||||
};
|
||||
|
||||
#[get("/")]
|
||||
#[derive(FromForm)]
|
||||
pub struct NewDaily {
|
||||
name: String,
|
||||
unit: String,
|
||||
unit_count: u32,
|
||||
}
|
||||
|
||||
#[post("/dailies/new", data = "<daily>")]
|
||||
pub fn new_daily(
|
||||
_auth: Authorized,
|
||||
db: &State<sled::Db>,
|
||||
daily: Form<NewDaily>,
|
||||
) -> Result<Redirect, StatusJson> {
|
||||
let daily = daily.into_inner();
|
||||
let daily = Daily {
|
||||
name: daily.name,
|
||||
unit: serde_json::from_str(&format!("\"{}\"", daily.unit)).unwrap(), // TODO
|
||||
unit_count: daily.unit_count,
|
||||
deleted: false,
|
||||
};
|
||||
|
||||
let dailies_tree = db.open_tree(daily::NAME)?;
|
||||
daily::put(&dailies_tree, &Uuid::new_v4(), &daily)?;
|
||||
|
||||
Ok(Redirect::to(uri!(dailies)))
|
||||
}
|
||||
|
||||
#[get("/dailies")]
|
||||
pub fn dailies(_auth: Authorized, db: &State<sled::Db>) -> Result<RawHtml<Template>, StatusJson> {
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Default, Debug, Serialize, Deserialize)]
|
||||
struct TemplateContext {
|
||||
dailies: Vec<Daily>,
|
||||
}
|
||||
|
||||
todo!()
|
||||
let dailies_tree = db.open_tree(daily::NAME)?;
|
||||
let mut ctx = TemplateContext::default();
|
||||
|
||||
ctx.dailies = daily::get_all(&dailies_tree)?.into_values().collect();
|
||||
ctx.dailies.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
|
||||
Ok(RawHtml(Template::render("dailies", &ctx)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user