use std::collections::HashMap; use crate::model::{ role::Role, user::{AdminUser, User, UserWithRoles}, }; use futures::future::join_all; use rocket::{ form::Form, get, post, request::FlashMessage, response::{Flash, Redirect}, routes, FromForm, Route, State, }; use rocket_dyn_templates::{tera::Context, Template}; use sqlx::SqlitePool; #[get("/user")] async fn index( db: &State, admin: AdminUser, flash: Option>, ) -> Template { let user_futures: Vec<_> = User::all(db) .await .into_iter() .map(|u| async move { UserWithRoles::from_user(u, db).await }) .collect(); let users: Vec = join_all(user_futures).await; let roles = Role::all(db).await; let mut context = Context::new(); if let Some(msg) = flash { context.insert("flash", &msg.into_inner()); } context.insert("users", &users); context.insert("roles", &roles); context.insert( "loggedin_user", &UserWithRoles::from_user(admin.user, db).await, ); Template::render("admin/user/index", context.into_json()) } #[get("/user//reset-pw")] async fn resetpw(db: &State, _admin: AdminUser, user: i32) -> Flash { let user = User::find_by_id(db, user).await; match user { Some(user) => { user.reset_pw(db).await; Flash::success( Redirect::to("/admin/user"), format!("Passwort von {} zurückgesetzt", user.name), ) } None => Flash::error(Redirect::to("/admin/user"), "User does not exist"), } } #[get("/user//delete")] async fn delete(db: &State, _admin: AdminUser, user: i32) -> Flash { let user = User::find_by_id(db, user).await; match user { Some(user) => { user.delete(db).await; Flash::success( Redirect::to("/admin/user"), format!("Benutzer {} gelöscht", user.name), ) } None => Flash::error(Redirect::to("/admin/user"), "User does not exist"), } } #[derive(FromForm, Debug)] pub struct UserEditForm { pub(crate) id: i32, pub(crate) dob: Option, pub(crate) weight: Option, pub(crate) sex: Option, pub(crate) roles: HashMap, pub(crate) member_since_date: Option, pub(crate) birthdate: Option, pub(crate) mail: Option, pub(crate) nickname: Option, pub(crate) notes: Option, pub(crate) phone: Option, pub(crate) address: Option, } #[post("/user", data = "")] async fn update( db: &State, data: Form, _admin: AdminUser, ) -> Flash { let user = User::find_by_id(db, data.id).await; let Some(user) = user else { return Flash::error( Redirect::to("/admin/user"), format!("User with ID {} does not exist!", data.id), ); }; user.update(db, data.into_inner()).await; Flash::success(Redirect::to("/admin/user"), "Successfully updated user") } #[derive(FromForm)] struct UserAddForm<'r> { name: &'r str, } #[post("/user/new", data = "")] async fn create( db: &State, data: Form>, _admin: AdminUser, ) -> Flash { if User::create(db, data.name).await { 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, delete] }