diff --git a/assets/custom.css b/assets/custom.css index 4bb6083..c6ca2d6 100644 --- a/assets/custom.css +++ b/assets/custom.css @@ -6,7 +6,12 @@ @apply bg-background text-foreground; } } - +.error-message { + color: red; +} +.error input { + box-shadow: 0 0 3px #cc0000; +} .animate-in { animation: animateIn 0.3s ease 0.15s both; } diff --git a/src/routes/link_routes.rs b/src/routes/link_routes.rs index fb216cf..86a1aae 100644 --- a/src/routes/link_routes.rs +++ b/src/routes/link_routes.rs @@ -5,6 +5,7 @@ use crate::{ use axum::{routing::post, Extension, Form, Router}; use rand; use rand::Rng; +use serde::Deserialize; use sqlx::{Error, PgPool, Row}; pub fn get_routes() -> Router { @@ -15,18 +16,31 @@ async fn add_link( ctx: Extension, Form(payload): Form, ) -> Result { + if payload.original_url.is_empty() { + return Err("
Please enter an URL
".to_string()); + } + if payload.path.len() > 0 && payload.path.len() < 5 { + return Err("
Path must be at least 5 characters long
".to_string()); + } + if payload.original_url.len() > 200 { + return Err("
URL must be less than 200 characters long
".to_string()); + } + if !payload.original_url.starts_with("http://") || !payload.original_url.starts_with("https://") + { + return Err("
URL must start with http:// or https://
".to_string()); + } // Store the links in the database let link = match insert_link(&payload, &ctx.db).await { Ok(link) => link, Err(err) => { eprintln!("Error inserting link: {}", err); - return Err("
Something went wrong
".to_string()); + return Err("
Something went wrong. Try a different path
".to_string()); } }; //return html Ok(format!( - "", + "", link.short_url, link.short_url )) } @@ -38,14 +52,18 @@ async fn insert_link(payload: &LinkRequest, pool: &PgPool) -> Result("id")?) + .execute(pool) + .await?; let link = LinkResponse { - short_url: insertion.try_get("short_url")?, + short_url: insertion.try_get::("short_url")?, }; Ok(link) }