From e4ff2df3f1fa6cf4ba00ffcefd26db556e33b3ba Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 3 Apr 2023 17:58:19 -0500 Subject: [PATCH] Ability to delete links from database --- actix/resources/js/main.js | 2 +- actix/src/database.rs | 6 ++++++ actix/src/main.rs | 10 +++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/actix/resources/js/main.js b/actix/resources/js/main.js index 48fdfa8..7f29ec5 100644 --- a/actix/resources/js/main.js +++ b/actix/resources/js/main.js @@ -108,7 +108,7 @@ const deleteButton = (shortUrl) => { e.preventDefault(); if (confirm("Do you want to delete the entry " + shortUrl + "?")) { document.getElementById("alertBox")?.remove(); - fetch(`/api/${shortUrl}`, { + fetch(`/api/del/${shortUrl}`, { method: "DELETE" }).then(_ => refreshData()); } diff --git a/actix/src/database.rs b/actix/src/database.rs index c14b5ca..6c411a7 100644 --- a/actix/src/database.rs +++ b/actix/src/database.rs @@ -56,3 +56,9 @@ pub fn add_link(shortlink: String, longlink: String) -> bool { Err(_) => false, } } + +pub fn delete_link(shortlink: String) -> () { + let db = Connection::open("./urls.sqlite").expect("Unable to open database!"); + db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink]) + .unwrap(); +} diff --git a/actix/src/main.rs b/actix/src/main.rs index 0e03943..96929a1 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -2,7 +2,7 @@ use std::env; use actix_files::{Files, NamedFile}; use actix_web::{ - get, post, + delete, get, post, web::{self, Redirect}, App, HttpResponse, HttpServer, Responder, }; @@ -56,6 +56,13 @@ async fn link_handler(shortlink: web::Path) -> impl Responder { } } +// Delete a given shortlink +#[delete("/api/del/{shortlink}")] +async fn delete_link(shortlink: web::Path) -> HttpResponse { + database::delete_link(shortlink.to_string()); + HttpResponse::Ok().body("") +} + #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| { @@ -65,6 +72,7 @@ async fn main() -> std::io::Result<()> { .service(getall) .service(siteurl) .service(add_link) + .service(delete_link) .service(Files::new("/", "./resources/").index_file("index.html")) }) .bind(("0.0.0.0", 2000))?