allow admin to create user

This commit is contained in:
philipp 2023-04-05 20:56:36 +02:00
parent ed28a50d98
commit f609ee1cb4
4 changed files with 46 additions and 4 deletions

View File

@ -7,7 +7,6 @@
# TODO # TODO
- [ ] Allow sign-outs only >2h before event - [ ] Allow sign-outs only >2h before event
- [ ] Don't allow to be a cox + rower in same trip - [ ] Don't allow to be a cox + rower in same trip
- [ ] Admin create user
# Frontend Process # Frontend Process
´cd frontend´ ´cd frontend´

View File

@ -89,6 +89,17 @@ impl User {
.unwrap(); //TODO: fixme .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<Self, sqlx::Error> { pub async fn find_by_id(db: &SqlitePool, id: i32) -> Result<Self, sqlx::Error> {
let user: User = sqlx::query_as!( let user: User = sqlx::query_as!(
User, User,

View File

@ -57,6 +57,29 @@ async fn update(
Flash::success(Redirect::to("/admin/user"), "Successfully updated user") Flash::success(Redirect::to("/admin/user"), "Successfully updated user")
} }
pub fn routes() -> Vec<Route> { #[derive(FromForm)]
routes![index, resetpw, update] struct UserAddForm {
name: String,
is_guest: bool,
}
#[post("/user/new", data = "<data>")]
async fn create(
db: &State<SqlitePool>,
data: Form<UserAddForm>,
_admin: AdminUser,
) -> Flash<Redirect> {
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<Route> {
routes![index, resetpw, update, create]
} }

View File

@ -4,6 +4,15 @@
<h1>Users</h1> <h1>Users</h1>
<form action="/admin/user/new" method="post">
Name: <input type="text" name="name" /><br />
Guest <input type="checkbox" name="is_guest" checked /><br />
<input type="submit" />
</form>
{% for user in users %} {% for user in users %}
<form action="/admin/user" method="post"> <form action="/admin/user" method="post">
<input type="hidden" name="id" value="{{ user.id }}" /> <input type="hidden" name="id" value="{{ user.id }}" />