From 05649a5b2c4a122cfb1644f009825abfc7345d72 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Tue, 27 Aug 2019 23:32:30 -0400 Subject: finished posting and display --- src/main.rs | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 5 deletions(-) (limited to 'src/main.rs') 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::(); - 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)]); } -- cgit 1.4.1-2-gfad0