diff --git a/db.txt b/db.txt new file mode 100644 index 0000000..0e49bad --- /dev/null +++ b/db.txt @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS "day" ( "day" text NOT NULL PRIMARY KEY, "planned_amount_cox" integer NOT NULL DEFAULT 0, "planned_starting_time" text, "open_registration" boolean NOT NULL DEFAULT TRUE ); +CREATE TABLE IF NOT EXISTS "user" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" text NOT NULL UNIQUE, "pw" text, "is_cox" boolean NOT NULL DEFAULT FALSE, "is_admin" boolean NOT NULL DEFAULT FALSE ); +CREATE TABLE IF NOT EXISTS "trip" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "day" text NOT NULL, "user_id" integer NOT NULL, "cox_id" integer, "begin" text, "created" text NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY ("day") REFERENCES "day" ("day"), FOREIGN KEY ("user_id") REFERENCES "user" ("id"), FOREIGN KEY ("cox_id") REFERENCES "trip" ("id") ); +create unique index UNIQ_trip on trip("day", "user_id", IFNULL(cox_id, ''), IFNULL(begin,'')); diff --git a/src/models/trip.rs b/src/models/trip.rs index c04afce..08cf199 100644 --- a/src/models/trip.rs +++ b/src/models/trip.rs @@ -6,9 +6,9 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] #[sea_orm(table_name = "trip")] pub struct Model { - #[sea_orm(primary_key, auto_increment = false)] + #[sea_orm(primary_key)] + pub id: i32, pub day: String, - #[sea_orm(primary_key, auto_increment = false)] pub user_id: i32, pub cox_id: Option, pub begin: Option, diff --git a/src/rest/restreg.rs b/src/rest/restreg.rs index 4f628f6..9a77381 100644 --- a/src/rest/restreg.rs +++ b/src/rest/restreg.rs @@ -67,10 +67,7 @@ async fn register( #[derive(FromForm)] struct DeleteForm { - day: NaiveDateForm, - user: i32, - cox_id: Option, - begin: Option, + id: i32, } #[delete("/", data = "")] @@ -79,40 +76,31 @@ async fn delete( delete: Form, user: user::Model, ) -> Flash { - let day = day::Entity::find_by_id(*delete.day) - .one(db.inner()) - .await - .unwrap() - .expect("There's no trip on this date (yet)"); - - if delete.user != user.id { - log::error!("{} tried to delete a registration from user_id {} on day {:?} (probably hand-crafted request)", user.name, delete.user, delete.day); - return Flash::error( - Redirect::to("/"), - "Du kannst nur deine eigenen Anmeldungen löschen!", - ); - } - - let trip = trip::Entity::find() - .filter(trip::Column::Day.eq(format!("{}", day.day.format("%Y-%m-%d")))) - .filter(trip::Column::UserId.eq(user.id)) - .filter(trip::Column::CoxId.eq(delete.cox_id)) - .filter(trip::Column::Begin.eq(delete.begin.clone())) + let trip = trip::Entity::find_by_id(delete.id) .one(db.inner()) .await .unwrap(); + match trip { None => { - log::error!("Tried to register w/o being authenticated (prob. hand crafted request (user.name = {})", user.name); + log::error!("Tried to delete registration of non-existing trip (prob. hand crafted request (user.name = {})", user.name); return Flash::error(Redirect::to("/"), "Du bist gar nicht angemeldet!"); } Some(trip) => { + if trip.user_id != user.id { + log::error!( + "{} tried to delete a registration from user_id {} (probably hand-crafted request)", + user.name, + delete.id + ); + return Flash::error( + Redirect::to("/"), + "Du kannst nur deine eigenen Anmeldungen löschen!", + ); + } log::info!("User {} deleted the registration for {:?}", user.name, trip); trip::Entity::delete(trip::ActiveModel { - day: Set(trip.day), - user_id: Set(trip.user_id), - //cox_id: Set(delete.cox_id), - begin: Set(delete.begin.clone()), + id: Set(trip.id), ..Default::default() }) .exec(db.inner()) diff --git a/templates/index.html.tera b/templates/index.html.tera index 4a613ab..ec0af98 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -84,10 +84,7 @@
{{ default_trips | length }} angemeldete Person{{ default_trips | length | pluralize(plural="en") }}: {{ cox | length }} Steuerperson{{ cox | length | pluralize(plural="en") }} ({% for c in cox %}{{ c.user.name }} {% if c.user.name == user.name %}
- - - - +
{% endif %} {% endfor %}), {{ rowers | length }} Ruderer:
@@ -99,11 +96,7 @@ {% if r.user.name == user.name %}
- - - - - +
@@ -136,14 +129,11 @@ {% for trip in indep_trips %} {% if trip.trip.begin %} {{trip.user.name}} @ {{trip.trip.begin}} - {% set rowers = indep_trips | filter(attribute="trip.cox_id", value=trip.user.id) %} + {% set rowers = indep_trips | filter(attribute="trip.cox_id", value=trip.trip.id) %} {% if trip.user.name == user.name and rowers | length == 0 %}
- - - - +
{% endif %} @@ -155,11 +145,7 @@ {% if r.user.name == user.name %}
- - - - - +
@@ -172,7 +158,7 @@
- +