about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-08-30 16:17:10 -0400
committerBen Morrison <ben@gbmor.dev>2019-08-30 16:17:17 -0400
commitebc4efa5d0cc8bf552d528baa5b4e68956aea764 (patch)
tree7dd1cb6e6a74e5241ca4d15020d82d81ebfb788f
parentf3c780970b05a1e78152920d4dfc5e154e82e212 (diff)
downloadclinte-ebc4efa5d0cc8bf552d528baa5b4e68956aea764.tar.gz
enabled deletion of posts v0.3.0
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs38
3 files changed, 38 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index c613889..07b835b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -119,7 +119,7 @@ dependencies = [
 
 [[package]]
 name = "clinte"
-version = "0.2.0"
+version = "0.3.0"
 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 81b163b..0b9779f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "clinte"
-version = "0.2.0"
+version = "0.3.0"
 authors = ["Ben Morrison <ben@gbmor.dev>"]
 edition = "2018"
 
diff --git a/src/main.rs b/src/main.rs
index ff6e8fd..ff1783a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,11 +18,11 @@ fn main() {
                 .about("Update a notice you've posted")
                 .arg(clap::Arg::with_name("id").help("Numeric ID of the post")),
         )
-        /*.subcommand(
+        .subcommand(
             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();
@@ -42,6 +42,9 @@ fn main() {
     } else if arg_matches.subcommand_matches("update").is_some() {
         info!("Updating post ...");
         update(&db);
+    } else if arg_matches.subcommand_matches("delete").is_some() {
+        info!("Deleting post");
+        delete(&db);
     }
 
     list_matches(&db);
@@ -176,3 +179,34 @@ fn update(db: &db::Conn) {
     let mut stmt = db.conn.prepare(&body_stmt).unwrap();
     stmt.execute_named(&[(":body", &new_body)]).unwrap();
 }
+
+fn delete(db: &db::Conn) {
+    let cur_user = users::get_current_username()
+        .unwrap()
+        .into_string()
+        .unwrap();
+
+    println!();
+    println!("ID of the post to delete?");
+    let mut id_num_in = String::new();
+    io::stdin().read_line(&mut id_num_in).unwrap();
+    let id_num_in: u32 = id_num_in.trim().parse().unwrap();
+
+    let del_stmt = format!("DELETE FROM posts WHERE id = {}", id_num_in);
+    let get_stmt = format!("SELECT * FROM posts WHERE id = {}", id_num_in);
+
+    let mut get_stmt = db.conn.prepare(&get_stmt).unwrap();
+    let mut del_stmt = db.conn.prepare(&del_stmt).unwrap();
+
+    let user_in_post: String = get_stmt
+        .query_row(rusqlite::NO_PARAMS, |row| row.get(2))
+        .unwrap();
+
+    if cur_user != user_in_post {
+        println!("Users don't match. Can't delete!");
+        println!();
+        return;
+    }
+
+    del_stmt.execute(rusqlite::NO_PARAMS).unwrap();
+}