Plume/src/models/post_authors.rs

39 lines
1.0 KiB
Rust
Raw Normal View History

2018-04-24 11:21:39 +02:00
use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
2018-04-29 22:23:44 +02:00
use models::posts::Post;
use models::users::User;
2018-04-23 16:37:49 +02:00
use schema::post_authors;
2018-04-29 22:23:44 +02:00
#[derive(Queryable, Identifiable, Associations)]
#[belongs_to(Post)]
#[belongs_to(User, foreign_key = "author_id")]
2018-04-23 16:37:49 +02:00
pub struct PostAuthor {
pub id: i32,
pub post_id: i32,
pub author_id: i32
}
#[derive(Insertable)]
#[table_name = "post_authors"]
pub struct NewPostAuthor {
pub post_id: i32,
pub author_id: i32
}
impl PostAuthor {
pub fn insert (conn: &PgConnection, new: NewPostAuthor) -> PostAuthor {
diesel::insert_into(post_authors::table)
.values(new)
.get_result(conn)
.expect("Error saving new blog author")
}
pub fn get(conn: &PgConnection, id: i32) -> Option<PostAuthor> {
post_authors::table.filter(post_authors::id.eq(id))
.limit(1)
.load::<PostAuthor>(conn)
.expect("Error loading blog author by id")
.into_iter().nth(0)
}
}