chg: Use json for sending url list

This commit is contained in:
SinTan1729
2024-03-31 15:38:59 -05:00
parent 99b5298cd8
commit f2b5e1ab6d
3 changed files with 24 additions and 24 deletions

View File

@@ -1,4 +1,12 @@
use rusqlite::Connection;
use serde::Serialize;
#[derive(Serialize)]
pub struct DbRow {
shortlink: String,
longlink: String,
hits: i64,
}
pub fn find_url(shortlink: &str, db: &Connection) -> Option<String> {
let mut statement = db
@@ -10,17 +18,19 @@ pub fn find_url(shortlink: &str, db: &Connection) -> Option<String> {
.ok()
}
pub fn getall(db: &Connection) -> Vec<String> {
pub fn getall(db: &Connection) -> Vec<DbRow> {
let mut statement = db.prepare_cached("SELECT * FROM urls").unwrap();
let mut data = statement.query([]).unwrap();
let mut links: Vec<String> = Vec::new();
let mut links: Vec<DbRow> = Vec::new();
while let Some(row) = data.next().unwrap() {
let short_url: String = row.get("short_url").unwrap();
let long_url: String = row.get("long_url").unwrap();
let hits: i64 = row.get("hits").unwrap();
links.push(format!("{short_url},{long_url},{hits}"));
let row_struct = DbRow {
shortlink: row.get("short_url").unwrap(),
longlink: row.get("long_url").unwrap(),
hits: row.get("hits").unwrap(),
};
links.push(row_struct);
}
links

View File

@@ -6,7 +6,6 @@ use rand::seq::SliceRandom;
use regex::Regex;
use rusqlite::Connection;
use serde::Deserialize;
use serde_json::from_str;
#[derive(Deserialize)]
struct Url {
@@ -29,11 +28,11 @@ fn validate_link(link: &str) -> bool {
pub fn getall(db: &Connection) -> String {
let links = database::getall(db);
links.join("\n")
serde_json::to_string(&links).unwrap()
}
pub fn add_link(req: String, db: &Connection) -> (bool, String) {
let mut chunks: Url = from_str(&req).unwrap();
let mut chunks: Url = serde_json::from_str(&req).unwrap();
let style = env::var("slug_style").unwrap_or(String::from("Pair"));
let len_str = env::var("slug_length").unwrap_or(String::from("8"));