summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-08-30 19:17:16 -0400
committerBen Morrison <ben@gbmor.dev>2019-08-30 19:18:26 -0400
commitd098a93d910f4bb23e4f0f81a69db83693dd2b78 (patch)
treea044a8a54dceccc0984c251a681e6d92ceb57b30
parentcf2b9750ca87ed4fadee63606d8de6f109e84cb8 (diff)
downloadclinte-d098a93d910f4bb23e4f0f81a69db83693dd2b78.tar.gz
moved some functionality to module, added tests
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs33
-rw-r--r--src/posts.rs79
4 files changed, 97 insertions, 19 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 831c6fa..f56227c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -119,7 +119,7 @@ dependencies = [
 
 [[package]]
 name = "clinte"
-version = "0.3.2"
+version = "0.3.3"
 dependencies = [
  "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index 94d6300..728c3c8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "clinte"
-version = "0.3.2"
+version = "0.3.3"
 authors = ["Ben Morrison <ben@gbmor.dev>"]
 edition = "2018"
 
diff --git a/src/main.rs b/src/main.rs
index b837d47..2c9122f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,6 +6,7 @@ use users;
 
 mod db;
 mod logging;
+mod posts;
 
 fn main() {
     let arg_matches = clap::App::new("clinte")
@@ -124,13 +125,7 @@ fn post(db: &db::Conn) {
         &body
     };
 
-    let user = users::get_current_username()
-        .unwrap()
-        .into_string()
-        .unwrap();
-
-    stmt.execute_named(&[(":title", &title), (":author", &user), (":body", &body)])
-        .unwrap();
+    posts::new(&mut stmt, title, body).unwrap();
 
     println!();
 }
@@ -181,15 +176,7 @@ fn update(db: &db::Conn) {
     io::stdin().read_line(&mut new_body).unwrap();
     println!();
 
-    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).unwrap();
-    stmt.execute_named(&[(":title", &new_title)]).unwrap();
-    let body_stmt = format!("UPDATE posts SET body = :body WHERE id = {}", id_num_in);
-    let mut stmt = db.conn.prepare(&body_stmt).unwrap();
-    stmt.execute_named(&[(":body", &new_body)]).unwrap();
+    posts::update(&new_title, &new_body, id_num_in, &db).unwrap();
 }
 
 fn delete(db: &db::Conn) {
@@ -221,5 +208,17 @@ fn delete(db: &db::Conn) {
         return;
     }
 
-    del_stmt.execute(rusqlite::NO_PARAMS).unwrap();
+    posts::exec_stmt_no_params(&mut del_stmt).unwrap();
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    // Just a "Don't Panic" test
+    #[test]
+    fn display() {
+        let db = db::Conn::new();
+        list_matches(&db);
+    }
 }
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);
+    }
+}
01dd2003f172bdc5afdb8bb8bf34eccf0fa'>2c5ea01d ^
582f3519 ^
b06433bc ^
582f3519 ^

94c5d83e ^
dee6cfa6 ^
e9e4b4ff ^
b0a216f5 ^
76612b05 ^
a082b66a ^
b0a216f5 ^
e9e4b4ff ^





612b8b8b ^
e9e4b4ff ^
5fca2a0b ^
f473be92 ^
5fca2a0b ^
9cf43c17 ^
5fca2a0b ^

c7720fff ^


a6836d5d ^

c7720fff ^
8d21b83c ^


e9e4b4ff ^
25a4162d ^
e9e4b4ff ^
0c2c782d ^
636d9393 ^
b0a216f5 ^
c6afc196 ^

c0d63e78 ^


c6afc196 ^
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