From 0d96cbefe189dce252b94e55445de35f850a492f Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 9 May 2018 21:35:02 +0100 Subject: [PATCH] Comment model --- .../down.sql | 2 + .../2018-05-09-192013_create_comments/up.sql | 12 +++++ src/models/comments.rs | 46 +++++++++++++++++++ src/models/mod.rs | 1 + src/schema.rs | 17 +++++++ 5 files changed, 78 insertions(+) create mode 100644 migrations/2018-05-09-192013_create_comments/down.sql create mode 100644 migrations/2018-05-09-192013_create_comments/up.sql create mode 100644 src/models/comments.rs diff --git a/migrations/2018-05-09-192013_create_comments/down.sql b/migrations/2018-05-09-192013_create_comments/down.sql new file mode 100644 index 00000000..d0841ffb --- /dev/null +++ b/migrations/2018-05-09-192013_create_comments/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE comments; diff --git a/migrations/2018-05-09-192013_create_comments/up.sql b/migrations/2018-05-09-192013_create_comments/up.sql new file mode 100644 index 00000000..66ab301f --- /dev/null +++ b/migrations/2018-05-09-192013_create_comments/up.sql @@ -0,0 +1,12 @@ +-- Your SQL goes here +CREATE TABLE comments ( + id SERIAL PRIMARY KEY, + content TEXT NOT NULL DEFAULT '', + in_response_to_id INTEGER REFERENCES comments(id), + post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE NOT NULL, + author_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL, + creation_date TIMESTAMP NOT NULL DEFAULT now(), + ap_url VARCHAR, + sensitive BOOLEAN NOT NULL DEFAULT 'f', + spoiler_text TEXT NOT NULL DEFAULT '' +) diff --git a/src/models/comments.rs b/src/models/comments.rs new file mode 100644 index 00000000..89c100e8 --- /dev/null +++ b/src/models/comments.rs @@ -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, + pub post_id: i32, + pub author_id: i32, + pub creation_date: chrono::NaiveDateTime, + pub ap_url: Option, + pub sensitive: bool, + pub spoiler_text: String +} + +#[derive(Insertable)] +#[table_name = "comments"] +pub struct NewComment { + pub content: String, + pub in_response_to_id: Option, + pub post_id: i32, + pub author_id: i32, + pub ap_url: Option, + 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 { + comments::table.filter(comments::id.eq(id)) + .limit(1) + .load::(conn) + .expect("Error loading comment by id") + .into_iter().nth(0) + } +} diff --git a/src/models/mod.rs b/src/models/mod.rs index 265f3dc0..00df02aa 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,5 +1,6 @@ pub mod blog_authors; pub mod blogs; +pub mod comments; pub mod follows; pub mod instance; pub mod post_authors; diff --git a/src/schema.rs b/src/schema.rs index f44dfdb3..a10c3628 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -23,6 +23,20 @@ table! { } } +table! { + comments (id) { + id -> Int4, + content -> Text, + in_response_to_id -> Nullable, + post_id -> Int4, + author_id -> Int4, + creation_date -> Timestamp, + ap_url -> Nullable, + 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,