markdown.rs 3.42 KB
use std::sync::Arc;
use crate::schema::*;
use crate::error::Error;
use crate::Pool;
use diesel::prelude::*;
use diesel::{
    dsl::{delete, insert_into, update},
    RunQueryDsl
};
use serde::{Deserialize, Serialize};


#[derive(Debug, Serialize, Deserialize, Queryable, Identifiable)]
pub struct Markdown {
    pub id: i32,
    pub name: String,
    pub content: String,
    pub number_of_versions: i32,
    pub date_created: String,
    pub date_updated: String,
}

#[derive(Debug, Insertable)]
#[table_name = "markdowns"]
pub struct MarkdownNew<'a> {
    pub name: &'a str,
    pub content: &'a str,
    pub number_of_versions: i32,
    pub date_created: &'a str,
    pub date_updated: &'a str,
}

#[derive(Debug, Serialize, Deserialize, AsChangeset)]
#[table_name="markdowns"]
pub struct MarkdownJson {
    pub name: String,
    pub content: String,
    pub number_of_versions: i32,
}

pub(crate) enum Action {
    Created(Markdown),
    Found(Markdown),
}


pub(crate) fn create_markdown( pool: Arc<Pool>
                             , item: MarkdownJson ) -> Result<Action, Error> {
    use crate::schema::markdowns::dsl::*;
    let db_connection = pool.get()?;

    match markdowns . filter(name.eq(&item.name))
                    . first::<Markdown>(&db_connection)
    {
        Ok(result) => Ok(Action::Found(result)),
        Err(_)     => {
            let now = chrono::Local::now().naive_local();
            let new_markdown = MarkdownNew {
                name:               &item.name,
                content:            &item.content,
                number_of_versions: item.number_of_versions,
                date_created:       &format!("{}", now),
                date_updated:       &format!("{}", now),
            };

            Ok(Action::Created(db_connection.transaction(|| {
                insert_into(markdowns) . values(&new_markdown)
                                       . execute(&db_connection)?;

                markdowns . order(id.desc())
                          . first::<Markdown>(&db_connection)
            })?))
        }
    }
}

pub(crate) fn get_markdowns(pool: Arc<Pool>) -> Result<Vec<Markdown>, Error>
{
    use crate::schema::markdowns::dsl::*;
    let db_connection = pool.get()?;
    Ok(markdowns.load::<Markdown>(&db_connection)?)
}

pub(crate) fn get_markdown( pool:  Arc<Pool>
                          , ident: i32 ) -> Result<Markdown, Error>
{
    use crate::schema::markdowns::dsl::*;
    let db_connection = pool.get()?;
    Ok(markdowns.find(ident).first::<Markdown>(&db_connection)?)
}

pub(crate) fn delete_markdown( pool: Arc<Pool>
                             , ident: i32 ) -> Result<usize, Error>
{
    use crate::schema::markdowns::dsl::*;
    let db_connection = pool.get()?;
    Ok(delete(markdowns.find(ident)).execute(&db_connection)?)
}

pub(crate) fn update_markdown( pool: Arc<Pool>
                             , ident: i32
                             , item: MarkdownJson ) -> Result<Markdown, Error>
{
    use crate::schema::markdowns::dsl::*;
    let db_connection = pool.get()?;
    let mut markdown = markdowns.find(ident).first::<Markdown>(&db_connection)?;
    let now = chrono::Local::now().naive_local();

    update(markdowns.find(ident)).set(&item).execute(&db_connection)?;
    markdown.name               = item.name;
    markdown.content            = item.content;
    markdown.number_of_versions = item.number_of_versions;
    markdown.date_updated       = format!("{}", now);

    Ok(markdown)
}