diff options
Diffstat (limited to 'src/models')
-rw-r--r-- | src/models/post.rs | 22 | ||||
-rw-r--r-- | src/models/user.rs | 17 |
2 files changed, 19 insertions, 20 deletions
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 { |