1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-12-26 23:58:35 -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(); e.preventDefault();
if (confirm("Do you want to delete the entry " + shortUrl + "?")) { if (confirm("Do you want to delete the entry " + shortUrl + "?")) {
document.getElementById("alertBox")?.remove(); document.getElementById("alertBox")?.remove();
fetch(`/api/${shortUrl}`, { fetch(`/api/del/${shortUrl}`, {
method: "DELETE" method: "DELETE"
}).then(_ => refreshData()); }).then(_ => refreshData());
} }

View file

@ -56,3 +56,9 @@ pub fn add_link(shortlink: String, longlink: String) -> bool {
Err(_) => false, 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_files::{Files, NamedFile};
use actix_web::{ use actix_web::{
get, post, delete, get, post,
web::{self, Redirect}, web::{self, Redirect},
App, HttpResponse, HttpServer, Responder, 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] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
HttpServer::new(|| { HttpServer::new(|| {
@ -65,6 +72,7 @@ async fn main() -> std::io::Result<()> {
.service(getall) .service(getall)
.service(siteurl) .service(siteurl)
.service(add_link) .service(add_link)
.service(delete_link)
.service(Files::new("/", "./resources/").index_file("index.html")) .service(Files::new("/", "./resources/").index_file("index.html"))
}) })
.bind(("0.0.0.0", 2000))? .bind(("0.0.0.0", 2000))?