mirror of
https://github.com/eliasrenman/url-shortener.git
synced 2026-03-16 20:16:06 +01:00
feat: conditional delete
This commit is contained in:
@@ -34,7 +34,7 @@ pub fn upsert_entry(
|
|||||||
.execute(connection)
|
.execute(connection)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_entry(path: &str) -> Result<usize, Error> {
|
pub fn delete_entry(id: &str, path: &str) -> Result<usize, Error> {
|
||||||
let connection = &mut establish_connection();
|
let connection = &mut establish_connection();
|
||||||
delete(urls.filter(url.eq(path))).execute(connection)
|
delete(urls.filter(url.eq(path)).filter(owned_by.eq(id))).execute(connection)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,16 +10,16 @@ pub fn handle_redirect(url: &str) -> Result<Redirect, (Status, &'static str)> {
|
|||||||
Ok(Redirect::to(row.destination_url))
|
Ok(Redirect::to(row.destination_url))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_upsert(username: &str, dto: UpsertUrlDto<'_>) -> (Status, &'static str) {
|
pub fn handle_upsert(id: &str, dto: UpsertUrlDto<'_>) -> (Status, &'static str) {
|
||||||
let row = upsert_entry(username, dto.url, dto.destination_url, dto.ttl);
|
let row = upsert_entry(id, dto.url, dto.destination_url, dto.ttl);
|
||||||
if row.is_err() {
|
if row.is_err() {
|
||||||
return (Status::BadRequest, "Failed to upsert redirect");
|
return (Status::BadRequest, "Failed to upsert redirect");
|
||||||
}
|
}
|
||||||
(Status::Ok, "Successfully upserted redirect")
|
(Status::Ok, "Successfully upserted redirect")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_delete(url: &str) -> (Status, &'static str) {
|
pub fn handle_delete(id: &str, url: &str) -> (Status, &'static str) {
|
||||||
let row = delete_entry(url);
|
let row = delete_entry(id, url);
|
||||||
if row.is_err() {
|
if row.is_err() {
|
||||||
return (Status::BadRequest, "Failed to delete redirect");
|
return (Status::BadRequest, "Failed to delete redirect");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,6 +72,6 @@ fn upsert(user: User, upsert: Json<dto::UpsertUrlDto<'_>>) -> (Status, &'static
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[delete("/<url>")]
|
#[delete("/<url>")]
|
||||||
fn delete(url: &str) -> (Status, &'static str) {
|
fn delete(user: User, url: &str) -> (Status, &'static str) {
|
||||||
handle_delete(url)
|
handle_delete(&user.id.as_str(), url)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user