Plume/src/models/blog_authors.rs

37 lines
947 B
Rust
Raw Normal View History

2018-04-24 11:21:39 +02:00
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
2018-04-23 13:27:27 +02:00
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)
2018-04-23 13:28:03 +02:00
.expect("Error saving new blog author")
2018-04-23 13:27:27 +02:00
}
pub fn get(conn: &PgConnection, id: i32) -> Option<BlogAuthor> {
blog_authors::table.filter(blog_authors::id.eq(id))
.limit(1)
.load::<BlogAuthor>(conn)
2018-04-23 13:28:03 +02:00
.expect("Error loading blog author by id")
2018-04-23 13:27:27 +02:00
.into_iter().nth(0)
}
}