markdown.rs
3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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)
}