diff --git a/src/model/user.rs b/src/model/user.rs index abe3487..8634682 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -690,18 +690,8 @@ WHERE family_id IS NULL; } pub async fn ergo(db: &SqlitePool) -> Vec { - sqlx::query_as!( - Self, - " -SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token -FROM user -WHERE deleted = 0 AND dob != '' and weight != '' and sex != '' -ORDER BY name - " - ) - .fetch_all(db) - .await - .unwrap() + let ergo = Role::find_by_name(db, "ergo").await.unwrap(); + Self::all_with_role(db, &ergo).await } pub async fn cox(db: &SqlitePool) -> Vec { @@ -735,6 +725,19 @@ ORDER BY last_access DESC .is_ok() } + pub async fn update_ergo(&self, db: &SqlitePool, dob: i32, weight: i64, sex: &str) { + sqlx::query!( + "UPDATE user SET dob = ?, weight = ?, sex = ? where id = ?", + dob, + weight, + sex, + self.id + ) + .execute(db) + .await + .unwrap(); //Okay, because we can only create a User of a valid id + } + pub async fn update(&self, db: &SqlitePool, data: UserEditForm<'_>) -> Result<(), String> { let mut db = db.begin().await.map_err(|e| e.to_string())?; diff --git a/src/tera/ergo.rs b/src/tera/ergo.rs index 493ae64..a029a7b 100644 --- a/src/tera/ergo.rs +++ b/src/tera/ergo.rs @@ -1,6 +1,6 @@ use std::env; -use chrono::Utc; +use chrono::{Datelike, Utc}; use rocket::{ form::Form, fs::TempFile, @@ -17,8 +17,7 @@ use sqlx::SqlitePool; use tera::Context; use crate::model::{ - log::Log, - user::{AdminUser, User, UserWithDetails}, + log::Log, notification::Notification, role::Role, user::{AdminUser, User, UserWithDetails} }; #[derive(Serialize)] @@ -50,7 +49,7 @@ async fn send(db: &State, _user: AdminUser) -> Template { .unwrap(); Template::render( - "ergo.final", + "ergo/final", context!(loggedin_user: &UserWithDetails::from_user(_user.user, db).await, thirty, dozen), ) } @@ -98,6 +97,19 @@ async fn update( #[get("/")] async fn index(db: &State, user: User, flash: Option>) -> Template { + let mut context = Context::new(); + if let Some(msg) = flash { + context.insert("flash", &msg.into_inner()); + } + context.insert("loggedin_user", &UserWithDetails::from_user(user.clone(), db).await); + + if !user.has_role(db, "ergo").await { + return Template::render( + "ergo/missing-data", + context.into_json() + ); + } + let users = User::ergo(db).await; let thirty = sqlx::query_as!( @@ -116,16 +128,56 @@ async fn index(db: &State, user: User, flash: Option, + data: Form, + user: User, +) -> Flash { + if user.has_role(db, "ergo").await { + return + Flash::error(Redirect::to("/ergo"), "Du hast deine Daten schon eingegeben. Wenn du sie updaten willst, melde dich bitte bei it@rudernlinz.at"); + } + + // check data + if data.birthyear <1900 || data.birthyear > chrono::Utc::now().year() - 5 { + return + Flash::error(Redirect::to("/ergo"), "Bitte überprüfe dein Geburtsjahr..."); + } + if data.weight < 20 || data.weight > 200 { + return + Flash::error(Redirect::to("/ergo"), "Bitte überprüfe dein Gewicht..."); + } + if &data.sex != "f" && &data.sex != "m"{ + return + Flash::error(Redirect::to("/ergo"), "Bitte überprüfe dein Geschlecht..."); + } + + // set data + user.update_ergo(db, data.birthyear, data.weight, &data.sex).await; + + // inform all other `ergo` users + let ergo = Role::find_by_name(db, "ergo").await.unwrap(); + Notification::create_for_role(db, &ergo, &format!("{} nimmt heuer an der Ergochallenge teil 💪", user.name), "Ergo Challenge", None, None).await; + + // add to `ergo` group + user.add_role(db,&ergo).await.unwrap(); + + Flash::success(Redirect::to("/ergo"), "Du hast deine Daten erfolgreich eingegeben. Viel Spaß beim Schwitzen :-)") } #[derive(FromForm, Debug)] @@ -173,6 +225,9 @@ async fn new_thirty( ) .await; + let ergo = Role::find_by_name(db, "ergo").await.unwrap(); + Notification::create_for_role(db, &ergo, &format!("{} ist gerade die Dirty Thirty Challenge gefahren 🥵", user.name), "Ergo Challenge", Some("/ergo"), None).await; + Flash::success(Redirect::to("/ergo"), "Erfolgreich eingetragen") } @@ -214,11 +269,14 @@ async fn new_dozen( ) .await; + let ergo = Role::find_by_name(db, "ergo").await.unwrap(); + Notification::create_for_role(db, &ergo, &format!("{} ist gerade die Dirty Dozen Challenge gefahren 🥵", user.name), "Ergo Challenge", Some("/ergo"), None).await; + Flash::success(Redirect::to("/ergo"), "Erfolgreich eingetragen") } pub fn routes() -> Vec { - routes![index, new_thirty, new_dozen, send, reset, update] + routes![index, new_thirty, new_dozen, send, reset, update, new_user] } #[cfg(test)] diff --git a/src/tera/mod.rs b/src/tera/mod.rs index 363b6ac..486661e 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -1,6 +1,6 @@ use std::{fs::OpenOptions, io::Write}; -use chrono::Local; +use chrono::{Datelike, Local}; use rocket::{ catch, catchers, fairing::{AdHoc, Fairing, Info, Kind}, @@ -63,6 +63,11 @@ async fn index(db: &State, user: User, flash: Option= 10 { + context.insert("show_quick_ergo_button", "yes"); + } + context.insert("achievements", &Achievements::for_user(db, &user).await); context.insert("notifications", &Notification::for_user(db, &user).await); context.insert("loggedin_user", &UserWithDetails::from_user(user, db).await); diff --git a/templates/ergo.html.tera b/templates/ergo.html.tera deleted file mode 100644 index dff7d8a..0000000 --- a/templates/ergo.html.tera +++ /dev/null @@ -1,205 +0,0 @@ -{% import "includes/macros" as macros %} -{% extends "base" %} -{% block content %} -
-

Ergo Challenges

-
- -
-

- Neuer Eintrag -

-
- Dirty Thirty -
-
-
- - -
- {{ macros::input(label="Distanz [m]", name="result", required=true, type="number", class="input rounded-md") }} -
- - -
-
- -
-
-
-
-
- Dirty Dozen -
-
-
- - -
- {{ macros::input(label="Zeit [hh:mm:ss.s] oder Distanz [m]", name="result", required=true, type="text", class="input rounded-md", pattern="(?:\d+:\d{2}:\d{2}\.\d+|\d{1,2}:\d{2}\.\d+|\d+(\.\d+)?)") }} -
- - -
-
- -
-
-
-
-
-
-

Aktuelle Woche

-
- - Dirty Thirty ({{ thirty | length }}) - -
-
    - {% for stat in thirty %} -
  1. - {{ stat.name }}: {{ stat.result }} -
  2. - {% endfor %} -
-
-
-
- - Dirty Dozen ({{ dozen | length }}) - -
-
    - {% for stat in dozen %} -
  1. - {{ stat.name }}: {{ stat.result }} -
  2. - {% endfor %} -
-
-
-
- {% if "admin" in loggedin_user.roles %} -
-

Update

-
- - Dirty Thirty ({{ thirty | length }}) - -
-
    - {% for stat in thirty %} -
  1. -
    - {{ stat.name }}: - - -
    -
  2. - {% endfor %} -
-
-
-
- - Dirty Dozen ({{ dozen | length }}) - -
-
    - {% for stat in dozen %} -
  1. -
    - {{ stat.name }}: - - -
    -
  2. - {% endfor %} -
-
-
-
-
- {% endif %} -
- -{% endblock content %} diff --git a/templates/ergo.final.html.tera b/templates/ergo/final.html.tera similarity index 100% rename from templates/ergo.final.html.tera rename to templates/ergo/final.html.tera diff --git a/templates/ergo/index.html.tera b/templates/ergo/index.html.tera new file mode 100644 index 0000000..4d3f010 --- /dev/null +++ b/templates/ergo/index.html.tera @@ -0,0 +1,230 @@ +{% import "includes/macros" as macros %} +{% extends "base" %} +{% block content %} +
+

Ergo Challenges

+
+ +
+

+ Neuer Eintrag +

+
+ Dirty Thirty +
+
+
+ + +
+ {{ macros::input(label="Distanz [m]", name="result", required=true, type="number", class="input rounded-md") }} +
+ + +
+
+ +
+
+
+
+
+ Dirty Dozen +
+
+
+ + +
+ {{ macros::input(label="Zeit [hh:mm:ss.s] oder Distanz [m]", name="result", required=true, type="text", class="input rounded-md", pattern="(?:\d+:\d{2}:\d{2}\.\d+|\d{1,2}:\d{2}\.\d+|\d+(\.\d+)?)") }} +
+ + +
+
+ +
+
+
+
+
+
+

Aktuelle Woche

+
+ + Dirty Thirty ({{ thirty | length }}) + +
+
    + {% for stat in thirty %} +
  1. + {{ stat.name }}: {{ stat.result }} +
  2. + {% endfor %} +
+
+
+
+ + Dirty Dozen ({{ dozen | length }}) + +
+
    + {% for stat in dozen %} +
  1. + {{ stat.name }}: {{ stat.result }} +
  2. + {% endfor %} +
+
+
+
+ {% if "admin" in loggedin_user.roles %} +
+

Update

+
+ + Dirty Thirty ({{ thirty | length }}) + +
+
    + {% for stat in thirty %} +
  1. +
    + {{ stat.name }}: + + +
    +
  2. + {% endfor %} +
+
+
+
+ + Dirty Dozen ({{ dozen | length }}) + +
+
    + {% for stat in dozen %} +
  1. +
    + {{ stat.name }}: + + +
    +
  2. + {% endfor %} +
+
+
+
+
+ {% endif %} +
+ + {% endblock content %} diff --git a/templates/ergo/missing-data.html.tera b/templates/ergo/missing-data.html.tera new file mode 100644 index 0000000..29df513 --- /dev/null +++ b/templates/ergo/missing-data.html.tera @@ -0,0 +1,37 @@ +{% import "includes/macros" as macros %} +{% import "includes/forms/boat" as boat %} +{% extends "base" %} +{% block content %} +
+

Ergo Challenge

+
+ +
+
+{% endblock content %} diff --git a/templates/index.html.tera b/templates/index.html.tera index 890ff4f..232d8d6 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -6,12 +6,23 @@

Ruder­assistent

+ {% if show_quick_ergo_button %} + + {% endif %} {% if notifications %}
{% endif %} + {% if "schnupper-betreuer" in loggedin_user.roles %} {% endif %} - {% if loggedin_user.weight and loggedin_user.sex and loggedin_user.dob %} - - {% endif %}