mirror of
https://git.joinplu.me/Plume/Plume.git
synced 2024-11-29 06:51:01 +00:00
Add relation between blogs and authors
This commit is contained in:
parent
4f9df753f5
commit
68db4a82cf
5 changed files with 58 additions and 0 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
DROP TABLE blog_authors;
|
7
migrations/2018-04-23-111655_create_blog_authors/up.sql
Normal file
7
migrations/2018-04-23-111655_create_blog_authors/up.sql
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
-- Your SQL goes here
|
||||||
|
CREATE TABLE blog_authors (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
blog_id INTEGER REFERENCES blogs(id) ON DELETE CASCADE NOT NULL,
|
||||||
|
author_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL,
|
||||||
|
is_owner BOOLEAN NOT NULL DEFAULT 'f'
|
||||||
|
)
|
36
src/models/blog_authors.rs
Normal file
36
src/models/blog_authors.rs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
use diesel;
|
||||||
|
use diesel::{QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
|
||||||
|
use schema::blog_authors;
|
||||||
|
|
||||||
|
#[derive(Queryable, Identifiable)]
|
||||||
|
pub struct BlogAuthor {
|
||||||
|
pub id: i32,
|
||||||
|
pub blog_id: i32,
|
||||||
|
pub author_id: i32,
|
||||||
|
pub is_owner: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Insertable)]
|
||||||
|
#[table_name = "blog_authors"]
|
||||||
|
pub struct NewBlogAuthor {
|
||||||
|
pub blog_id: i32,
|
||||||
|
pub author_id: i32,
|
||||||
|
pub is_owner: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BlogAuthor {
|
||||||
|
pub fn insert (conn: &PgConnection, new: NewBlogAuthor) -> BlogAuthor {
|
||||||
|
diesel::insert_into(blog_authors::table)
|
||||||
|
.values(new)
|
||||||
|
.get_result(conn)
|
||||||
|
.expect("Error saving new blog")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(conn: &PgConnection, id: i32) -> Option<BlogAuthor> {
|
||||||
|
blog_authors::table.filter(blog_authors::id.eq(id))
|
||||||
|
.limit(1)
|
||||||
|
.load::<BlogAuthor>(conn)
|
||||||
|
.expect("Error loading blog by id")
|
||||||
|
.into_iter().nth(0)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
pub mod blog_authors;
|
||||||
pub mod blogs;
|
pub mod blogs;
|
||||||
pub mod instance;
|
pub mod instance;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
|
@ -1,3 +1,12 @@
|
||||||
|
table! {
|
||||||
|
blog_authors (id) {
|
||||||
|
id -> Int4,
|
||||||
|
blog_id -> Int4,
|
||||||
|
author_id -> Int4,
|
||||||
|
is_owner -> Bool,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
table! {
|
table! {
|
||||||
blogs (id) {
|
blogs (id) {
|
||||||
id -> Int4,
|
id -> Int4,
|
||||||
|
@ -36,10 +45,13 @@ table! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
joinable!(blog_authors -> blogs (blog_id));
|
||||||
|
joinable!(blog_authors -> users (author_id));
|
||||||
joinable!(blogs -> instances (instance_id));
|
joinable!(blogs -> instances (instance_id));
|
||||||
joinable!(users -> instances (instance_id));
|
joinable!(users -> instances (instance_id));
|
||||||
|
|
||||||
allow_tables_to_appear_in_same_query!(
|
allow_tables_to_appear_in_same_query!(
|
||||||
|
blog_authors,
|
||||||
blogs,
|
blogs,
|
||||||
instances,
|
instances,
|
||||||
users,
|
users,
|
||||||
|
|
Loading…
Reference in a new issue