aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/controllers/users_controller.rs16
-rw-r--r--src/models/user.rs5
2 files changed, 20 insertions, 1 deletions
diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs
index b2ba308..87a0b16 100644
--- a/src/controllers/users_controller.rs
+++ b/src/controllers/users_controller.rs
@@ -22,6 +22,20 @@ fn index(flash: Option<rocket::request::FlashMessage>, conn: utils::DbConn) -> u
}
}
+#[get("/<id>/delete")]
+fn delete(id: i32, route: &rocket::Route, conn: utils::DbConn) -> Flash<Redirect> {
+ match ::models::User::delete(id, conn) {
+ Ok(user) => Flash::success(
+ Redirect::to(route.base.path()),
+ format!("User {} was successfully deleted.", user.username)
+ ),
+ Err(msg) => Flash::error(
+ Redirect::to(route.base.path()),
+ format!("Error deleting user: {}", msg)
+ )
+ }
+}
+
#[derive(BartDisplay)]
#[template = "templates/new_user.html"]
pub struct NewUserTemplate {
@@ -80,5 +94,5 @@ fn create(user: Form<RegisterUserForm>, route: &rocket::Route, conn: utils::DbCo
}
pub fn routes() -> Vec<rocket::Route> {
- routes![index, new, create]
+ routes![index, new, create, delete]
}
diff --git a/src/models/user.rs b/src/models/user.rs
index 0345e92..9587fd3 100644
--- a/src/models/user.rs
+++ b/src/models/user.rs
@@ -38,6 +38,11 @@ impl User {
.get_result(&*conn)
}
+ pub fn delete(user_id: i32, conn: utils::DbConn) -> QueryResult<User> {
+ use ::schema::users::dsl::*;
+ diesel::delete(users.filter(id.eq(user_id))).get_result::<User>(&*conn)
+ }
+
pub fn realname(&self) -> String {
self.realname.as_ref().unwrap_or(&String::new()).clone()
}