diff options
Diffstat (limited to 'src/models')
-rw-r--r-- | src/models/mod.rs | 4 | ||||
-rw-r--r-- | src/models/post.rs | 20 | ||||
-rw-r--r-- | src/models/user.rs | 16 |
3 files changed, 22 insertions, 18 deletions
diff --git a/src/models/mod.rs b/src/models/mod.rs index f6aba77..3c5acf7 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,7 +1,7 @@ pub mod post; -pub use self::post::Post; pub use self::post::NewPost; +pub use self::post::Post; pub mod user; -pub use self::user::User; pub use self::user::NewUser; +pub use self::user::User; diff --git a/src/models/post.rs b/src/models/post.rs index 17ac74a..be44dda 100644 --- a/src/models/post.rs +++ b/src/models/post.rs @@ -1,6 +1,6 @@ -use schema::posts; -use diesel::prelude::*; use diesel; +use diesel::prelude::*; +use schema::posts; use utils; #[derive(AsChangeset, FromForm, Identifiable, Serialize, Queryable)] @@ -12,7 +12,7 @@ pub struct Post { } #[derive(Default, FromForm, Insertable)] -#[table_name="posts"] +#[table_name = "posts"] pub struct NewPost { pub title: String, pub body: String, @@ -21,16 +21,18 @@ pub struct NewPost { impl Post { pub fn get_all(conn: utils::DbConn) -> Vec<Post> { - use ::schema::posts::dsl::*; - posts.filter(published.eq(false)) + use schema::posts::dsl::*; + posts + .filter(published.eq(false)) .limit(5) .load::<Post>(&*conn) .expect("Error loading posts") } fn get_internal(post_id: i32, conn: &utils::DbConn) -> Post { - use ::schema::posts::dsl::*; - posts.find(post_id) + use schema::posts::dsl::*; + posts + .find(post_id) .get_result(&**conn) .expect(&format!("Unable to find post with id={}", post_id)) } @@ -40,7 +42,7 @@ impl Post { } pub fn create(new_post: &NewPost, conn: utils::DbConn) { - use ::schema::posts::dsl::*; + use schema::posts::dsl::*; diesel::insert_into(posts) .values(new_post) .execute(&*conn) @@ -56,7 +58,7 @@ impl Post { } pub fn delete(post_id: i32, conn: utils::DbConn) { - use ::schema::posts::dsl::*; + use schema::posts::dsl::*; diesel::delete(posts.filter(id.eq(post_id))) .execute(&*conn) .expect(&format!("Could not delete post with id {}", post_id)); diff --git a/src/models/user.rs b/src/models/user.rs index 82a359b..37c5333 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -9,7 +9,7 @@ pub struct User { pub username: String, pub realname: Option<String>, pub email: Option<String>, - pub password: String + pub password: String, } impl User { @@ -20,31 +20,33 @@ impl User { pub fn by_id(user_id: i32, conn: &utils::DbConn) -> QueryResult<User> { use schema::users::dsl::*; - users.filter(id.eq(user_id)) + users + .filter(id.eq(user_id)) .load::<User>(&**conn) .map(|ref v| v[0].clone()) } pub fn by_email(user_email: &str, conn: utils::DbConn) -> QueryResult<User> { use schema::users::dsl::*; - users.filter(email.eq(user_email)) + users + .filter(email.eq(user_email)) .get_result::<User>(&*conn) } pub fn create(new_user: &NewUser, conn: utils::DbConn) -> QueryResult<User> { - use ::schema::users::dsl::*; + use schema::users::dsl::*; diesel::insert_into(users) .values(new_user) .get_result(&*conn) } pub fn update(user: &User, conn: &utils::DbConn) -> QueryResult<User> { - use ::schema::users::dsl::*; + use schema::users::dsl::*; user.save_changes(&*conn) } pub fn delete(user_id: i32, conn: utils::DbConn) -> QueryResult<User> { - use ::schema::users::dsl::*; + use schema::users::dsl::*; diesel::delete(users.filter(id.eq(user_id))).get_result::<User>(&*conn) } @@ -58,7 +60,7 @@ impl User { } #[derive(Default, Insertable)] -#[table_name="users"] +#[table_name = "users"] pub struct NewUser { pub username: String, pub realname: Option<String>, |