From 228376c1089033e1ee643db0e93d12580d5494d1 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 19 Jan 2018 13:16:40 +0100 Subject: Implement new/create user logic. --- src/controllers/mod.rs | 1 + src/controllers/users_controller.rs | 40 +++++++++++++++++++++++++++++++++++++ src/main.rs | 7 ++++++- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/controllers/users_controller.rs (limited to 'src') 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 { + utils::Page { + title: String::from("New user"), + flash: None, + content: NewUserTemplate { + user: Default::default() + } + } +} + +#[post("/create", data="")] +fn create(user: Form<::models::NewUser>, conn: utils::DbConn) -> Flash { + match ::models::User::create(user.get(), conn) { + Ok(_) => Flash::success(Redirect::to("/"), "User successfully created!"), + Err(_) => Flash::error(Redirect::to("/"), "Could not create user!") + } +} diff --git a/src/main.rs b/src/main.rs index 424dbde..af3063a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod models; mod schema; mod controllers; use controllers::home_controller; -use controllers::posts_controller; +use controllers::{posts_controller, users_controller}; fn main() { if let Ok(dburl) = dotenv::var("DATABASE_URL") { @@ -35,6 +35,11 @@ fn main() { posts_controller::edit, posts_controller::update, posts_controller::delete]) + .mount("/users", + routes![ + users_controller::new, + users_controller::create, + ]) .launch(); } else { -- cgit v1.2.3