aboutsummaryrefslogtreecommitdiffstats
path: root/src/controllers
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2018-07-31 22:13:30 +0200
committerHarald Eilertsen <haraldei@anduin.net>2018-07-31 22:13:30 +0200
commitd285d0525f86160a61067942346fb40f2bc5c797 (patch)
treefa7da0b2bf0de8604dafb8cf3a4aac68165ee56c /src/controllers
parenta1bba63fae68e7e6a680b14b674d9c47dfcf4862 (diff)
downloadrocket-blog-d285d0525f86160a61067942346fb40f2bc5c797.tar.gz
rocket-blog-d285d0525f86160a61067942346fb40f2bc5c797.tar.bz2
rocket-blog-d285d0525f86160a61067942346fb40f2bc5c797.zip
Implemet editing users.
Diffstat (limited to 'src/controllers')
-rw-r--r--src/controllers/users_controller.rs79
1 files changed, 78 insertions, 1 deletions
diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs
index 87a0b16..a3281de 100644
--- a/src/controllers/users_controller.rs
+++ b/src/controllers/users_controller.rs
@@ -22,6 +22,25 @@ fn index(flash: Option<rocket::request::FlashMessage>, conn: utils::DbConn) -> u
}
}
+#[derive(BartDisplay)]
+#[template = "templates/edit_user.html"]
+pub struct EditUserTemplate {
+ user: ::models::User
+}
+
+implement_responder_for!(EditUserTemplate);
+
+#[get("/<id>/edit")]
+fn edit(id: i32, conn: utils::DbConn) -> utils::Page<EditUserTemplate> {
+ utils::Page {
+ title: String::from("Edit users"),
+ flash: None,
+ content: EditUserTemplate {
+ user: ::models::User::by_id(id, &conn).unwrap()
+ }
+ }
+}
+
#[get("/<id>/delete")]
fn delete(id: i32, route: &rocket::Route, conn: utils::DbConn) -> Flash<Redirect> {
match ::models::User::delete(id, conn) {
@@ -78,6 +97,21 @@ impl RegisterUserForm {
})
}
}
+
+ fn attrs(&self) -> Result<::models::User, &'static str> {
+ if self.password != self.password_confirm {
+ Err("Passwords don't match")
+ }
+ else {
+ Ok(::models::User {
+ id: Default::default(),
+ username: self.username.clone(),
+ realname: self.realname.clone(),
+ email: self.email.clone(),
+ password: self.password.clone()
+ })
+ }
+ }
}
#[post("/create", data="<user>")]
@@ -93,6 +127,49 @@ fn create(user: Form<RegisterUserForm>, route: &rocket::Route, conn: utils::DbCo
}
}
+#[post("/<id>/update", data="<user_form>")]
+fn update(id: i32, user_form: Form<RegisterUserForm>, route: &rocket::Route, conn: utils::DbConn) -> Flash<Redirect> {
+ match user_form.get().attrs() {
+ Ok(attrs) => {
+ if let Ok(mut user) = ::models::User::by_id(id, &conn) {
+ if user.username != attrs.username {
+ user.username = attrs.username.clone();
+ }
+ if user.realname != attrs.realname {
+ user.realname = attrs.realname.clone();
+ }
+ if user.email != attrs.email {
+ user.email = attrs.email.clone();
+ }
+ if !attrs.password.is_empty() && user.password != attrs.password {
+ user.password = attrs.password.clone();
+ }
+ match ::models::User::update(&user, &conn) {
+ Ok(user) => Flash::success(
+ Redirect::to(route.base.path()),
+ format!("User {} updated successfully", user.username)
+ ),
+ Err(msg) => Flash::error(
+ Redirect::to(route.base.path()),
+ format!("User {} was not updated! {}", user.username, msg)
+ )
+
+ }
+ }
+ else {
+ Flash::error(
+ Redirect::to(route.base.path()),
+ "Can't delete users which does not exist"
+ )
+ }
+ },
+ Err(msg) => Flash::error(
+ Redirect::to(route.base.path()),
+ format!("Invalid form data: {}", msg)
+ )
+ }
+}
+
pub fn routes() -> Vec<rocket::Route> {
- routes![index, new, create, delete]
+ routes![index, new, create, edit, update, delete]
}