mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-11-22 01:06:46 +00:00
chg: Use Option instead of returning empty String
This commit is contained in:
parent
5e4db14ea2
commit
5d8dd6fb63
@ -1,20 +1,13 @@
|
||||
use rusqlite::Connection;
|
||||
|
||||
pub fn find_url(shortlink: &str, db: &Connection) -> String {
|
||||
pub fn find_url(shortlink: &str, db: &Connection) -> Option<String> {
|
||||
let mut statement = db
|
||||
.prepare_cached("SELECT long_url FROM urls WHERE short_url = ?1")
|
||||
.unwrap();
|
||||
|
||||
let links = statement
|
||||
.query_map([shortlink], |row| row.get("long_url"))
|
||||
.unwrap();
|
||||
|
||||
let mut longlink = String::new();
|
||||
for link in links {
|
||||
longlink = link.unwrap();
|
||||
}
|
||||
|
||||
longlink
|
||||
statement
|
||||
.query_row([shortlink], |row| row.get("long_url"))
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub fn getall(db: &Connection) -> Vec<String> {
|
||||
|
@ -78,10 +78,7 @@ async fn error404() -> impl Responder {
|
||||
#[get("/{shortlink}")]
|
||||
async fn link_handler(shortlink: web::Path<String>, data: web::Data<AppState>) -> impl Responder {
|
||||
let shortlink_str = shortlink.to_string();
|
||||
let longlink = utils::get_longurl(shortlink_str, &data.db);
|
||||
if longlink.is_empty() {
|
||||
Redirect::to("/err/404")
|
||||
} else {
|
||||
if let Some(longlink) = utils::get_longurl(shortlink_str, &data.db) {
|
||||
let redirect_method = env::var("redirect_method").unwrap_or(String::from("PERMANENT"));
|
||||
database::add_hit(shortlink.as_str(), &data.db);
|
||||
if redirect_method == "TEMPORARY" {
|
||||
@ -90,6 +87,8 @@ async fn link_handler(shortlink: web::Path<String>, data: web::Data<AppState>) -
|
||||
// Defaults to permanent redirection
|
||||
Redirect::to(longlink).permanent()
|
||||
}
|
||||
} else {
|
||||
Redirect::to("/err/404")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,11 +3,11 @@ use rand::seq::SliceRandom;
|
||||
use regex::Regex;
|
||||
use rusqlite::Connection;
|
||||
|
||||
pub fn get_longurl(shortlink: String, db: &Connection) -> String {
|
||||
pub fn get_longurl(shortlink: String, db: &Connection) -> Option<String> {
|
||||
if validate_link(&shortlink) {
|
||||
database::find_url(shortlink.as_str(), db)
|
||||
} else {
|
||||
String::new()
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) {
|
||||
shortlink = random_name();
|
||||
}
|
||||
|
||||
if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_empty() {
|
||||
if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_none() {
|
||||
(
|
||||
database::add_link(shortlink.clone(), longlink, db),
|
||||
shortlink,
|
||||
|
Loading…
Reference in New Issue
Block a user