feat: basic creation and deletion of urls

This commit is contained in:
Elias Renman
2025-03-24 10:16:37 +01:00
parent e789cf5a2c
commit 1f65d20d75
27 changed files with 446 additions and 26 deletions

View File

@@ -1,7 +1,7 @@
use rocket::{http::Status, response::Redirect};
use crate::{
db::url::{delete_entry, get_entry, upsert_entry},
db::url::{delete_entry, get_entry, list, upsert_entry},
dto::UpsertUrlDto,
};
@@ -10,18 +10,23 @@ pub fn handle_redirect(url: &str) -> Result<Redirect, (Status, &'static str)> {
Ok(Redirect::to(row.destination_url))
}
pub fn handle_upsert(id: &str, dto: UpsertUrlDto<'_>) -> (Status, &'static str) {
let row = upsert_entry(id, dto.url, dto.destination_url, dto.ttl);
pub fn handle_upsert(owned_by: &str, url: &str, dto: UpsertUrlDto<'_>) -> (Status, &'static str) {
let row = upsert_entry(owned_by, url, dto.destination_url, dto.ttl);
if row.is_err() {
return (Status::BadRequest, "Failed to upsert redirect");
}
(Status::Ok, "Successfully upserted redirect")
}
pub fn handle_delete(id: &str, url: &str) -> (Status, &'static str) {
let row = delete_entry(id, url);
pub fn handle_delete(owned_by: &str, url: &str) -> (Status, &'static str) {
let row = delete_entry(owned_by, url);
if row.is_err() {
return (Status::BadRequest, "Failed to delete redirect");
}
(Status::Ok, "Successfully deleted redirect")
}
pub fn handle_list(owned_by: &str) -> String {
let urls = list(owned_by);
serde_json::to_string(&urls).unwrap()
}