chg: Added some checks during deletion of a shortlink

This commit is contained in:
SinTan1729 2024-03-13 18:59:10 -05:00
parent aa097ad982
commit 5ecd29926d
No known key found for this signature in database
GPG Key ID: EB3E68BFBA25C85F
3 changed files with 16 additions and 5 deletions

View File

@ -49,9 +49,9 @@ pub fn add_link(shortlink: String, longlink: String, db: &Connection) -> bool {
.is_ok() .is_ok()
} }
pub fn delete_link(shortlink: String, db: &Connection) { pub fn delete_link(shortlink: String, db: &Connection) -> bool {
db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink]) let out = db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink]);
.unwrap(); out.is_ok() && (out.unwrap() > 0)
} }
pub fn open_db(path: String) -> Connection { pub fn open_db(path: String) -> Connection {

View File

@ -114,8 +114,11 @@ async fn delete_link(
session: Session, session: Session,
) -> HttpResponse { ) -> HttpResponse {
if auth::validate(session) { if auth::validate(session) {
database::delete_link(shortlink.to_string(), &data.db); if utils::delete_link(shortlink.to_string(), &data.db) {
HttpResponse::Ok().body("") HttpResponse::Ok().body(format!("Deleted {shortlink}"))
} else {
HttpResponse::NotFound().body("Not found!")
}
} else { } else {
HttpResponse::Forbidden().body("Wrong password!") HttpResponse::Forbidden().body("Wrong password!")
} }

View File

@ -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 { fn random_name() -> String {
#[rustfmt::skip] #[rustfmt::skip]
static ADJECTIVES: [&str; 108] = ["admiring", "adoring", "affectionate", "agitated", "amazing", "angry", "awesome", "beautiful", static ADJECTIVES: [&str; 108] = ["admiring", "adoring", "affectionate", "agitated", "amazing", "angry", "awesome", "beautiful",