91 lines
2.5 KiB
Rust
91 lines
2.5 KiB
Rust
|
#[macro_use]
|
||
|
extern crate rocket;
|
||
|
|
||
|
mod models;
|
||
|
|
||
|
use std::collections::HashMap;
|
||
|
|
||
|
use chrono::Duration;
|
||
|
use chrono::Local;
|
||
|
use rocket::{form::Form, fs::FileServer, State};
|
||
|
use rocket_dyn_templates::context;
|
||
|
use rocket_dyn_templates::Template;
|
||
|
use sea_orm::ColumnTrait;
|
||
|
use sea_orm::QueryFilter;
|
||
|
use sea_orm::{
|
||
|
ActiveModelTrait, Database, DatabaseConnection, EntityTrait, Order, QueryOrder, Set,
|
||
|
};
|
||
|
|
||
|
use crate::models::day;
|
||
|
|
||
|
#[get("/")]
|
||
|
async fn index(db: &State<DatabaseConnection>) -> Template {
|
||
|
let days: Vec<day::Model> = day::Entity::find()
|
||
|
.filter(day::Column::Day.gte(format!("{}", Local::now().format("%Y-%m-%d")))) // don't show stuff from the past
|
||
|
.order_by(day::Column::Day, Order::Asc)
|
||
|
.all(db.inner())
|
||
|
.await
|
||
|
.unwrap();
|
||
|
|
||
|
let days: HashMap<chrono::NaiveDate, day::Model> =
|
||
|
days.into_iter().map(|x| (x.day, x)).collect();
|
||
|
|
||
|
let mut next_days = Vec::new();
|
||
|
for i in 0..6 {
|
||
|
next_days.push(Local::now() + Duration::days(i));
|
||
|
}
|
||
|
|
||
|
println!("{:?}", next_days);
|
||
|
|
||
|
Template::render("index", context! { days, next_days })
|
||
|
}
|
||
|
|
||
|
#[derive(FromForm, Debug)]
|
||
|
struct DayForm {
|
||
|
day: String,
|
||
|
planned_amount_cox: Option<i32>,
|
||
|
planned_starting_time: Option<String>,
|
||
|
open_registration: bool,
|
||
|
}
|
||
|
|
||
|
#[put("/day", data = "<day>")]
|
||
|
async fn create(db: &State<DatabaseConnection>, day: Form<DayForm>) -> Template {
|
||
|
let day = day::ActiveModel {
|
||
|
day: Set(chrono::NaiveDate::parse_from_str(&day.day, "%Y-%m-%d").unwrap()),
|
||
|
planned_amount_cox: Set(day.planned_amount_cox),
|
||
|
planned_starting_time: Set(day.planned_starting_time.clone()),
|
||
|
open_registration: Set(day.open_registration),
|
||
|
};
|
||
|
|
||
|
day.insert(db.inner()).await.unwrap(); //TODO: fixme
|
||
|
|
||
|
Template::render("index", context! {})
|
||
|
}
|
||
|
|
||
|
#[launch]
|
||
|
async fn rocket() -> _ {
|
||
|
rocket::build()
|
||
|
.attach(Template::fairing())
|
||
|
.manage(Database::connect("sqlite://db.sqlite").await.unwrap())
|
||
|
.mount("/public", FileServer::from("static/"))
|
||
|
.mount("/", routes![index, create])
|
||
|
}
|
||
|
|
||
|
//#[tokio::main]
|
||
|
//async fn main() {
|
||
|
// println!("Hello, world!");
|
||
|
// let db = Database::connect("sqlite://db.sqlite").await.unwrap();
|
||
|
//
|
||
|
// let day = day::ActiveModel {
|
||
|
// day: Set("2023-02-08".into()),
|
||
|
// ..Default::default()
|
||
|
// };
|
||
|
//
|
||
|
// //day.insert(&db).await.unwrap();
|
||
|
//
|
||
|
// let a: Vec<day::Model> = day::Entity::find().all(&db).await.unwrap();
|
||
|
// println!("{:?}", a);
|
||
|
//
|
||
|
// db.close().await.unwrap();
|
||
|
//}
|