add ergo tool
This commit is contained in:
@ -58,12 +58,15 @@ async fn delete(db: &State<SqlitePool>, _admin: AdminUser, user: i32) -> Flash<R
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct UserEditForm {
|
||||
id: i32,
|
||||
is_guest: bool,
|
||||
is_cox: bool,
|
||||
is_admin: bool,
|
||||
is_tech: bool,
|
||||
pub struct UserEditForm {
|
||||
pub(crate) id: i32,
|
||||
pub(crate) is_guest: bool,
|
||||
pub(crate) is_cox: bool,
|
||||
pub(crate) is_admin: bool,
|
||||
pub(crate) is_tech: bool,
|
||||
pub(crate) dob: Option<String>,
|
||||
pub(crate) weight: Option<String>,
|
||||
pub(crate) sex: Option<String>,
|
||||
}
|
||||
|
||||
#[post("/user", data = "<data>")]
|
||||
@ -80,8 +83,7 @@ async fn update(
|
||||
);
|
||||
};
|
||||
|
||||
user.update(db, data.is_cox, data.is_admin, data.is_guest, data.is_tech)
|
||||
.await;
|
||||
user.update(db, data.into_inner()).await;
|
||||
|
||||
Flash::success(Redirect::to("/admin/user"), "Successfully updated user")
|
||||
}
|
||||
|
191
src/tera/ergo.rs
Normal file
191
src/tera/ergo.rs
Normal file
@ -0,0 +1,191 @@
|
||||
use std::env;
|
||||
|
||||
use rocket::{
|
||||
form::Form,
|
||||
fs::TempFile,
|
||||
get,
|
||||
http::ContentType,
|
||||
post,
|
||||
request::FlashMessage,
|
||||
response::{Flash, Redirect},
|
||||
routes, FromForm, Route, State,
|
||||
};
|
||||
use rocket_dyn_templates::{context, Template};
|
||||
use serde::Serialize;
|
||||
use sqlx::SqlitePool;
|
||||
use tera::Context;
|
||||
|
||||
use crate::model::{
|
||||
log::Log,
|
||||
user::{AdminUser, NonGuestUser, User},
|
||||
};
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ErgoStat {
|
||||
name: String,
|
||||
dob: Option<String>,
|
||||
weight: Option<String>,
|
||||
sex: Option<String>,
|
||||
result: Option<String>,
|
||||
}
|
||||
|
||||
#[get("/final")]
|
||||
async fn send(db: &State<SqlitePool>, _user: AdminUser) -> Template {
|
||||
let thirty = sqlx::query_as!(
|
||||
ErgoStat,
|
||||
"SELECT name, dirty_thirty as result, dob, weight, sex FROM user WHERE deleted = 0 AND dirty_thirty is not null ORDER BY result DESC"
|
||||
)
|
||||
.fetch_all(db.inner())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let dozen= sqlx::query_as!(
|
||||
ErgoStat,
|
||||
"SELECT name, dirty_dozen as result, dob, weight, sex FROM user WHERE deleted = 0 AND dirty_dozen is not null ORDER BY result DESC"
|
||||
)
|
||||
.fetch_all(db.inner())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Template::render(
|
||||
"ergo.final",
|
||||
context!(loggedin_user: &_user.user, thirty, dozen),
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/reset")]
|
||||
async fn reset(db: &State<SqlitePool>, _user: AdminUser) -> Flash<Redirect> {
|
||||
sqlx::query!("UPDATE user SET dirty_thirty = NULL, dirty_dozen = NULL;")
|
||||
.execute(db.inner())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Flash::success(
|
||||
Redirect::to("/ergo"),
|
||||
"Erfolgreich zurückgesetzt (Bilder müssen manuell gelöscht werden!)",
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn index(
|
||||
db: &State<SqlitePool>,
|
||||
user: NonGuestUser,
|
||||
flash: Option<FlashMessage<'_>>,
|
||||
) -> Template {
|
||||
let users = User::ergo(db).await;
|
||||
|
||||
let thirty = sqlx::query_as!(
|
||||
ErgoStat,
|
||||
"SELECT name, dirty_thirty as result, dob, weight, sex FROM user WHERE deleted = 0 AND dirty_thirty is not null ORDER BY result DESC"
|
||||
)
|
||||
.fetch_all(db.inner())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let dozen= sqlx::query_as!(
|
||||
ErgoStat,
|
||||
"SELECT name, dirty_dozen as result, dob, weight, sex FROM user WHERE deleted = 0 AND dirty_dozen is not null ORDER BY result DESC"
|
||||
)
|
||||
.fetch_all(db.inner())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut context = Context::new();
|
||||
if let Some(msg) = flash {
|
||||
context.insert("flash", &msg.into_inner());
|
||||
}
|
||||
context.insert("loggedin_user", &user.user);
|
||||
context.insert("users", &users);
|
||||
context.insert("thirty", &thirty);
|
||||
context.insert("dozen", &dozen);
|
||||
|
||||
Template::render("ergo", context.into_json())
|
||||
}
|
||||
|
||||
#[derive(FromForm, Debug)]
|
||||
pub struct ErgoToAdd<'a> {
|
||||
user: i64,
|
||||
result: String,
|
||||
proof: TempFile<'a>,
|
||||
}
|
||||
|
||||
#[post("/thirty", data = "<data>", format = "multipart/form-data")]
|
||||
async fn new_thirty(
|
||||
db: &State<SqlitePool>,
|
||||
mut data: Form<ErgoToAdd<'_>>,
|
||||
created_by: NonGuestUser,
|
||||
) -> Flash<Redirect> {
|
||||
let user = User::find_by_id(db, data.user as i32).await.unwrap();
|
||||
|
||||
let extension = if data.proof.content_type() == Some(&ContentType::JPEG) {
|
||||
"jpg"
|
||||
} else {
|
||||
return Flash::error(Redirect::to("/ergo"), "Es werden nur JPG Bilder akzeptiert");
|
||||
};
|
||||
let base_dir = env::current_dir().unwrap();
|
||||
let file_path = base_dir.join(format!("data-ergo/thirty/{}.{extension}", user.name));
|
||||
if let Err(e) = data.proof.move_copy_to(file_path).await {
|
||||
eprintln!("Failed to persist file: {:?}", e);
|
||||
}
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE user SET dirty_thirty = ? where id = ?",
|
||||
data.result,
|
||||
data.user
|
||||
)
|
||||
.execute(db.inner())
|
||||
.await
|
||||
.unwrap(); //Okay, because we can only create a User of a valid id
|
||||
|
||||
Log::create(
|
||||
db,
|
||||
format!("{created_by:?} created thirty-ergo entry: {data:?}"),
|
||||
)
|
||||
.await;
|
||||
|
||||
Flash::success(Redirect::to("/ergo"), "Erfolgreich eingetragen")
|
||||
}
|
||||
|
||||
#[post("/dozen", data = "<data>", format = "multipart/form-data")]
|
||||
async fn new_dozen(
|
||||
db: &State<SqlitePool>,
|
||||
mut data: Form<ErgoToAdd<'_>>,
|
||||
created_by: NonGuestUser,
|
||||
) -> Flash<Redirect> {
|
||||
let user = User::find_by_id(db, data.user as i32).await.unwrap();
|
||||
|
||||
let extension = if data.proof.content_type() == Some(&ContentType::JPEG) {
|
||||
"jpg"
|
||||
} else {
|
||||
return Flash::error(Redirect::to("/ergo"), "Es werden nur JPG Bilder akzeptiert");
|
||||
};
|
||||
let base_dir = env::current_dir().unwrap();
|
||||
let file_path = base_dir.join(format!("data-ergo/dozen/{}.{extension}", user.name));
|
||||
if let Err(e) = data.proof.move_copy_to(file_path).await {
|
||||
eprintln!("Failed to persist file: {:?}", e);
|
||||
}
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE user SET dirty_dozen = ? where id = ?",
|
||||
data.result,
|
||||
data.user
|
||||
)
|
||||
.execute(db.inner())
|
||||
.await
|
||||
.unwrap(); //Okay, because we can only create a User of a valid id
|
||||
|
||||
Log::create(
|
||||
db,
|
||||
format!("{created_by:?} created dozen-ergo entry: {data:?}"),
|
||||
)
|
||||
.await;
|
||||
|
||||
Flash::success(Redirect::to("/ergo"), "Erfolgreich eingetragen")
|
||||
}
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![index, new_thirty, new_dozen, send, reset]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {}
|
@ -20,10 +20,11 @@ use crate::model::{
|
||||
usertrip::{UserTrip, UserTripDeleteError, UserTripError},
|
||||
};
|
||||
|
||||
mod admin;
|
||||
pub(crate) mod admin;
|
||||
mod auth;
|
||||
mod boatdamage;
|
||||
mod cox;
|
||||
mod ergo;
|
||||
mod log;
|
||||
mod misc;
|
||||
mod stat;
|
||||
@ -56,6 +57,7 @@ async fn index(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage<'_
|
||||
if let Some(msg) = flash {
|
||||
context.insert("flash", &msg.into_inner());
|
||||
}
|
||||
println!("{user:#?}");
|
||||
context.insert("loggedin_user", &user);
|
||||
context.insert("days", &days);
|
||||
Template::render("index", context.into_json())
|
||||
@ -212,6 +214,7 @@ pub fn config(rocket: Rocket<Build>) -> Rocket<Build> {
|
||||
.mount("/auth", auth::routes())
|
||||
.mount("/wikiauth", routes![wikiauth])
|
||||
.mount("/log", log::routes())
|
||||
.mount("/ergo", ergo::routes())
|
||||
.mount("/stat", stat::routes())
|
||||
.mount("/boatdamage", boatdamage::routes())
|
||||
.mount("/cox", cox::routes())
|
||||
|
Reference in New Issue
Block a user