diff options
-rw-r--r-- | src/controllers/users_controller.rs | 79 | ||||
-rw-r--r-- | src/models/user.rs | 9 | ||||
-rw-r--r-- | templates/edit_user.html | 30 | ||||
-rw-r--r-- | templates/users.html | 5 |
4 files changed, 119 insertions, 4 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] } diff --git a/src/models/user.rs b/src/models/user.rs index 9587fd3..82a359b 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -18,10 +18,10 @@ impl User { users.load::<User>(&*conn) } - pub fn by_id(user_id: i32, conn: utils::DbConn) -> QueryResult<User> { + pub fn by_id(user_id: i32, conn: &utils::DbConn) -> QueryResult<User> { use schema::users::dsl::*; users.filter(id.eq(user_id)) - .load::<User>(&*conn) + .load::<User>(&**conn) .map(|ref v| v[0].clone()) } @@ -38,6 +38,11 @@ impl User { .get_result(&*conn) } + pub fn update(user: &User, conn: &utils::DbConn) -> QueryResult<User> { + use ::schema::users::dsl::*; + user.save_changes(&*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) diff --git a/templates/edit_user.html b/templates/edit_user.html new file mode 100644 index 0000000..e1f379c --- /dev/null +++ b/templates/edit_user.html @@ -0,0 +1,30 @@ +<form id="edit_user" name="user" method="post" action="/users/{{ user.id }}/update"> + <div class="field"> + <label>Username:</label> + <input type="text" name="username" value="{{ user.username }}"> + </div> + + <div class="field"> + <label>Real Name:</label> + <input type="text" name="realname" value="{{ user.realname() }}"> + </div> + + <div class="field"> + <label>Email:</label> + <input type="text" name="email" value="{{ user.email() }}"> + </div> + + <div class="field"> + <label>Password:</label> + <input type="text" name="password" value=""> + </div> + + <div class="field"> + <label>Confirm password:</label> + <input type="text" name="password_confirm" value=""> + </div> + + <div class="actions"> + <input type="submit" value="Save changes"> + </div> +</form> diff --git a/templates/users.html b/templates/users.html index 4656e88..3ed4aa6 100644 --- a/templates/users.html +++ b/templates/users.html @@ -12,7 +12,10 @@ <td>{{ .username }}</td> <td>{{ .realname() }}</td> <td>{{ .email() }}</td> - <td><a href="/users/{{ .id }}/delete" id="delete_user">Delete</a></td> + <td> + <a href="/users/{{ .id }}/delete" id="delete_user">Delete</a> + <a href="/users/{{ .id }}/edit" id="edit_user_{{ .id }}">Edit</a> + </td> </tr> {{/ users }} </table> |