From f609ee1cb4474b2906dd1b9c69b464394eafaad0 Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 5 Apr 2023 20:56:36 +0200 Subject: [PATCH] allow admin to create user --- README.md | 3 +-- src/model/user.rs | 11 +++++++++++ src/rest/admin/user.rs | 27 +++++++++++++++++++++++++-- templates/admin/user/index.html.tera | 9 +++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0877936..40e4660 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,8 @@ # TODO - [ ] Allow sign-outs only >2h before event - [ ] Don't allow to be a cox + rower in same trip -- [ ] Admin create user # Frontend Process ´cd frontend´ ´npm install´ -´npm run watch´ \ No newline at end of file +´npm run watch´ diff --git a/src/model/user.rs b/src/model/user.rs index 8d73a0b..acfbb7c 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -89,6 +89,17 @@ impl User { .unwrap(); //TODO: fixme } + pub async fn create(db: &SqlitePool, name: String, is_guest: bool) -> bool { + sqlx::query!( + "INSERT INTO USER(name, is_guest) VALUES (?,?)", + name, + is_guest, + ) + .execute(db) + .await + .is_ok() + } + pub async fn find_by_id(db: &SqlitePool, id: i32) -> Result { let user: User = sqlx::query_as!( User, diff --git a/src/rest/admin/user.rs b/src/rest/admin/user.rs index 6512891..6a0be2d 100644 --- a/src/rest/admin/user.rs +++ b/src/rest/admin/user.rs @@ -57,6 +57,29 @@ async fn update( Flash::success(Redirect::to("/admin/user"), "Successfully updated user") } -pub fn routes() -> Vec { - routes![index, resetpw, update] +#[derive(FromForm)] +struct UserAddForm { + name: String, + is_guest: bool, +} + +#[post("/user/new", data = "")] +async fn create( + db: &State, + data: Form, + _admin: AdminUser, +) -> Flash { + if User::create(db, data.name.clone(), data.is_guest).await { + //TODO: fix clone() above + Flash::success(Redirect::to("/admin/user"), "Successfully created user") + } else { + Flash::error( + Redirect::to("/admin/user"), + format!("User {} already exists", data.name), + ) + } +} + +pub fn routes() -> Vec { + routes![index, resetpw, update, create] } diff --git a/templates/admin/user/index.html.tera b/templates/admin/user/index.html.tera index 4f45d0d..c24a78d 100644 --- a/templates/admin/user/index.html.tera +++ b/templates/admin/user/index.html.tera @@ -4,6 +4,15 @@

Users

+
+ Name:
+ Guest
+ +
+ + + + {% for user in users %}