From 5ecd29926dcd8cdfd7859775b2d898ca6b3f278b Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Wed, 13 Mar 2024 18:59:10 -0500 Subject: [PATCH] chg: Added some checks during deletion of a shortlink --- actix/src/database.rs | 6 +++--- actix/src/main.rs | 7 +++++-- actix/src/utils.rs | 8 ++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/actix/src/database.rs b/actix/src/database.rs index 0c903f9..c939c14 100644 --- a/actix/src/database.rs +++ b/actix/src/database.rs @@ -49,9 +49,9 @@ pub fn add_link(shortlink: String, longlink: String, db: &Connection) -> bool { .is_ok() } -pub fn delete_link(shortlink: String, db: &Connection) { - db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink]) - .unwrap(); +pub fn delete_link(shortlink: String, db: &Connection) -> bool { + let out = db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink]); + out.is_ok() && (out.unwrap() > 0) } pub fn open_db(path: String) -> Connection { diff --git a/actix/src/main.rs b/actix/src/main.rs index 3c2caef..1c2419c 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -114,8 +114,11 @@ async fn delete_link( session: Session, ) -> HttpResponse { if auth::validate(session) { - database::delete_link(shortlink.to_string(), &data.db); - HttpResponse::Ok().body("") + if utils::delete_link(shortlink.to_string(), &data.db) { + HttpResponse::Ok().body(format!("Deleted {shortlink}")) + } else { + HttpResponse::NotFound().body("Not found!") + } } else { HttpResponse::Forbidden().body("Wrong password!") } diff --git a/actix/src/utils.rs b/actix/src/utils.rs index 248cd9a..b474fe5 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -45,6 +45,14 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) { } } +pub fn delete_link(shortlink: String, db: &Connection) -> bool { + if validate_link(shortlink.as_str()) { + database::delete_link(shortlink, db) + } else { + false + } +} + fn random_name() -> String { #[rustfmt::skip] static ADJECTIVES: [&str; 108] = ["admiring", "adoring", "affectionate", "agitated", "amazing", "angry", "awesome", "beautiful",