diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-08-01 13:50:01 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-08-01 19:40:40 +0200 |
commit | 7633b869d0ca0772f73c687023e299d7ce7e4b52 (patch) | |
tree | a6680ecd5951421861f8d03ca7404867fcb5efbf /src | |
parent | e569a688d9678e14f048003b15d076d1d57a3b55 (diff) | |
download | rocket-blog-7633b869d0ca0772f73c687023e299d7ce7e4b52.tar.gz rocket-blog-7633b869d0ca0772f73c687023e299d7ce7e4b52.tar.bz2 rocket-blog-7633b869d0ca0772f73c687023e299d7ce7e4b52.zip |
Make imports explicit.
Diffstat (limited to 'src')
-rw-r--r-- | src/controllers/home_controller.rs | 28 | ||||
-rw-r--r-- | src/controllers/login_controller.rs | 25 | ||||
-rw-r--r-- | src/controllers/posts_controller.rs | 53 | ||||
-rw-r--r-- | src/controllers/users_controller.rs | 63 |
4 files changed, 100 insertions, 69 deletions
diff --git a/src/controllers/home_controller.rs b/src/controllers/home_controller.rs index 38db52a..6cfa736 100644 --- a/src/controllers/home_controller.rs +++ b/src/controllers/home_controller.rs @@ -1,5 +1,13 @@ -use ::{models, utils}; -use rocket; +use models::Post; +use utils::{ + DbConn, + Page +}; +use rocket::{ + response::NamedFile, + request::FlashMessage, + Route +}; use rocket_contrib::Json; use std::path::PathBuf; use posts_controller::ShowPostTemplate; @@ -13,12 +21,12 @@ struct IndexTemplate { implement_responder_for!(IndexTemplate); #[get("/", format = "text/html")] -fn index(flash: Option<rocket::request::FlashMessage>, conn: utils::DbConn) -> utils::Page<IndexTemplate> { - utils::Page { +fn index(flash: Option<FlashMessage>, conn: DbConn) -> Page<IndexTemplate> { + Page { title: String::from("Bloggen"), flash: flash, content: IndexTemplate { - posts: models::Post::get_all(conn).into_iter() + posts: Post::get_all(conn).into_iter() .map(|p| ShowPostTemplate { post: p }) .collect() } @@ -26,19 +34,19 @@ fn index(flash: Option<rocket::request::FlashMessage>, conn: utils::DbConn) -> u } #[get("/", format = "application/json")] -fn index_json(conn: utils::DbConn) -> Json<Vec<models::Post>> { - Json(models::Post::get_all(conn)) +fn index_json(conn: DbConn) -> Json<Vec<Post>> { + Json(Post::get_all(conn)) } // // Serve files from the public directory if no routes matches. // #[get("/<file..>", rank = 99)] -fn public_file(file: PathBuf) -> Option<rocket::response::NamedFile> { - rocket::response::NamedFile::open(PathBuf::from("public/").join(file)).ok() +fn public_file(file: PathBuf) -> Option<NamedFile> { + NamedFile::open(PathBuf::from("public/").join(file)).ok() } -pub fn routes() -> Vec<rocket::Route> { +pub fn routes() -> Vec<Route> { routes![ index, index_json, diff --git a/src/controllers/login_controller.rs b/src/controllers/login_controller.rs index 564deff..090d299 100644 --- a/src/controllers/login_controller.rs +++ b/src/controllers/login_controller.rs @@ -1,8 +1,13 @@ -use rocket; -use rocket::request::Form; -use rocket::response::{Flash, Redirect}; -use models; -use utils; +use rocket::{ + request::{FlashMessage, Form}, + response::{Flash, Redirect}, + Route +}; +use models::User; +use utils::{ + DbConn, + Page +}; #[derive(BartDisplay)] #[template = "templates/login.html"] @@ -11,8 +16,8 @@ pub struct LoginTemplate; implement_responder_for!(LoginTemplate); #[get("/", format="text/html")] -pub fn new(flash: Option<rocket::request::FlashMessage>) -> utils::Page<LoginTemplate> { - utils::Page { +pub fn new(flash: Option<FlashMessage>) -> Page<LoginTemplate> { + Page { title: String::from("Log in"), flash: flash, content: LoginTemplate{} @@ -26,9 +31,9 @@ pub struct LoginForm { } #[post("/create", data="<login>")] -pub fn create(login: Form<LoginForm>, conn: utils::DbConn) -> Flash<Redirect> { +pub fn create(login: Form<LoginForm>, conn: DbConn) -> Flash<Redirect> { let login = login.get(); - if let Ok(user) = models::User::by_email(&login.email, conn) { + if let Ok(user) = User::by_email(&login.email, conn) { if user.password == login.password { return Flash::success(Redirect::to("/"), format!("{} logged in successfully", user.realname.or(Some(user.username)).unwrap())); } @@ -37,6 +42,6 @@ pub fn create(login: Form<LoginForm>, conn: utils::DbConn) -> Flash<Redirect> { Flash::error(Redirect::to("/login"), "Invalid email or passoword!") } -pub fn routes() -> Vec<rocket::Route> { +pub fn routes() -> Vec<Route> { routes![new, create] } diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index f10f312..7cc3849 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -1,20 +1,29 @@ use comrak::{markdown_to_html, ComrakOptions}; -use rocket; -use rocket::request::Form; -use rocket::response::{Flash, Redirect}; -use utils; +use rocket::{ + response::{Flash, Redirect}, + request::Form, + Route +}; +use models::{ + NewPost, + Post +}; +use utils::{ + DbConn, + Page +}; #[derive(BartDisplay)] #[template = "templates/new_post.html"] pub struct NewPostTemplate { - post: ::models::NewPost + post: NewPost } implement_responder_for!(NewPostTemplate); #[get("/new", format = "text/html")] -fn new(_conn: utils::DbConn) -> utils::Page<NewPostTemplate> { - utils::Page { +fn new(_conn: DbConn) -> Page<NewPostTemplate> { + Page { title: String::from("New post"), flash: None, content: NewPostTemplate { @@ -24,15 +33,15 @@ fn new(_conn: utils::DbConn) -> utils::Page<NewPostTemplate> { } #[post("/create", data="<post>")] -fn create(post: Form<::models::NewPost>, conn: utils::DbConn) -> Flash<Redirect> { - ::models::Post::create(post.get(), conn); +fn create(post: Form<NewPost>, conn: DbConn) -> Flash<Redirect> { + Post::create(post.get(), conn); Flash::success(Redirect::to("/"), "Post successfully created!") } #[derive(BartDisplay, Serialize)] #[template = "templates/show_post.html"] pub struct ShowPostTemplate { - pub post: ::models::Post + pub post: Post } implement_responder_for!(ShowPostTemplate); @@ -44,9 +53,9 @@ impl ShowPostTemplate { } #[get("/<id>", format = "text/html")] -fn show(id: i32, conn: utils::DbConn) -> utils::Page<ShowPostTemplate> { - let p = ::models::Post::get(id, conn); - utils::Page { +fn show(id: i32, conn: DbConn) -> Page<ShowPostTemplate> { + let p = Post::get(id, conn); + Page { title: p.title.clone(), flash: None, content: ShowPostTemplate { @@ -58,15 +67,15 @@ fn show(id: i32, conn: utils::DbConn) -> utils::Page<ShowPostTemplate> { #[derive(BartDisplay)] #[template = "templates/edit_post.html"] pub struct EditPostTemplate { - post: ::models::Post + post: Post } implement_responder_for!(EditPostTemplate); #[get("/<id>/edit", format = "text/html")] -fn edit(id: i32, conn: utils::DbConn) -> utils::Page<EditPostTemplate> { - let p = ::models::Post::get(id, conn); - utils::Page { +fn edit(id: i32, conn: DbConn) -> Page<EditPostTemplate> { + let p = Post::get(id, conn); + Page { title: String::from("Edit post"), flash: None, content: EditPostTemplate { post: p } @@ -74,18 +83,18 @@ fn edit(id: i32, conn: utils::DbConn) -> utils::Page<EditPostTemplate> { } #[post("/update", data="<post>")] -fn update(post: Form<::models::Post>, conn: utils::DbConn) -> Flash<Redirect> { - ::models::Post::update(post.get(), conn); +fn update(post: Form<Post>, conn: DbConn) -> Flash<Redirect> { + Post::update(post.get(), conn); Flash::success(Redirect::to("/"), "Post updated successfully!") } #[get("/<id>/delete", format = "text/html")] -fn delete(id: i32, conn: utils::DbConn) -> Flash<Redirect> { - ::models::Post::delete(id, conn); +fn delete(id: i32, conn: DbConn) -> Flash<Redirect> { + Post::delete(id, conn); Flash::success(Redirect::to("/"), "Post deleted!") } -pub fn routes() -> Vec<rocket::Route> { +pub fn routes() -> Vec<Route> { routes![ new, create, diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs index 91cfc5c..0dee68a 100644 --- a/src/controllers/users_controller.rs +++ b/src/controllers/users_controller.rs @@ -1,23 +1,32 @@ -use rocket; -use rocket::request::Form; -use rocket::response::{Flash, Redirect}; -use utils; +use rocket::{ + response::{Flash, Redirect}, + request::{FlashMessage, Form}, + Route +}; +use models::{ + NewUser, + User +}; +use utils::{ + DbConn, + Page +}; #[derive(BartDisplay)] #[template = "templates/users.html"] pub struct UsersTemplate { - users: Vec<::models::User> + users: Vec<User> } implement_responder_for!(UsersTemplate); #[get("/", format = "text/html")] -fn index(flash: Option<rocket::request::FlashMessage>, conn: utils::DbConn) -> utils::Page<UsersTemplate> { - utils::Page { +fn index(flash: Option<FlashMessage>, conn: DbConn) -> Page<UsersTemplate> { + Page { title: String::from("Users"), flash: flash, content: UsersTemplate { - users: ::models::User::all(conn).unwrap() + users: User::all(conn).unwrap() } } } @@ -25,25 +34,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 + user: User } implement_responder_for!(EditUserTemplate); #[get("/<id>/edit")] -fn edit(id: i32, conn: utils::DbConn) -> utils::Page<EditUserTemplate> { - utils::Page { +fn edit(id: i32, conn: DbConn) -> Page<EditUserTemplate> { + Page { title: String::from("Edit users"), flash: None, content: EditUserTemplate { - user: ::models::User::by_id(id, &conn).unwrap() + user: 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) { +fn delete(id: i32, route: &Route, conn: DbConn) -> Flash<Redirect> { + match User::delete(id, conn) { Ok(user) => Flash::success( Redirect::to(route.base.path()), format!("User {} was successfully deleted.", user.username) @@ -58,14 +67,14 @@ fn delete(id: i32, route: &rocket::Route, conn: utils::DbConn) -> Flash<Redirect #[derive(BartDisplay)] #[template = "templates/new_user.html"] pub struct NewUserTemplate { - user: ::models::NewUser + user: NewUser } implement_responder_for!(NewUserTemplate); #[get("/new", format = "text/html")] -fn new(flash: Option<rocket::request::FlashMessage>) -> utils::Page<NewUserTemplate> { - utils::Page { +fn new(flash: Option<FlashMessage>) -> Page<NewUserTemplate> { + Page { title: String::from("New user"), flash: flash, content: NewUserTemplate { @@ -84,12 +93,12 @@ struct RegisterUserForm { } impl RegisterUserForm { - fn new_user(&self) -> Result<::models::NewUser, &'static str> { + fn new_user(&self) -> Result<NewUser, &'static str> { if self.password != self.password_confirm { Err("Passwords don't match") } else { - Ok(::models::NewUser { + Ok(NewUser { username: self.username.clone(), realname: self.realname.clone(), email: self.email.clone(), @@ -98,12 +107,12 @@ impl RegisterUserForm { } } - fn attrs(&self) -> Result<::models::User, &'static str> { + fn attrs(&self) -> Result<User, &'static str> { if self.password != self.password_confirm { Err("Passwords don't match") } else { - Ok(::models::User { + Ok(User { id: Default::default(), username: self.username.clone(), realname: self.realname.clone(), @@ -115,10 +124,10 @@ impl RegisterUserForm { } #[post("/create", data="<user>")] -fn create(user: Form<RegisterUserForm>, route: &rocket::Route, conn: utils::DbConn) -> Flash<Redirect> { +fn create(user: Form<RegisterUserForm>, route: &Route, conn: DbConn) -> Flash<Redirect> { match user.get().new_user() { Ok(new_user) => { - match ::models::User::create(&new_user, conn) { + match User::create(&new_user, conn) { Ok(_) => Flash::success(Redirect::to(route.base.path()), "User successfully created!"), Err(_) => Flash::error(Redirect::to("/"), "Could not create user!") } @@ -128,10 +137,10 @@ 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> { +fn update(id: i32, user_form: Form<RegisterUserForm>, route: &Route, conn: DbConn) -> Flash<Redirect> { match user_form.get().attrs() { Ok(attrs) => { - if let Ok(mut user) = ::models::User::by_id(id, &conn) { + if let Ok(mut user) = User::by_id(id, &conn) { if user.username != attrs.username { user.username = attrs.username.clone(); } @@ -144,7 +153,7 @@ fn update(id: i32, user_form: Form<RegisterUserForm>, route: &rocket::Route, con if !attrs.password.is_empty() && user.password != attrs.password { user.password = attrs.password.clone(); } - match ::models::User::update(&user, &conn) { + match User::update(&user, &conn) { Ok(user) => Flash::success( Redirect::to(route.base.path()), format!("User {} updated successfully", user.username) @@ -170,6 +179,6 @@ fn update(id: i32, user_form: Form<RegisterUserForm>, route: &rocket::Route, con } } -pub fn routes() -> Vec<rocket::Route> { +pub fn routes() -> Vec<Route> { routes![index, new, create, edit, update, delete] } |