This commit is contained in:
philipp 2023-02-09 15:29:44 +01:00
parent 03dcfc6255
commit 9a72413934
12 changed files with 293 additions and 254 deletions

View File

@ -1,5 +1,6 @@
# Next
- User edit view (for admins)
- If is\_cox -> show 100(?) instead of 7 days
# UI
- Next 7 days as columns

BIN
db.sqlite

Binary file not shown.

View File

@ -2,236 +2,9 @@
extern crate rocket;
mod models;
use std::collections::HashMap;
use std::ops::Deref;
use chrono::Duration;
use chrono::Local;
use chrono::NaiveDate;
use rocket::fairing::AdHoc;
use rocket::form;
use rocket::form::validate::range;
use rocket::form::ValueField;
use rocket::http::Cookie;
use rocket::http::CookieJar;
use rocket::request;
use rocket::request::FromRequest;
use rocket::request::Outcome;
use rocket::response::Redirect;
use rocket::Request;
use rocket::{form::Form, fs::FileServer, State};
use rocket_dyn_templates::context;
use rocket_dyn_templates::Template;
use sea_orm::ColumnTrait;
use sea_orm::ModelTrait;
use sea_orm::QueryFilter;
use sea_orm::{
ActiveModelTrait, Database, DatabaseConnection, EntityTrait, Order, QueryOrder, Set,
};
use serde::Serialize;
use crate::models::{day, trip, user};
#[derive(Serialize, Debug)]
struct TripWithUser {
trip: trip::Model,
user: user::Model,
}
impl TripWithUser {
async fn new(trip: trip::Model, db: &DatabaseConnection) -> Self {
Self {
trip: trip.clone(),
user: trip
.find_related(user::Entity)
.one(db)
.await
.unwrap()
.unwrap(),
}
}
}
#[derive(Serialize, Debug)]
struct DayWithTrips {
day: day::Model,
trips: Vec<TripWithUser>,
}
impl DayWithTrips {
async fn new(day: day::Model, db: &DatabaseConnection) -> Self {
let mut trips = Vec::new();
for trip in day.find_related(trip::Entity).all(db).await.unwrap() {
trips.push(TripWithUser::new(trip, db).await);
}
Self { day, trips }
}
}
#[derive(Serialize)]
struct Name(user::Model);
#[rocket::async_trait]
impl<'r> FromRequest<'r> for Name {
type Error = rocket::Error;
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
match req.cookies().get("name") {
Some(name) => {
let db = req.guard::<&'r State<DatabaseConnection>>();
let name = name.value();
let user = find_or_create_user(name, db.await.unwrap().inner()).await;
Outcome::Success(Name(user))
}
None => Outcome::Forward(()), //No cookie set
}
}
}
#[get("/")]
async fn index(db: &State<DatabaseConnection>, name: Name) -> 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 mut dwu = HashMap::new();
for day in days {
dwu.insert(
format!("{}", day.day.format("%Y-%m-%d")),
DayWithTrips::new(day, db.inner()).await,
);
}
let mut next_days = Vec::new();
for i in 0..6 {
next_days.push(Local::now() + Duration::days(i));
}
Template::render("index", context! { dwu, next_days, name })
}
#[get("/", rank = 2)]
fn name() -> Template {
Template::render("name", context! {})
}
#[derive(FromForm)]
struct NameForm(String);
#[post("/setname", data = "<name>")]
fn setname(cookies: &CookieJar<'_>, name: Form<NameForm>) -> Redirect {
cookies.add(Cookie::new("name", name.0.clone()));
Redirect::to("/")
}
#[derive(Debug)]
struct NaiveDateForm(NaiveDate);
impl<'v> rocket::form::FromFormField<'v> for NaiveDateForm {
fn from_value(field: ValueField<'v>) -> form::Result<'v, NaiveDateForm> {
let naivedate = chrono::NaiveDate::parse_from_str(&field.value, "%Y-%m-%d").unwrap(); //TODO:
//fixme
Ok(NaiveDateForm(naivedate))
}
}
impl Deref for NaiveDateForm {
type Target = NaiveDate;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(FromForm, Debug)]
struct DayForm {
day: NaiveDateForm,
#[field(validate = range(0..20))]
planned_amount_cox: i32,
planned_starting_time: Option<String>,
open_registration: bool,
}
#[put("/day", data = "<day>")]
async fn create(db: &State<DatabaseConnection>, day: Form<DayForm>) -> Redirect {
let new_day = day::ActiveModel {
day: Set(*day.day),
planned_amount_cox: Set(day.planned_amount_cox),
planned_starting_time: Set(day.planned_starting_time.clone()),
open_registration: Set(day.open_registration),
};
let day: Option<day::Model> = day::Entity::find_by_id(*day.day)
.one(db.inner())
.await
.unwrap();
match day {
Some(_) => {
new_day.update(db.inner()).await.unwrap(); //TODO: fixme
}
None => {
new_day.insert(db.inner()).await.unwrap(); //TODO: fixme
}
}
Redirect::to("/")
}
#[derive(FromForm)]
struct RegisterForm {
day: NaiveDateForm,
#[field(validate = len(3..))]
name: String,
}
async fn find_or_create_user(name: &str, db: &DatabaseConnection) -> user::Model {
let user = user::Entity::find()
.filter(user::Column::Name.eq(name))
.one(db)
.await
.unwrap();
match user {
Some(user) => user,
None => {
let user = user::ActiveModel {
name: Set(name.clone().into()),
..Default::default()
};
user.insert(db).await.unwrap()
}
}
}
#[put("/register", data = "<register>")]
async fn register(db: &State<DatabaseConnection>, register: Form<RegisterForm>) -> Redirect {
let day = day::Entity::find_by_id(*register.day)
.one(db.inner())
.await
.unwrap()
.expect("There's no trip on this date (yet)");
let user = find_or_create_user(&register.name, db.inner()).await;
let day = format!("{}", day.day.format("%Y-%m-%d"));
let trip = trip::ActiveModel {
day: Set(day),
user_id: Set(user.id),
..Default::default()
};
trip.insert(db.inner()).await.unwrap();
Redirect::to("/")
}
mod rest;
#[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, register, name, setname])
rest::start().await
}

41
src/models/all.rs Normal file
View File

@ -0,0 +1,41 @@
use sea_orm::{DatabaseConnection, ModelTrait};
use serde::Serialize;
use super::{day, trip, user};
#[derive(Serialize, Debug)]
pub struct TripWithUser {
pub trip: trip::Model,
pub user: user::Model,
}
impl TripWithUser {
async fn new(trip: trip::Model, db: &DatabaseConnection) -> Self {
Self {
trip: trip.clone(),
user: trip
.find_related(user::Entity)
.one(db)
.await
.unwrap()
.unwrap(),
}
}
}
#[derive(Serialize, Debug)]
pub struct DayWithTrips {
pub day: day::Model,
pub trips: Vec<TripWithUser>,
}
impl DayWithTrips {
pub async fn new(day: day::Model, db: &DatabaseConnection) -> Self {
let mut trips = Vec::new();
for trip in day.find_related(trip::Entity).all(db).await.unwrap() {
trips.push(TripWithUser::new(trip, db).await);
}
Self { day, trips }
}
}

View File

@ -1,6 +1,6 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0
use sea_orm::entity::prelude::*;
use sea_orm::{entity::prelude::*, Set};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
@ -13,6 +13,22 @@ pub struct Model {
pub open_registration: bool,
}
impl Model {
pub async fn find_or_create_day(date: chrono::NaiveDate, db: &DatabaseConnection) -> Model {
let day = Entity::find_by_id(date).one(db).await.unwrap();
match day {
Some(day) => day,
None => {
let new_day = ActiveModel {
day: Set(date),
..Default::default()
};
new_day.insert(db).await.unwrap()
}
}
}
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::trip::Entity")]

View File

@ -2,6 +2,8 @@
pub mod prelude;
pub mod all;
pub mod day;
pub mod trip;
pub mod user;

View File

@ -1,6 +1,11 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0
use sea_orm::entity::prelude::*;
use rocket::{
http::Status,
request::{self, FromRequest, Outcome},
Request, State,
};
use sea_orm::{entity::prelude::*, Set};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
@ -13,6 +18,49 @@ pub struct Model {
pub is_admin: bool,
}
impl Model {
pub async fn find_or_create_user(name: &str, db: &DatabaseConnection) -> Model {
let user = Entity::find()
.filter(Column::Name.eq(name))
.one(db)
.await
.unwrap();
match user {
Some(user) => user,
None => {
let user = ActiveModel {
name: Set(name.clone().into()),
..Default::default()
};
user.insert(db).await.unwrap()
}
}
}
}
#[derive(Debug)]
pub enum UserError {
NoCookieSet,
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for Model {
type Error = UserError;
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
match req.cookies().get("name") {
Some(name) => {
let db = req.guard::<&'r State<DatabaseConnection>>();
let name = name.value();
let user = Model::find_or_create_user(name, db.await.unwrap().inner()).await;
Outcome::Success(user)
}
None => Outcome::Failure((Status::Unauthorized, UserError::NoCookieSet)),
}
}
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

79
src/rest/mod.rs Normal file
View File

@ -0,0 +1,79 @@
mod restday;
mod restreg;
use std::ops::Deref;
use chrono::{Duration, Local, NaiveDate};
use rocket::{
form::{self, Form, ValueField},
fs::FileServer,
http::{Cookie, CookieJar},
response::Redirect,
Build, Rocket, State,
};
use rocket_dyn_templates::{context, Template};
use sea_orm::{Database, DatabaseConnection};
use super::models::{all::DayWithTrips, day, user};
#[derive(Debug)]
struct NaiveDateForm(NaiveDate);
impl<'v> rocket::form::FromFormField<'v> for NaiveDateForm {
fn from_value(field: ValueField<'v>) -> form::Result<'v, NaiveDateForm> {
let naivedate = chrono::NaiveDate::parse_from_str(&field.value, "%Y-%m-%d").unwrap(); //TODO:
//fixme
Ok(NaiveDateForm(naivedate))
}
}
impl Deref for NaiveDateForm {
type Target = NaiveDate;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[get("/")]
async fn index(db: &State<DatabaseConnection>, user: user::Model) -> Template {
let mut data = Vec::new();
for i in 0..6 {
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);
}
Template::render("index", context! { data, user })
}
#[get("/name")]
fn name() -> Template {
Template::render("name", context! {})
}
#[derive(FromForm)]
struct NameForm {
name: String,
}
#[put("/name", data = "<name>")]
fn savename(name: Form<NameForm>, cookies: &CookieJar) -> Redirect {
cookies.add(Cookie::new("name", name.name.clone()));
Redirect::to("/")
}
#[catch(401)] //unauthorized
fn unauthorized_error() -> Redirect {
Redirect::to("/name")
}
pub async fn start() -> Rocket<Build> {
rocket::build()
.attach(Template::fairing())
.manage(Database::connect("sqlite://db.sqlite").await.unwrap())
.mount("/public", FileServer::from("static/"))
.mount("/", routes![index, name, savename])
.mount("/day", restday::routes())
.mount("/register", restreg::routes())
.register("/", catchers![unauthorized_error])
}

44
src/rest/restday.rs Normal file
View File

@ -0,0 +1,44 @@
use rocket::{form::Form, response::Redirect, Route, State};
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set};
use crate::models::day;
use super::NaiveDateForm;
#[derive(FromForm, Debug)]
struct DayForm {
day: NaiveDateForm,
#[field(validate = range(0..20))]
planned_amount_cox: i32,
planned_starting_time: Option<String>,
open_registration: bool,
}
#[put("/", data = "<day>")]
async fn create(db: &State<DatabaseConnection>, day: Form<DayForm>) -> Redirect {
let new_day = day::ActiveModel {
day: Set(*day.day),
planned_amount_cox: Set(day.planned_amount_cox),
planned_starting_time: Set(day.planned_starting_time.clone()),
open_registration: Set(day.open_registration),
};
let day: Option<day::Model> = day::Entity::find_by_id(*day.day)
.one(db.inner())
.await
.unwrap();
match day {
Some(_) => {
new_day.update(db.inner()).await.unwrap(); //TODO: fixme
}
None => {
new_day.insert(db.inner()).await.unwrap(); //TODO: fixme
}
}
Redirect::to("/")
}
pub fn routes() -> Vec<Route> {
routes![create]
}

39
src/rest/restreg.rs Normal file
View File

@ -0,0 +1,39 @@
use rocket::{form::Form, response::Redirect, Route, State};
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set};
use crate::models::{day, trip, user};
use super::NaiveDateForm;
#[derive(FromForm)]
struct RegisterForm {
day: NaiveDateForm,
#[field(validate = len(3..))]
name: String,
}
#[put("/", data = "<register>")]
async fn register(db: &State<DatabaseConnection>, register: Form<RegisterForm>) -> Redirect {
let day = day::Entity::find_by_id(*register.day)
.one(db.inner())
.await
.unwrap()
.expect("There's no trip on this date (yet)");
let user = user::Model::find_or_create_user(&register.name, db.inner()).await;
let day = format!("{}", day.day.format("%Y-%m-%d"));
let trip = trip::ActiveModel {
day: Set(day),
user_id: Set(user.id),
..Default::default()
};
trip.insert(db.inner()).await.unwrap();
Redirect::to("/")
}
pub fn routes() -> Vec<Route> {
routes![register]
}

View File

@ -1,31 +1,26 @@
{% extends "base" %}
{% block content %}
{% for day in next_days %}
{% set day_string = day | date(format="%Y-%m-%d") %}
{{ day | date(format="%d.%m.%Y")}}
{% for day_with_trip in data %}
{% set day = day_with_trip.day %}
{% set day_string = day.day | date(format="%Y-%m-%d") %}
{% set trips = day_with_trip.trips %}
{{ day.day | date(format="%d.%m.%Y")}}
<br />
{% if dwu[day_string] and dwu[day_string].day.planned_amount_cox > 0%}
{% set cur_day = dwu[day_string].day %}
{% set_global already_registered = false %}
Geplante Steuerpersonen: {{ cur_day.planned_amount_cox}}<br />
Geplante Abfahrtszeit: {{ cur_day.planned_starting_time }}<br />
{% if day.planned_amount_cox > 0%}
Geplante Steuerpersonen: {{ day.planned_amount_cox}}<br />
Geplante Abfahrtszeit: {{ day.planned_starting_time }}<br />
Angemeldete Personen:
<ol>
{% for trip in dwu[day_string].trips %}
<li>
{% if trip.user.name == name.name %}
{% set_global already_registered = true %}
DU
{% else %}
{{ trip.user.name }}
{% endif %}
{% for trip in trips %}
<li>{{ trip.user.name }}</li>
{% endfor %}
</ol>
{% if cur_day.open_registration %}
{% if day.open_registration %}
<details>
<summary class="button">&plus;</summary>
<form method="post" action="/register">
@ -34,7 +29,7 @@
<div class="row">
<div class="six columns">
<label for="name">Name</label>
<input class="u-full-width" type="text" id="name" name="name" value="{% if already_registered == false %}{{ name.name }}{% endif %}" />
<input class="u-full-width" type="text" id="name" name="name" value="{{ user.name }}" />
</div>
<div class="six columns">
<input class="button-primary" type="submit" value="Speichern">
@ -57,15 +52,15 @@
<div class="row">
<div class="three columns">
<label for="planned_amount_cox">Geplante Steuerpersonen</label>
<input class="u-full-width" type="number" id="planned_amount_cox" name="planned_amount_cox" value="{{ cur_day.planned_amount_cox | default(value=2) }}">
<input class="u-full-width" type="number" id="planned_amount_cox" name="planned_amount_cox" value="{{ day.planned_amount_cox }}">
</div>
<div class="three columns">
<label for="planned_starting_time">Geplante Abfahrtszeit</label>
<input class="u-full-width" type="time" id="planned_starting_time" name="planned_starting_time" value="{{ cur_day.planned_starting_time | default(value='17:00') }}">
<input class="u-full-width" type="time" id="planned_starting_time" name="planned_starting_time" value="{% if day.planned_starting_time %}{{ day.planned_starting_time }}{% else %}17:00{%endif%}">
</div>
<div class="three columns">
<label for="open_registration">Registrierung offen</label>
<input class="u-full-width" type="checkbox" id="open_registration" name="open_registration" {% if not cur_day or cur_day.open_registration %} checked="true" {% endif %}/>
<input class="u-full-width" type="checkbox" id="open_registration" name="open_registration" {% if not day or day.open_registration %} checked="true" {% endif %}/>
</div>
<div class="three columns">
<input class="button-primary" type="submit" value="Speichern">

View File

@ -2,7 +2,8 @@
{% block content %}
What's your name?
<form action="/setname" method="post">
<form action="/name" method="post">
<input type="hidden" name="_method" value="put" />
<input type="text" name="name"/>
<input type="submit" value="Speichern"/>
</form>