1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-12-26 07:38:36 -06:00

Ability to delete links from database

This commit is contained in:
Sayantan Santra 2023-04-03 17:58:19 -05:00
parent 98d10cfd5b
commit e4ff2df3f1
3 changed files with 16 additions and 2 deletions

View file

@ -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());
}

View file

@ -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();
}

View file

@ -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<String>) -> impl Responder {
}
}
// Delete a given shortlink
#[delete("/api/del/{shortlink}")]
async fn delete_link(shortlink: web::Path<String>) -> 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))?