From ca7dc5870b5581ba5d5d2c097745fff5b7a072d5 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Fri, 30 Aug 2019 13:41:16 -0400 Subject: added functionality to update posts --- src/main.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index dd532a4..ff6e8fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,12 +13,12 @@ fn main() { .author("Ben Morrison (gbmor)") .about("Command-line community notices system") .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")), ) - .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")), @@ -28,7 +28,7 @@ fn main() { let start = time::Instant::now(); logging::init(); info!("clinte starting up!"); - println!("clinte-0.1-dev"); + println!("clinte v{}", clap::crate_version!()); println!("a community notices system"); println!(); @@ -39,6 +39,9 @@ fn main() { if arg_matches.subcommand_matches("post").is_some() { info!("New post..."); post(&db); + } else if arg_matches.subcommand_matches("update").is_some() { + info!("Updating post ..."); + update(&db); } list_matches(&db); @@ -116,3 +119,60 @@ fn post(db: &db::Conn) { println!(); } + +fn update(db: &db::Conn) { + let cur_user = users::get_current_username() + .unwrap() + .into_string() + .unwrap(); + + println!(); + println!("ID number of your post to edit?"); + 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 mut get_stmt = db + .conn + .prepare("SELECT * FROM posts WHERE id = :id") + .unwrap(); + + let row = get_stmt + .query_row_named(&[(":id", &id_num_in)], |row| { + let title: String = row.get(1).unwrap(); + let author = row.get(2).unwrap(); + let body = row.get(3).unwrap(); + Ok(vec![title, author, body]) + }) + .unwrap(); + + if cur_user != row[1] { + println!(); + println!("Username mismatch - can't update post!"); + return; + } + + let mut new_title = String::new(); + let mut new_body = String::new(); + + println!("Updating post {}", id_num_in); + println!(); + println!("Title: {}\n\nBody: {}", row[0], row[2]); + println!(); + println!("Enter new title:"); + io::stdin().read_line(&mut new_title).unwrap(); + println!(); + println!("Enter new body:"); + 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(); +} -- cgit 1.4.1-2-gfad0 a id='n26' href='#n26'>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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216