Comment model

This commit is contained in:
Bat
2018-05-09 21:35:02 +01:00
parent 292f4d6b27
commit 0d96cbefe1
5 changed files with 78 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
use chrono;
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods};
use schema::comments;
#[derive(Queryable, Identifiable, Serialize)]
pub struct Comment {
pub id: i32,
pub content: String,
pub in_response_to_id: Option<i32>,
pub post_id: i32,
pub author_id: i32,
pub creation_date: chrono::NaiveDateTime,
pub ap_url: Option<String>,
pub sensitive: bool,
pub spoiler_text: String
}
#[derive(Insertable)]
#[table_name = "comments"]
pub struct NewComment {
pub content: String,
pub in_response_to_id: Option<i32>,
pub post_id: i32,
pub author_id: i32,
pub ap_url: Option<String>,
pub sensitive: bool,
pub spoiler_text: String
}
impl Comment {
pub fn insert (conn: &PgConnection, new: NewComment) -> Comment {
diesel::insert_into(comments::table)
.values(new)
.get_result(conn)
.expect("Error saving new comment")
}
pub fn get(conn: &PgConnection, id: i32) -> Option<Comment> {
comments::table.filter(comments::id.eq(id))
.limit(1)
.load::<Comment>(conn)
.expect("Error loading comment by id")
.into_iter().nth(0)
}
}
+1
View File
@@ -1,5 +1,6 @@
pub mod blog_authors;
pub mod blogs;
pub mod comments;
pub mod follows;
pub mod instance;
pub mod post_authors;
+17
View File
@@ -23,6 +23,20 @@ table! {
}
}
table! {
comments (id) {
id -> Int4,
content -> Text,
in_response_to_id -> Nullable<Int4>,
post_id -> Int4,
author_id -> Int4,
creation_date -> Timestamp,
ap_url -> Nullable<Varchar>,
sensitive -> Bool,
spoiler_text -> Text,
}
}
table! {
follows (id) {
id -> Int4,
@@ -85,6 +99,8 @@ table! {
joinable!(blog_authors -> blogs (blog_id));
joinable!(blog_authors -> users (author_id));
joinable!(blogs -> instances (instance_id));
joinable!(comments -> posts (post_id));
joinable!(comments -> users (author_id));
joinable!(post_authors -> posts (post_id));
joinable!(post_authors -> users (author_id));
joinable!(posts -> blogs (blog_id));
@@ -93,6 +109,7 @@ joinable!(users -> instances (instance_id));
allow_tables_to_appear_in_same_query!(
blog_authors,
blogs,
comments,
follows,
instances,
post_authors,