diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-01-19 13:16:40 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-01-19 13:16:40 +0100 |
commit | 228376c1089033e1ee643db0e93d12580d5494d1 (patch) | |
tree | df96bff470daddd14cf2944869d5fefd1e65722d /src/controllers | |
parent | a3db0a3cdfaba887bdcec1748d341bd8ebf6ac47 (diff) | |
download | rocket-blog-228376c1089033e1ee643db0e93d12580d5494d1.tar.gz rocket-blog-228376c1089033e1ee643db0e93d12580d5494d1.tar.bz2 rocket-blog-228376c1089033e1ee643db0e93d12580d5494d1.zip |
Implement new/create user logic.
Diffstat (limited to 'src/controllers')
-rw-r--r-- | src/controllers/mod.rs | 1 | ||||
-rw-r--r-- | src/controllers/users_controller.rs | 40 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index b23cd61..401071a 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -1,2 +1,3 @@ pub mod home_controller; pub mod posts_controller; +pub mod users_controller; diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs new file mode 100644 index 0000000..8dcd4c0 --- /dev/null +++ b/src/controllers/users_controller.rs @@ -0,0 +1,40 @@ +use rocket::request::Form; +use rocket::response::{Flash, Redirect}; +use utils; + +#[derive(BartDisplay)] +#[template = "templates/new_user.html"] +pub struct NewUserTemplate { + user: ::models::NewUser +} + +implement_responder_for!(NewUserTemplate); + +impl NewUserTemplate { + pub fn realname(&self) -> String { + self.user.realname.as_ref().unwrap_or(&String::new()).clone() + } + + pub fn email(&self) -> String { + self.user.email.as_ref().unwrap_or(&String::new()).clone() + } +} + +#[get("/new", format = "text/html")] +fn new() -> utils::Page<NewUserTemplate> { + utils::Page { + title: String::from("New user"), + flash: None, + content: NewUserTemplate { + user: Default::default() + } + } +} + +#[post("/create", data="<user>")] +fn create(user: Form<::models::NewUser>, conn: utils::DbConn) -> Flash<Redirect> { + match ::models::User::create(user.get(), conn) { + Ok(_) => Flash::success(Redirect::to("/"), "User successfully created!"), + Err(_) => Flash::error(Redirect::to("/"), "Could not create user!") + } +} |