2017-08-20 22:59:16 +03:00
|
|
|
use std;
|
|
|
|
|
2017-08-20 23:17:16 +03:00
|
|
|
use chrono;
|
2017-08-20 22:59:16 +03:00
|
|
|
use diesel::sqlite::SqliteConnection;
|
2017-08-21 00:44:52 +03:00
|
|
|
use diesel::prelude::*;
|
2017-08-20 22:59:16 +03:00
|
|
|
|
2017-08-20 23:17:16 +03:00
|
|
|
use models;
|
|
|
|
|
2017-08-20 22:59:16 +03:00
|
|
|
pub struct State {
|
|
|
|
db_connection: SqliteConnection
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
pub fn new(db_connection: SqliteConnection) -> State {
|
|
|
|
State { db_connection }
|
|
|
|
}
|
|
|
|
|
2017-08-21 00:44:52 +03:00
|
|
|
pub fn get_article_revision_by_slug(&self, slug: &str) -> Result<Option<models::ArticleRevision>, Box<std::error::Error>> {
|
|
|
|
Ok(Some(models::ArticleRevision {
|
|
|
|
article_id: 0,
|
2017-08-20 23:17:16 +03:00
|
|
|
revision: 0,
|
|
|
|
created: chrono::Local::now().naive_local(),
|
|
|
|
title: slug.to_owned(),
|
|
|
|
body: "Look at me!".to_owned(),
|
|
|
|
}))
|
2017-08-20 22:59:16 +03:00
|
|
|
}
|
2017-08-21 00:44:52 +03:00
|
|
|
|
|
|
|
pub fn get_article_revision_by_id(&self, article_id: i32) -> Result<Option<models::ArticleRevision>, Box<std::error::Error>> {
|
|
|
|
use schema::article_revisions;
|
|
|
|
|
|
|
|
Ok(article_revisions::table
|
|
|
|
.filter(article_revisions::article_id.eq(article_id))
|
|
|
|
.order(article_revisions::revision.desc())
|
|
|
|
.limit(1)
|
|
|
|
.load::<models::ArticleRevision>(&self.db_connection)?
|
|
|
|
.pop())
|
|
|
|
}
|
2017-08-20 22:59:16 +03:00
|
|
|
}
|