summary refs log tree commit diff stats
path: root/src/posts.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/posts.rs')
-rw-r--r--src/posts.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/posts.rs b/src/posts.rs
new file mode 100644
index 0000000..28ffac6
--- /dev/null
+++ b/src/posts.rs
@@ -0,0 +1,79 @@
+use crate::db;
+use rusqlite;
+use std::error::Error;
+use users;
+
+type Result<T> = std::result::Result<T, Box<dyn Error>>;
+
+pub fn new(stmt: &mut rusqlite::Statement, title: &str, body: &str) -> Result<()> {
+    let user = users::get_current_username()
+        .unwrap()
+        .into_string()
+        .unwrap();
+
+    stmt.execute_named(&[(":title", &title), (":author", &user), (":body", &body)])?;
+    Ok(())
+}
+
+pub fn update(new_title: &str, new_body: &str, id_num_in: u32, db: &db::Conn) -> Result<()> {
+    let new_title = new_title.trim();
+    let new_body = new_body.trim();
+
+    let title_stmt = format!("UPDATE posts SET title = :title WHERE id = {}", id_num_in);
+    let mut stmt = db.conn.prepare(&title_stmt)?;
+    stmt.execute_named(&[(":title", &new_title)])?;
+    let body_stmt = format!("UPDATE posts SET body = :body WHERE id = {}", id_num_in);
+    let mut stmt = db.conn.prepare(&body_stmt)?;
+
+    stmt.execute_named(&[(":body", &new_body)])?;
+
+    Ok(())
+}
+
+pub fn exec_stmt_no_params(stmt: &mut rusqlite::Statement) -> Result<()> {
+    stmt.execute(rusqlite::NO_PARAMS)?;
+
+    Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    fn post_new() {
+        let db = db::Conn::new();
+        let mut stmt = db
+            .conn
+            .prepare("INSERT INTO posts (title, author, body) VALUES (:title, :author, :body)")
+            .unwrap();
+
+        let title = String::from("TEST TITLE");
+
+        new(&mut stmt, &title, "TEST BODY").unwrap();
+
+        let mut stmt = db
+            .conn
+            .prepare("SELECT * FROM posts WHERE title = :title")
+            .unwrap();
+
+        let out: String = stmt
+            .query_row_named(&[(":title", &title)], |row| row.get::<usize, String>(1))
+            .unwrap();
+
+        assert_eq!("TEST TITLE", &out);
+    }
+
+    #[test]
+    fn post_upd8() {
+        post_new();
+        let db = db::Conn::new();
+        update("NEW TITLE", "TEST BODY", 1, &db).unwrap();
+
+        let mut stmt = db.conn.prepare("SELECT * FROM posts WHERE id = 1").unwrap();
+
+        let out: String = stmt
+            .query_row(rusqlite::NO_PARAMS, |row| row.get::<usize, String>(1))
+            .unwrap();
+        assert_eq!("NEW TITLE", &out);
+    }
+}
i/profani-tty/commit/themes/boothj5?id=8d10dbfc494945d9f06d47466c20d6df32b8bc8e'>8d10dbfc ^
30b5f112 ^





8d10dbfc ^


20fa9632 ^
8d10dbfc ^
20fa9632 ^
30b5f112 ^
71679a31 ^

26840cc3 ^


02a4beb6 ^
2f82f50a ^

1a3dc91e ^

26840cc3 ^
976e5aac ^
976e5aac ^
fbc30231 ^

















cd2458c0 ^

0ae975c2 ^

71a8e391 ^
b21edfaa ^
20fa9632 ^

20fa9632 ^
84f441b9 ^
30b5f112 ^

06afcb59 ^


a952776b ^
06afcb59 ^
503e0ae3 ^
1900402f ^
99fc70bd ^
20fa9632 ^

30b5f112 ^
446027b9 ^
30b5f112 ^

095c129a ^
7bdc46c0 ^
6e4a9de6 ^
216493ef ^
095c129a ^
6e4a9de6 ^
f4759cf3 ^
0a022618 ^
2bda22c3 ^
56751e89 ^
0a022618 ^
216493ef ^
0a022618 ^
216493ef ^




0a022618 ^
216493ef ^
5bccee93 ^
abc2f0de ^
cf80fdc3 ^
97d96746 ^
2bda22c3 ^
519b10d1 ^
f91b21a1 ^
f4759cf3 ^
a00095c8 ^
2bda22c3 ^
27adf031 ^

30b5f112 ^

3892665f ^
f0f3a739 ^
35239ee3 ^

93358d5e ^
fb7001cf ^
72c1c496 ^
bab75cae ^
d3cc5bd7 ^



aa520f4f ^


513a80c9 ^
34aa7a71 ^
aa520f4f ^

d3cc5bd7 ^
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152