summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-08-27 23:32:30 -0400
committerBen Morrison <ben@gbmor.dev>2019-08-27 23:32:30 -0400
commit05649a5b2c4a122cfb1644f009825abfc7345d72 (patch)
tree4921b4de4d8cfdce3ebe8eb33b163b40c62dca40 /src
parent99273b69b161ae472f5ee37707d0a50ef5608be9 (diff)
downloadclinte-05649a5b2c4a122cfb1644f009825abfc7345d72.tar.gz
finished posting and display
Diffstat (limited to 'src')
-rw-r--r--src/db.rs36
-rw-r--r--src/main.rs86
2 files changed, 90 insertions, 32 deletions
diff --git a/src/db.rs b/src/db.rs
index 93d73a8..a0cc2cf 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,23 +1,20 @@
 use log::info;
-use rand;
 use rusqlite;
-use std::sync::mpsc;
 use std::time;
 
 const DB_PATH: &str = "/tmp/clinte.db";
 
 #[derive(Debug)]
 pub struct Post {
-    id: u32,
-    title: String,
-    author: String,
-    body: String,
+    pub id: u32,
+    pub title: String,
+    pub author: String,
+    pub body: String,
 }
 
 #[derive(Debug)]
 pub struct Conn {
-    db: rusqlite::Connection,
-    rx: mpsc::Receiver<Cmd>,
+    pub conn: rusqlite::Connection,
 }
 
 #[derive(Debug)]
@@ -42,7 +39,7 @@ impl Conn {
 
         conn.execute(
             "CREATE TABLE IF NOT EXISTS posts (
-            id INTEGER PRIMARY KEY NOT NULL,
+            id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
             title TEXT NOT NULL,
             author TEXT NOT NULL,
             body TEXT NOT NULL
@@ -59,11 +56,8 @@ impl Conn {
         conn
     }
 
-    pub fn new(rx: mpsc::Receiver<Cmd>) -> Self {
-        Conn {
-            db: Conn::init(),
-            rx,
-        }
+    pub fn new() -> Self {
+        Conn { conn: Conn::init() }
     }
 }
 
@@ -80,7 +74,7 @@ impl Cmd {
 
 impl Post {
     pub fn new(title: &str, author: &str, body: &str) -> Self {
-        let id = rand::random::<u32>();
+        let id = 0;
         let title = title.to_string();
         let author = author.to_string();
         let body = body.to_string();
@@ -91,16 +85,4 @@ impl Post {
             body,
         }
     }
-    pub fn id(&self) -> String {
-        format!("{}", self.id)
-    }
-    pub fn title(&self) -> String {
-        self.title.clone()
-    }
-    pub fn author(&self) -> String {
-        self.author.clone()
-    }
-    pub fn body(&self) -> String {
-        self.body.clone()
-    }
 }
diff --git a/src/main.rs b/src/main.rs
index 3f93d92..1b64c84 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,9 @@
 use clap;
 use log::info;
-use std::sync::mpsc;
+use std::io;
+use std::process;
 use std::time;
+use users;
 
 mod db;
 mod logging;
@@ -13,7 +15,7 @@ fn main() {
         .about("Command-line community notices system")
         .subcommand(clap::SubCommand::with_name("list").about("Display notices"))
         .subcommand(clap::SubCommand::with_name("post").about("Post a new notice"))
-        .subcommand(
+        /*.subcommand(
             clap::SubCommand::with_name("update")
                 .about("Update a notice you've posted")
                 .arg(clap::Arg::with_name("id").help("Numeric ID of the post")),
@@ -22,7 +24,7 @@ fn main() {
             clap::SubCommand::with_name("delete")
                 .about("Delete a notice you've posted")
                 .arg(clap::Arg::with_name("id").help("Numeric ID of the post")),
-        )
+        )*/
         .get_matches();
 
     let start = time::Instant::now();
@@ -30,9 +32,83 @@ fn main() {
     info!("clinte starting up!");
     println!("clinte-0.1-dev");
     println!("a community notices system");
+    println!();
 
-    let (_tx, rx) = mpsc::channel::<db::Cmd>();
-    let db = db::Conn::new(rx);
+    let db = db::Conn::new();
 
     info!("Startup completed in {:?}ms", start.elapsed().as_millis());
+
+    if let Some(_) = arg_matches.subcommand_matches("list") {
+        info!("Listing notes...");
+        list_matches(&db);
+        process::exit(0);
+    } else if let Some(_) = arg_matches.subcommand_matches("post") {
+        info!("New post...");
+        post(&db);
+        list_matches(&db);
+        process::exit(0);
+    }
+}
+
+fn list_matches(db: &db::Conn) {
+    let mut stmt = db.conn.prepare("SELECT * FROM posts").unwrap();
+    let out = stmt
+        .query_map(rusqlite::NO_PARAMS, |row| {
+            let id = row.get(0)?;
+            let title = row.get(1)?;
+            let author = row.get(2)?;
+            let body = row.get(3)?;
+            Ok(db::Post {
+                id,
+                title,
+                author,
+                body,
+            })
+        })
+        .unwrap();
+
+    out.for_each(|row| {
+        if let Ok(post) = row {
+            println!(
+                "{}. {} -> by {}\n{}",
+                post.id, post.title, post.author, post.body
+            );
+        }
+    });
+}
+
+fn post(db: &db::Conn) {
+    let mut stmt = db
+        .conn
+        .prepare("INSERT INTO posts (title, author, body) VALUES (:title, :author, :body)")
+        .unwrap();
+
+    println!();
+    println!("Title of the new post: ");
+    let mut title = String::new();
+    io::stdin().read_line(&mut title).unwrap();
+    let title = title.trim();
+    let title = if title.len() > 30 {
+        &title[..30]
+    } else {
+        &title
+    }
+
+    println!();
+    println!("Body of the new post: ");
+    let mut body = String::new();
+    io::stdin().read_line(&mut body).unwrap();
+    let body = body.trim();
+    let body = if body.len() > 500 {
+        &body[..500]
+    } else {
+        &body
+    }
+
+    let user = users::get_current_username()
+        .unwrap()
+        .into_string()
+        .unwrap();
+
+    stmt.execute_named(&[(":title", &title), (":author", &user), (":body", &body)]);
 }
es.subx?h=hlt&id=cf1ddc4170e9d76eea196e4bc34dcf5f9bc66fdd'>cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
8b1da9bb ^
cf1ddc41 ^
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
153
154
155
156
157
158
159
160
161
162
163
164