diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-07-31 20:12:07 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-07-31 20:12:07 +0200 |
commit | a1bba63fae68e7e6a680b14b674d9c47dfcf4862 (patch) | |
tree | 6484735d42d923aad111766b6c6a3cf755e92377 /src | |
parent | 706a887f9b5641d3aa609a0a8b4d3d68b659f653 (diff) | |
download | rocket-blog-a1bba63fae68e7e6a680b14b674d9c47dfcf4862.tar.gz rocket-blog-a1bba63fae68e7e6a680b14b674d9c47dfcf4862.tar.bz2 rocket-blog-a1bba63fae68e7e6a680b14b674d9c47dfcf4862.zip |
Implement deleting users.
Diffstat (limited to 'src')
-rw-r--r-- | src/controllers/users_controller.rs | 16 | ||||
-rw-r--r-- | src/models/user.rs | 5 |
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() } |