diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/controllers/home_controller.rs | 7 | ||||
-rw-r--r-- | src/controllers/login_controller.rs | 5 | ||||
-rw-r--r-- | src/controllers/posts_controller.rs | 11 | ||||
-rw-r--r-- | src/controllers/users_controller.rs | 11 | ||||
-rw-r--r-- | src/models/post.rs | 22 | ||||
-rw-r--r-- | src/models/user.rs | 17 | ||||
-rw-r--r-- | src/utils/mod.rs | 2 |
7 files changed, 39 insertions, 36 deletions
diff --git a/src/controllers/home_controller.rs b/src/controllers/home_controller.rs index ba2ea0c..cc0fa8a 100644 --- a/src/controllers/home_controller.rs +++ b/src/controllers/home_controller.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] use models::Post; use posts_controller::ShowPostTemplate; use rocket::{request::FlashMessage, response::NamedFile, Route}; @@ -17,9 +18,9 @@ implement_responder_for!(IndexTemplate); fn index(flash: Option<FlashMessage>, conn: DbConn) -> Page<IndexTemplate> { Page { title: String::from("Bloggen"), - flash: flash, + flash, content: IndexTemplate { - posts: Post::get_all(conn) + posts: Post::get_all(&conn) .into_iter() .map(|p| ShowPostTemplate { post: p }) .collect(), @@ -29,7 +30,7 @@ fn index(flash: Option<FlashMessage>, conn: DbConn) -> Page<IndexTemplate> { #[get("/", format = "application/json")] fn index_json(conn: DbConn) -> Json<Vec<Post>> { - Json(Post::get_all(conn)) + Json(Post::get_all(&conn)) } // diff --git a/src/controllers/login_controller.rs b/src/controllers/login_controller.rs index 6ee5de1..8800c59 100644 --- a/src/controllers/login_controller.rs +++ b/src/controllers/login_controller.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] use models::User; use rocket::{ request::{FlashMessage, Form}, @@ -16,7 +17,7 @@ implement_responder_for!(LoginTemplate); pub fn new(flash: Option<FlashMessage>) -> Page<LoginTemplate> { Page { title: String::from("Log in"), - flash: flash, + flash, content: LoginTemplate {}, } } @@ -30,7 +31,7 @@ pub struct LoginForm { #[post("/create", data = "<login>")] pub fn create(login: Form<LoginForm>, conn: DbConn) -> Flash<Redirect> { let login = login.get(); - if let Ok(user) = 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("/"), diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 99ff651..781f9f5 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] use comrak::{markdown_to_html, ComrakOptions}; use models::{NewPost, Post}; use rocket::{ @@ -28,7 +29,7 @@ fn new(_conn: DbConn) -> Page<NewPostTemplate> { #[post("/create", data = "<post>")] fn create(post: Form<NewPost>, conn: DbConn) -> Flash<Redirect> { - Post::create(post.get(), conn); + Post::create(post.get(), &conn); Flash::success(Redirect::to("/"), "Post successfully created!") } @@ -48,7 +49,7 @@ impl ShowPostTemplate { #[get("/<id>", format = "text/html")] fn show(id: i32, conn: DbConn) -> Page<ShowPostTemplate> { - let p = Post::get(id, conn); + let p = Post::get(id, &conn); Page { title: p.title.clone(), flash: None, @@ -66,7 +67,7 @@ implement_responder_for!(EditPostTemplate); #[get("/<id>/edit", format = "text/html")] fn edit(id: i32, conn: DbConn) -> Page<EditPostTemplate> { - let p = Post::get(id, conn); + let p = Post::get(id, &conn); Page { title: String::from("Edit post"), flash: None, @@ -76,13 +77,13 @@ fn edit(id: i32, conn: DbConn) -> Page<EditPostTemplate> { #[post("/update", data = "<post>")] fn update(post: Form<Post>, conn: DbConn) -> Flash<Redirect> { - Post::update(post.get(), conn); + Post::update(post.get(), &conn); Flash::success(Redirect::to("/"), "Post updated successfully!") } #[get("/<id>/delete", format = "text/html")] fn delete(id: i32, conn: DbConn) -> Flash<Redirect> { - Post::delete(id, conn); + Post::delete(id, &conn); Flash::success(Redirect::to("/"), "Post deleted!") } diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs index 4713705..19d5970 100644 --- a/src/controllers/users_controller.rs +++ b/src/controllers/users_controller.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] use models::{NewUser, User}; use rocket::{ request::{FlashMessage, Form}, @@ -18,9 +19,9 @@ implement_responder_for!(UsersTemplate); fn index(flash: Option<FlashMessage>, conn: DbConn) -> Page<UsersTemplate> { Page { title: String::from("Users"), - flash: flash, + flash, content: UsersTemplate { - users: User::all(conn).unwrap(), + users: User::all(&conn).unwrap(), }, } } @@ -46,7 +47,7 @@ fn edit(id: i32, conn: DbConn) -> Page<EditUserTemplate> { #[get("/<id>/delete")] fn delete(id: i32, route: &Route, conn: DbConn) -> Flash<Redirect> { - match User::delete(id, conn) { + match User::delete(id, &conn) { Ok(user) => Flash::success( Redirect::to(route.base.path()), format!("User {} was successfully deleted.", user.username), @@ -70,7 +71,7 @@ implement_responder_for!(NewUserTemplate); fn new(flash: Option<FlashMessage>) -> Page<NewUserTemplate> { Page { title: String::from("New user"), - flash: flash, + flash, content: NewUserTemplate { user: Default::default(), }, @@ -120,7 +121,7 @@ impl RegisterUserForm { #[post("/create", data = "<user>")] fn create(user: Form<RegisterUserForm>, route: &Route, conn: DbConn) -> Flash<Redirect> { match user.get().new_user() { - Ok(new_user) => match User::create(&new_user, conn) { + Ok(new_user) => match User::create(&new_user, &conn) { Ok(_) => Flash::success( Redirect::to(route.base.path()), "User successfully created!", diff --git a/src/models/post.rs b/src/models/post.rs index bde8e54..c057775 100644 --- a/src/models/post.rs +++ b/src/models/post.rs @@ -20,12 +20,12 @@ pub struct NewPost { } impl Post { - pub fn get_all(conn: DbConn) -> Vec<Post> { + pub fn get_all(conn: &DbConn) -> Vec<Post> { use schema::posts::dsl::*; posts .filter(published.eq(false)) .limit(5) - .load::<Post>(&*conn) + .load::<Post>(&**conn) .expect("Error loading posts") } @@ -34,33 +34,33 @@ impl Post { posts .find(post_id) .get_result(&**conn) - .expect(&format!("Unable to find post with id={}", post_id)) + .unwrap_or_else(|_| panic!("Unable to find post with id={}", post_id)) } - pub fn get(post_id: i32, conn: DbConn) -> Post { + pub fn get(post_id: i32, conn: &DbConn) -> Post { Post::get_internal(post_id, &conn) } - pub fn create(new_post: &NewPost, conn: DbConn) { + pub fn create(new_post: &NewPost, conn: &DbConn) { use schema::posts::dsl::*; diesel::insert_into(posts) .values(new_post) - .execute(&*conn) + .execute(&**conn) .expect("Error saving post."); } - pub fn update(updated_post: &Post, conn: DbConn) { + pub fn update(updated_post: &Post, conn: &DbConn) { let p = Post::get_internal(updated_post.id, &conn); diesel::update(&p) .set(updated_post) - .execute(&*conn) + .execute(&**conn) .expect("Error saving post."); } - pub fn delete(post_id: i32, conn: DbConn) { + pub fn delete(post_id: i32, conn: &DbConn) { use schema::posts::dsl::*; diesel::delete(posts.filter(id.eq(post_id))) - .execute(&*conn) - .expect(&format!("Could not delete post with id {}", post_id)); + .execute(&**conn) + .unwrap_or_else(|_| panic!("Could not delete post with id {}", post_id)); } } diff --git a/src/models/user.rs b/src/models/user.rs index 08c54d7..991cb66 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -13,9 +13,9 @@ pub struct User { } impl User { - pub fn all(conn: DbConn) -> QueryResult<Vec<User>> { + pub fn all(conn: &DbConn) -> QueryResult<Vec<User>> { use schema::users::dsl::*; - users.load::<User>(&*conn) + users.load::<User>(&**conn) } pub fn by_id(user_id: i32, conn: &DbConn) -> QueryResult<User> { @@ -26,28 +26,27 @@ impl User { .map(|ref v| v[0].clone()) } - pub fn by_email(user_email: &str, conn: DbConn) -> QueryResult<User> { + pub fn by_email(user_email: &str, conn: &DbConn) -> QueryResult<User> { use schema::users::dsl::*; users .filter(email.eq(user_email)) - .get_result::<User>(&*conn) + .get_result::<User>(&**conn) } - pub fn create(new_user: &NewUser, conn: DbConn) -> QueryResult<User> { + pub fn create(new_user: &NewUser, conn: &DbConn) -> QueryResult<User> { use schema::users::dsl::*; diesel::insert_into(users) .values(new_user) - .get_result(&*conn) + .get_result(&**conn) } pub fn update(user: &User, conn: &DbConn) -> QueryResult<User> { - use schema::users::dsl::*; user.save_changes(&*conn) } - pub fn delete(user_id: i32, conn: DbConn) -> QueryResult<User> { + pub fn delete(user_id: i32, conn: &DbConn) -> QueryResult<User> { use schema::users::dsl::*; - diesel::delete(users.filter(id.eq(user_id))).get_result::<User>(&*conn) + diesel::delete(users.filter(id.eq(user_id))).get_result::<User>(&**conn) } pub fn realname(&self) -> String { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index ad6cff1..23b551e 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -7,7 +7,7 @@ use std::fmt::Display; type Pool = r2d2::Pool<ConnectionManager<PgConnection>>; /// Initializes a database pool. -pub fn init_db_pool<'a>(dburl: &'a str) -> Pool { +pub fn init_db_pool(dburl: &str) -> Pool { let manager = ConnectionManager::<PgConnection>::new(dburl); r2d2::Pool::builder().build(manager).expect("db pool") } |