Skip to content

Commit a904ea9

Browse files
committed
added option to delete link
1 parent a7a37ac commit a904ea9

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/cli.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ enum MainMenuOptions {
88
Status,
99
GetLink,
1010
AddLink,
11+
DeleteLink,
1112
SearchLink,
1213
Other,
1314
Exit,
1415
}
1516

17+
enum DeleteOptions {
18+
DeleteLink,
19+
MainMenu,
20+
Exit,
21+
}
22+
1623
enum GetLinkOptions {
1724
MarkAsComplete,
1825
Skip,
@@ -35,6 +42,7 @@ pub fn show_options(db: &Db) -> Result<(), CustomErrors> {
3542
"Check Status",
3643
"Get Link",
3744
"Add Link",
45+
"Delete Link",
3846
"Search Link",
3947
"Other",
4048
"Exit",
@@ -61,6 +69,7 @@ pub fn show_options(db: &Db) -> Result<(), CustomErrors> {
6169
"Check Status" => MainMenuOptions::Status,
6270
"Get Link" => MainMenuOptions::GetLink,
6371
"Add Link" => MainMenuOptions::AddLink,
72+
"Delete Link" => MainMenuOptions::DeleteLink,
6473
"Search Link" => MainMenuOptions::SearchLink,
6574
"Other" => MainMenuOptions::Other,
6675
"Exit" => MainMenuOptions::Exit,
@@ -71,6 +80,7 @@ pub fn show_options(db: &Db) -> Result<(), CustomErrors> {
7180
MainMenuOptions::Status => get_status(&db)?,
7281
MainMenuOptions::GetLink => get_link_options(&db)?,
7382
MainMenuOptions::AddLink => add_link_options(&db)?,
83+
MainMenuOptions::DeleteLink => delete_link_options(&db)?,
7484
MainMenuOptions::SearchLink => search_link_options(&db)?,
7585
MainMenuOptions::Other => show_other_options(&db)?,
7686
MainMenuOptions::Exit => return Err(CustomErrors::Exit),
@@ -156,6 +166,53 @@ fn add_link_options(db: &Db) -> Result<(), CustomErrors> {
156166
Ok(())
157167
}
158168

169+
fn delete_link_options(db: &Db) -> Result<(), CustomErrors> {
170+
let links = db.get_links()?;
171+
172+
let link = match Select::new("select link to delete", links).prompt() {
173+
Ok(val) => val,
174+
Err(_) => {
175+
return Err(CustomErrors::Others(
176+
"Error: Something went wrong while deleting links".to_owned(),
177+
))
178+
}
179+
};
180+
181+
let options = vec!["Delete Link", "Main Menu", "Exit"];
182+
183+
let choice = match Select::new("select your option", options).prompt() {
184+
Ok(val) => val,
185+
Err(_) => {
186+
return Err(CustomErrors::Others(
187+
"Error: Something went wrong while showing delete options".to_owned(),
188+
))
189+
}
190+
};
191+
192+
let selected_option = match choice {
193+
"Delete Link" => DeleteOptions::DeleteLink,
194+
"Main Menu" => DeleteOptions::MainMenu,
195+
"Exit" => DeleteOptions::Exit,
196+
_ => unreachable!(),
197+
};
198+
199+
loop {
200+
match selected_option {
201+
DeleteOptions::DeleteLink => {
202+
match db.delete_link(link) {
203+
Ok(_) => show_green("Successfully deleted the link"),
204+
Err(e) => return Err(e),
205+
};
206+
break;
207+
}
208+
DeleteOptions::MainMenu => break,
209+
DeleteOptions::Exit => return Err(CustomErrors::Exit),
210+
};
211+
}
212+
213+
Ok(())
214+
}
215+
159216
fn single_link_options(db: &Db, link: &str) -> Result<(), CustomErrors> {
160217
let options = vec![
161218
"Mark As Complete?",

src/database.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ impl Db {
7070
}
7171
}
7272

73+
/// delete link from the db
74+
pub fn delete_link(&self, link: String) -> Result<(), CustomErrors> {
75+
match self
76+
.conn
77+
.execute("DELETE FROM links WHERE link = ?1", [&link])
78+
{
79+
Ok(_) => (),
80+
Err(_) => {
81+
return Err(CustomErrors::Others(
82+
"Error: Something went wrong while deleting the selected link".to_owned(),
83+
))
84+
}
85+
}
86+
Ok(())
87+
}
88+
7389
pub fn get_links(&self) -> Result<Vec<String>, CustomErrors> {
7490
let mut stmt = match self.conn.prepare("SELECT link FROM links") {
7591
Ok(val) => val,

0 commit comments

Comments
 (0)