Ability to delete links from database
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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))?
|
||||
|
||||
Reference in New Issue
Block a user