Add blog model

This commit is contained in:
Bat 2018-04-23 11:29:27 +01:00
parent 5f4cb6c065
commit cadb33cc1a
6 changed files with 86 additions and 1 deletions

View file

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE blogs;

View file

@ -0,0 +1,10 @@
-- Your SQL goes here
CREATE TABLE blogs (
id SERIAL PRIMARY KEY,
actor_id VARCHAR NOT NULL,
title VARCHAR NOT NULL,
summary TEXT NOT NULL DEFAULT '',
outbox_url VARCHAR NOT NULL,
inbox_url VARCHAR NOT NULL,
instance_id INTEGER REFERENCES instances(id) ON DELETE CASCADE NOT NULL
)

58
src/models/blogs.rs Normal file
View file

@ -0,0 +1,58 @@
use diesel;
use diesel::{QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
use schema::blogs;
#[derive(Queryable, Identifiable)]
pub struct Blog {
pub id: i32,
pub actor_id: String,
pub title: String,
pub summary: String,
pub outbox_url: String,
pub inbox_url: String,
pub instance_id: i32
}
#[derive(Insertable)]
#[table_name = "blogs"]
pub struct NewBlog {
pub actor_id: String,
pub title: String,
pub summary: String,
pub outbox_url: String,
pub inbox_url: String,
pub instance_id: i32
}
impl Blog {
pub fn insert (conn: &PgConnection, new: NewBlog) -> Blog {
diesel::insert_into(blogs::table)
.values(new)
.get_result(conn)
.expect("Error saving new blog")
}
pub fn compute_outbox(blog: String, hostname: String) -> String {
format!("https://{}/~/{}/outbox", hostname, blog)
}
pub fn compute_inbox(blog: String, hostname: String) -> String {
format!("https://{}/~/{}/inbox", hostname, blog)
}
pub fn get(conn: &PgConnection, id: i32) -> Option<Blog> {
blogs::table.filter(blogs::id.eq(id))
.limit(1)
.load::<Blog>(conn)
.expect("Error loading blog by id")
.into_iter().nth(0)
}
pub fn find_by_actor_id(conn: &PgConnection, username: String) -> Option<Blog> {
blogs::table.filter(blogs::actor_id.eq(username))
.limit(1)
.load::<Blog>(conn)
.expect("Error loading blog by email")
.into_iter().nth(0)
}
}

View file

@ -1,2 +1,3 @@
pub mod blogs;
pub mod instance;
pub mod user;

View file

@ -38,7 +38,7 @@ pub struct NewUser {
}
impl User {
fn grant_admin_rights() {}
pub fn grant_admin_rights() {}
pub fn insert (conn: &PgConnection, new: NewUser) -> User {
diesel::insert_into(users::table)

View file

@ -1,3 +1,15 @@
table! {
blogs (id) {
id -> Int4,
actor_id -> Varchar,
title -> Varchar,
summary -> Text,
outbox_url -> Varchar,
inbox_url -> Varchar,
instance_id -> Int4,
}
}
table! {
instances (id) {
id -> Int4,
@ -24,9 +36,11 @@ table! {
}
}
joinable!(blogs -> instances (instance_id));
joinable!(users -> instances (instance_id));
allow_tables_to_appear_in_same_query!(
blogs,
instances,
users,
);