aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2018-01-19 13:16:40 +0100
committerHarald Eilertsen <haraldei@anduin.net>2018-01-19 13:16:40 +0100
commit228376c1089033e1ee643db0e93d12580d5494d1 (patch)
treedf96bff470daddd14cf2944869d5fefd1e65722d /src
parenta3db0a3cdfaba887bdcec1748d341bd8ebf6ac47 (diff)
downloadrocket-blog-228376c1089033e1ee643db0e93d12580d5494d1.tar.gz
rocket-blog-228376c1089033e1ee643db0e93d12580d5494d1.tar.bz2
rocket-blog-228376c1089033e1ee643db0e93d12580d5494d1.zip
Implement new/create user logic.
Diffstat (limited to 'src')
-rw-r--r--src/controllers/mod.rs1
-rw-r--r--src/controllers/users_controller.rs40
-rw-r--r--src/main.rs7
3 files changed, 47 insertions, 1 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!")
+ }
+}
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 {