initial push
This commit is contained in:
90
src/main.rs
Normal file
90
src/main.rs
Normal file
@ -0,0 +1,90 @@
|
||||
#[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();
|
||||
//}
|
19
src/models/day.rs
Normal file
19
src/models/day.rs
Normal file
@ -0,0 +1,19 @@
|
||||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "day")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub day: chrono::NaiveDate,
|
||||
pub planned_amount_cox: Option<i32>,
|
||||
pub planned_starting_time: Option<String>,
|
||||
pub open_registration: bool,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
5
src/models/mod.rs
Normal file
5
src/models/mod.rs
Normal file
@ -0,0 +1,5 @@
|
||||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
pub mod day;
|
3
src/models/prelude.rs
Normal file
3
src/models/prelude.rs
Normal file
@ -0,0 +1,3 @@
|
||||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0
|
||||
|
||||
pub use super::day::Entity as Day;
|
Reference in New Issue
Block a user