Create and display comments

This commit is contained in:
Bat
2018-05-10 10:44:57 +01:00
parent 424517fab0
commit a3d73cb2c4
7 changed files with 100 additions and 9 deletions
+11
View File
@@ -1,6 +1,7 @@
use chrono;
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods};
use models::users::User;
use schema::comments;
#[derive(Queryable, Identifiable, Serialize)]
@@ -43,4 +44,14 @@ impl Comment {
.expect("Error loading comment by id")
.into_iter().nth(0)
}
pub fn for_post(conn: &PgConnection, post_id: i32) -> Vec<Comment> {
comments::table.filter(comments::post_id.eq(post_id))
.load::<Comment>(conn)
.expect("Error loading comment by post id")
}
pub fn get_author(&self, conn: &PgConnection) -> User {
User::get(conn, self.author_id).unwrap()
}
}