Add a model for posts

This commit is contained in:
Bat
2018-04-23 14:41:43 +01:00
parent 0e24b3cdb7
commit 268607da0e
6 changed files with 69 additions and 1 deletions
+1
View File
@@ -1,4 +1,5 @@
pub mod blog_authors;
pub mod blogs;
pub mod instance;
pub mod post;
pub mod user;
+42
View File
@@ -0,0 +1,42 @@
use diesel;
use diesel::{PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods};
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)
}
}
-1
View File
@@ -6,7 +6,6 @@ use std::collections::HashMap;
use db_conn::DbConn;
use models::user::*;
use models::instance::Instance;
use activity_pub::Actor;
#[get("/me")]
fn me(user: User) -> String {
+14
View File
@@ -30,6 +30,18 @@ table! {
}
}
table! {
posts (id) {
id -> Int4,
blog_id -> Int4,
slug -> Varchar,
title -> Varchar,
content -> Text,
published -> Bool,
license -> Varchar,
}
}
table! {
users (id) {
id -> Int4,
@@ -48,11 +60,13 @@ table! {
joinable!(blog_authors -> blogs (blog_id));
joinable!(blog_authors -> users (author_id));
joinable!(blogs -> instances (instance_id));
joinable!(posts -> blogs (blog_id));
joinable!(users -> instances (instance_id));
allow_tables_to_appear_in_same_query!(
blog_authors,
blogs,
instances,
posts,
users,
);