mirror of
https://github.com/SinTan1729/chhoto-url
synced 2025-02-05 13:52:33 -06:00
fix: Only pull hits when needed
This commit is contained in:
parent
2b9fafe440
commit
a60853fd21
3 changed files with 12 additions and 7 deletions
|
@ -13,9 +13,14 @@ pub struct DBRow {
|
|||
}
|
||||
|
||||
// Find a single URL
|
||||
pub fn find_url(shortlink: &str, db: &Connection) -> (Option<String>, Option<i64>) {
|
||||
pub fn find_url(shortlink: &str, db: &Connection, needhits: bool) -> (Option<String>, Option<i64>) {
|
||||
let query = if needhits {
|
||||
"SELECT long_url,hits FROM urls WHERE short_url = ?1"
|
||||
} else {
|
||||
"SELECT long_url FROM urls WHERE short_url = ?1"
|
||||
};
|
||||
let mut statement = db
|
||||
.prepare_cached("SELECT long_url,hits FROM urls WHERE short_url = ?1")
|
||||
.prepare_cached(query)
|
||||
.expect("Error preparing SQL statement for find_url.");
|
||||
|
||||
let longlink = statement
|
||||
|
|
|
@ -137,7 +137,7 @@ pub async fn getall(
|
|||
pub async fn expand(req: String, data: web::Data<AppState>, http: HttpRequest) -> HttpResponse {
|
||||
let result = utils::is_api_ok(http);
|
||||
if result.success {
|
||||
let linkinfo = utils::get_longurl(req, &data.db);
|
||||
let linkinfo = utils::get_longurl(req, &data.db, true);
|
||||
if let Some(longlink) = linkinfo.0 {
|
||||
let body = LinkInfo {
|
||||
success: true,
|
||||
|
@ -192,7 +192,7 @@ pub async fn link_handler(
|
|||
data: web::Data<AppState>,
|
||||
) -> impl Responder {
|
||||
let shortlink_str = shortlink.to_string();
|
||||
if let Some(longlink) = utils::get_longurl(shortlink_str, &data.db).0 {
|
||||
if let Some(longlink) = utils::get_longurl(shortlink_str, &data.db, false).0 {
|
||||
let redirect_method = env::var("redirect_method").unwrap_or(String::from("PERMANENT"));
|
||||
database::add_hit(shortlink.as_str(), &data.db);
|
||||
if redirect_method == "TEMPORARY" {
|
||||
|
|
|
@ -80,9 +80,9 @@ pub fn is_api_ok(http: HttpRequest) -> Response {
|
|||
}
|
||||
|
||||
// Request the DB for searching an URL
|
||||
pub fn get_longurl(shortlink: String, db: &Connection) -> (Option<String>, Option<i64>) {
|
||||
pub fn get_longurl(shortlink: String, db: &Connection, needhits: bool) -> (Option<String>, Option<i64>) {
|
||||
if validate_link(&shortlink) {
|
||||
database::find_url(shortlink.as_str(), db)
|
||||
database::find_url(shortlink.as_str(), db, needhits)
|
||||
} else {
|
||||
(None, None)
|
||||
}
|
||||
|
@ -124,7 +124,7 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) {
|
|||
}
|
||||
|
||||
if validate_link(chunks.shortlink.as_str())
|
||||
&& get_longurl(chunks.shortlink.clone(), db).0.is_none()
|
||||
&& get_longurl(chunks.shortlink.clone(), db, false).0.is_none()
|
||||
{
|
||||
(
|
||||
database::add_link(chunks.shortlink.clone(), chunks.longlink, db),
|
||||
|
|
Loading…
Reference in a new issue