Plume/src/models/posts.rs

59 lines
1.4 KiB
Rust
Raw Normal View History

2018-04-24 09:21:39 +00:00
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods};
2018-04-29 20:23:44 +00:00
use serde_json;
2018-04-24 09:21:39 +00:00
2018-04-29 20:23:44 +00:00
use activity_pub::object::Object;
2018-04-23 13:41:43 +00:00
use schema::posts;
#[derive(Queryable, Identifiable)]
pub struct Post {
pub id: i32,
pub blog_id: i32,
pub slug: String,
pub title: String,
pub content: String,
pub published: bool,
pub license: String
}
#[derive(Insertable)]
#[table_name = "posts"]
pub struct NewPost {
pub blog_id: i32,
pub slug: String,
pub title: String,
pub content: String,
pub published: bool,
pub license: String
}
impl Post {
pub fn insert (conn: &PgConnection, new: NewPost) -> Post {
diesel::insert_into(posts::table)
.values(new)
.get_result(conn)
.expect("Error saving new post")
}
pub fn get(conn: &PgConnection, id: i32) -> Option<Post> {
posts::table.filter(posts::id.eq(id))
.limit(1)
.load::<Post>(conn)
.expect("Error loading post by id")
.into_iter().nth(0)
}
2018-04-23 14:25:39 +00:00
pub fn find_by_slug(conn: &PgConnection, slug: String) -> Option<Post> {
posts::table.filter(posts::slug.eq(slug))
.limit(1)
.load::<Post>(conn)
.expect("Error loading post by slug")
.into_iter().nth(0)
}
2018-04-23 13:41:43 +00:00
}
2018-04-29 20:23:44 +00:00
impl Object for Post {
fn serialize(&self) -> serde_json::Value {
json!({})
}
}