mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-11-22 17:26:45 +00:00
chg: Use json for sending url list
This commit is contained in:
parent
99b5298cd8
commit
f2b5e1ab6d
@ -1,4 +1,12 @@
|
|||||||
use rusqlite::Connection;
|
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> {
|
pub fn find_url(shortlink: &str, db: &Connection) -> Option<String> {
|
||||||
let mut statement = db
|
let mut statement = db
|
||||||
@ -10,17 +18,19 @@ pub fn find_url(shortlink: &str, db: &Connection) -> Option<String> {
|
|||||||
.ok()
|
.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 statement = db.prepare_cached("SELECT * FROM urls").unwrap();
|
||||||
|
|
||||||
let mut data = statement.query([]).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() {
|
while let Some(row) = data.next().unwrap() {
|
||||||
let short_url: String = row.get("short_url").unwrap();
|
let row_struct = DbRow {
|
||||||
let long_url: String = row.get("long_url").unwrap();
|
shortlink: row.get("short_url").unwrap(),
|
||||||
let hits: i64 = row.get("hits").unwrap();
|
longlink: row.get("long_url").unwrap(),
|
||||||
links.push(format!("{short_url},{long_url},{hits}"));
|
hits: row.get("hits").unwrap(),
|
||||||
|
};
|
||||||
|
links.push(row_struct);
|
||||||
}
|
}
|
||||||
|
|
||||||
links
|
links
|
||||||
|
@ -6,7 +6,6 @@ use rand::seq::SliceRandom;
|
|||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::from_str;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Url {
|
struct Url {
|
||||||
@ -29,11 +28,11 @@ fn validate_link(link: &str) -> bool {
|
|||||||
|
|
||||||
pub fn getall(db: &Connection) -> String {
|
pub fn getall(db: &Connection) -> String {
|
||||||
let links = database::getall(db);
|
let links = database::getall(db);
|
||||||
links.join("\n")
|
serde_json::to_string(&links).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_link(req: String, db: &Connection) -> (bool, String) {
|
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 style = env::var("slug_style").unwrap_or(String::from("Pair"));
|
||||||
let len_str = env::var("slug_length").unwrap_or(String::from("8"));
|
let len_str = env::var("slug_length").unwrap_or(String::from("8"));
|
||||||
|
@ -31,16 +31,7 @@ const refreshData = async () => {
|
|||||||
document.getElementById("login-dialog").showModal();
|
document.getElementById("login-dialog").showModal();
|
||||||
document.getElementById("password").focus();
|
document.getElementById("password").focus();
|
||||||
} else {
|
} else {
|
||||||
data = reply
|
data = JSON.parse(reply)
|
||||||
.split("\n")
|
|
||||||
.filter(line => line !== "")
|
|
||||||
.map(line => line.split(","))
|
|
||||||
.map(arr => ({
|
|
||||||
short: arr[0],
|
|
||||||
long: arr[1],
|
|
||||||
hits: arr[2]
|
|
||||||
}));
|
|
||||||
|
|
||||||
displayData(data);
|
displayData(data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -87,18 +78,18 @@ const showAlert = async (text, col) => {
|
|||||||
|
|
||||||
const TR = (row, site) => {
|
const TR = (row, site) => {
|
||||||
const tr = document.createElement("tr");
|
const tr = document.createElement("tr");
|
||||||
const longTD = TD(A_LONG(row.long), "Long URL");
|
const longTD = TD(A_LONG(row["longlink"]), "Long URL");
|
||||||
var shortTD = null;
|
var shortTD = null;
|
||||||
if (window.isSecureContext) {
|
if (window.isSecureContext) {
|
||||||
shortTD = TD(A_SHORT(row.short, site), "Short URL");
|
shortTD = TD(A_SHORT(row["shortlink"], site), "Short URL");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
shortTD = TD(A_SHORT_INSECURE(row.short, site), "Short URL");
|
shortTD = TD(A_SHORT_INSECURE(row["shortlink"], site), "Short URL");
|
||||||
}
|
}
|
||||||
hitsTD = TD(row.hits);
|
hitsTD = TD(row["hits"]);
|
||||||
hitsTD.setAttribute("label", "Hits");
|
hitsTD.setAttribute("label", "Hits");
|
||||||
hitsTD.setAttribute("name", "hitsColumn");
|
hitsTD.setAttribute("name", "hitsColumn");
|
||||||
const btn = deleteButton(row.short);
|
const btn = deleteButton(row["shortlink"]);
|
||||||
|
|
||||||
tr.appendChild(shortTD);
|
tr.appendChild(shortTD);
|
||||||
tr.appendChild(longTD);
|
tr.appendChild(longTD);
|
||||||
|
Loading…
Reference in New Issue
Block a user