Add a tag model

This commit is contained in:
Bat 2018-09-05 19:05:53 +01:00
parent e16acf8436
commit 2b7a5bee93
5 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE tags;

View File

@ -0,0 +1,7 @@
-- Your SQL goes here
CREATE TABLE tags (
id SERIAL PRIMARY KEY,
tag TEXT NOT NULL DEFAULT '',
is_hastag BOOLEAN NOT NULL DEFAULT 'f',
post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE NOT NULL
)

View File

@ -119,4 +119,5 @@ pub mod posts;
pub mod reshares;
pub mod safe_string;
pub mod schema;
pub mod tags;
pub mod users;

View File

@ -139,6 +139,15 @@ table! {
}
}
table! {
tags (id) {
id -> Int4,
tag -> Text,
is_hastag -> Bool,
post_id -> Int4,
}
}
table! {
users (id) {
id -> Int4,
@ -178,6 +187,7 @@ joinable!(post_authors -> users (author_id));
joinable!(posts -> blogs (blog_id));
joinable!(reshares -> posts (post_id));
joinable!(reshares -> users (user_id));
joinable!(tags -> posts (post_id));
joinable!(users -> instances (instance_id));
allow_tables_to_appear_in_same_query!(
@ -193,5 +203,6 @@ allow_tables_to_appear_in_same_query!(
post_authors,
posts,
reshares,
tags,
users,
);

23
plume-models/src/tags.rs Normal file
View File

@ -0,0 +1,23 @@
use diesel::{self, PgConnection, ExpressionMethods, RunQueryDsl, QueryDsl};
use schema::tags;
#[derive(Queryable)]
pub struct Tag {
pub id: i32,
pub tag: String,
pub is_hastag: bool,
pub post_id: i32
}
#[derive(Insertable)]
#[table_name = "tags"]
pub struct NewTag {
pub tag: String,
pub is_hastag: bool,
pub post_id: i32
}
impl Tag {
insert!(tags, NewTag);
get!(tags);
}