Add a Mention model
parent
24ef3d00d1
commit
e074af57ff
@ -0,0 +1,2 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
DROP TABLE mentions;
|
@ -0,0 +1,7 @@
|
||||
-- Your SQL goes here
|
||||
CREATE TABLE mentions (
|
||||
id SERIAL PRIMARY KEY,
|
||||
mentioned_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL,
|
||||
post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE,
|
||||
comment_id INTEGER REFERENCES comments(id) ON DELETE CASCADE
|
||||
)
|
@ -0,0 +1,37 @@
|
||||
use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
|
||||
|
||||
use models::{
|
||||
comments::Comment,
|
||||
posts::Post
|
||||
};
|
||||
use schema::mentions;
|
||||
|
||||
#[derive(Queryable, Identifiable)]
|
||||
pub struct Mention {
|
||||
pub id: i32,
|
||||
pub mentioned_id: i32,
|
||||
pub post_id: Option<i32>,
|
||||
pub comment_id: Option<i32>
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "mentions"]
|
||||
pub struct NewMention {
|
||||
pub mentioned_id: i32,
|
||||
pub post_id: Option<i32>,
|
||||
pub comment_id: Option<i32>
|
||||
}
|
||||
|
||||
impl Mention {
|
||||
insert!(mentions, NewMention);
|
||||
get!(mentions);
|
||||
find_by!(mentions, find_for_user, mentioned_id as i32);
|
||||
|
||||
pub fn get_post(&self, conn: &PgConnection) -> Option<Post> {
|
||||
self.post_id.and_then(|id| Post::get(conn, id))
|
||||
}
|
||||
|
||||
pub fn get_comment(&self, conn: &PgConnection) -> Option<Comment> {
|
||||
self.post_id.and_then(|id| Comment::get(conn, id))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue