This commit is contained in:
2023-02-09 17:08:07 +01:00
parent 9a72413934
commit 432962f5a9
11 changed files with 156 additions and 20 deletions

View File

@ -1,9 +1,10 @@
mod restday;
mod restreg;
mod restuser;
use std::ops::Deref;
use chrono::{Duration, Local, NaiveDate};
use chrono::{Datelike, Duration, Local, NaiveDate};
use rocket::{
form::{self, Form, ValueField},
fs::FileServer,
@ -37,7 +38,17 @@ impl Deref for NaiveDateForm {
#[get("/")]
async fn index(db: &State<DatabaseConnection>, user: user::Model) -> Template {
let mut data = Vec::new();
for i in 0..6 {
let mut show_next_n_days = 6;
if user.is_cox {
let end_of_year = NaiveDate::from_ymd_opt(Local::now().year(), 5, 31).unwrap();
show_next_n_days = end_of_year
.signed_duration_since(Local::now().date_naive())
.num_days()
+ 1;
}
for i in 0..show_next_n_days {
let date = (Local::now() + Duration::days(i)).date_naive();
let day = day::Model::find_or_create_day(date, db.inner()).await;
data.push(DayWithTrips::new(day, db.inner()).await);
@ -62,6 +73,12 @@ fn savename(name: Form<NameForm>, cookies: &CookieJar) -> Redirect {
Redirect::to("/")
}
#[get("/logout")]
fn logout(cookies: &CookieJar) -> Redirect {
cookies.remove(Cookie::new("name", ""));
Redirect::to("/")
}
#[catch(401)] //unauthorized
fn unauthorized_error() -> Redirect {
Redirect::to("/name")
@ -72,8 +89,9 @@ pub async fn start() -> Rocket<Build> {
.attach(Template::fairing())
.manage(Database::connect("sqlite://db.sqlite").await.unwrap())
.mount("/public", FileServer::from("static/"))
.mount("/", routes![index, name, savename])
.mount("/", routes![index, name, savename, logout])
.mount("/day", restday::routes())
.mount("/register", restreg::routes())
.mount("/user", restuser::routes())
.register("/", catchers![unauthorized_error])
}