From 268607da0e64ee9a7dd42596522776929d0f9d6c Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 23 Apr 2018 14:41:43 +0100 Subject: [PATCH] Add a model for posts --- .../2018-04-23-132822_create_posts/down.sql | 2 + .../2018-04-23-132822_create_posts/up.sql | 10 +++++ src/models/mod.rs | 1 + src/models/post.rs | 42 +++++++++++++++++++ src/routes/user.rs | 1 - src/schema.rs | 14 +++++++ 6 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 migrations/2018-04-23-132822_create_posts/down.sql create mode 100644 migrations/2018-04-23-132822_create_posts/up.sql create mode 100644 src/models/post.rs diff --git a/migrations/2018-04-23-132822_create_posts/down.sql b/migrations/2018-04-23-132822_create_posts/down.sql new file mode 100644 index 0000000..56ed16e --- /dev/null +++ b/migrations/2018-04-23-132822_create_posts/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE posts; diff --git a/migrations/2018-04-23-132822_create_posts/up.sql b/migrations/2018-04-23-132822_create_posts/up.sql new file mode 100644 index 0000000..5a606cd --- /dev/null +++ b/migrations/2018-04-23-132822_create_posts/up.sql @@ -0,0 +1,10 @@ +-- Your SQL goes here +CREATE TABLE posts ( + id SERIAL PRIMARY KEY, + blog_id INTEGER REFERENCES blogs(id) ON DELETE CASCADE NOT NULL, + slug VARCHAR NOT NULL, + title VARCHAR NOT NULL, + content TEXT NOT NULL DEFAULT '', + published BOOLEAN NOT NULL DEFAULT 'f', + license VARCHAR NOT NULL DEFAULT 'CC-0' +) diff --git a/src/models/mod.rs b/src/models/mod.rs index 0639787..30b33e5 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,4 +1,5 @@ pub mod blog_authors; pub mod blogs; pub mod instance; +pub mod post; pub mod user; diff --git a/src/models/post.rs b/src/models/post.rs new file mode 100644 index 0000000..378c005 --- /dev/null +++ b/src/models/post.rs @@ -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 { + posts::table.filter(posts::id.eq(id)) + .limit(1) + .load::(conn) + .expect("Error loading post by id") + .into_iter().nth(0) + } +} diff --git a/src/routes/user.rs b/src/routes/user.rs index 1cc3a09..c875029 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -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 { diff --git a/src/schema.rs b/src/schema.rs index 06b777c..46f91ec 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -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, );