2023-04-03 15:46:22 -05:00
|
|
|
use rusqlite::Connection;
|
2023-04-02 22:26:23 -05:00
|
|
|
|
2023-04-03 18:52:17 -05:00
|
|
|
pub fn find_url(shortlink: &str, db: &Connection) -> String {
|
2023-04-03 15:46:22 -05:00
|
|
|
let mut statement = db
|
|
|
|
.prepare_cached("SELECT long_url FROM urls WHERE short_url = ?1")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let links = statement
|
|
|
|
.query_map([shortlink], |row| Ok(row.get("long_url")?))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut longlink = "".to_string();
|
|
|
|
for link in links {
|
|
|
|
longlink = link.unwrap();
|
2023-04-02 22:26:23 -05:00
|
|
|
}
|
|
|
|
|
2023-04-03 15:46:22 -05:00
|
|
|
longlink
|
2023-04-02 22:26:23 -05:00
|
|
|
}
|
2023-04-03 11:55:27 -05:00
|
|
|
|
2023-04-03 18:52:17 -05:00
|
|
|
pub fn getall(db: &Connection) -> Vec<String> {
|
2023-04-03 15:46:22 -05:00
|
|
|
let mut statement = db.prepare_cached("SELECT * FROM urls").unwrap();
|
2023-04-03 11:55:27 -05:00
|
|
|
|
2023-04-03 15:46:22 -05:00
|
|
|
let mut data = statement.query([]).unwrap();
|
2023-04-03 11:55:27 -05:00
|
|
|
|
|
|
|
let mut links: Vec<String> = Vec::new();
|
2023-04-03 15:46:22 -05:00
|
|
|
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();
|
2023-04-03 11:55:27 -05:00
|
|
|
links.push(format!("{short_url},{long_url},{hits}"));
|
|
|
|
}
|
|
|
|
|
|
|
|
links
|
|
|
|
}
|
2023-04-03 15:46:22 -05:00
|
|
|
|
2023-04-03 18:52:17 -05:00
|
|
|
pub fn add_hit(shortlink: &str, db: &Connection) -> () {
|
2023-04-03 15:46:22 -05:00
|
|
|
db.execute(
|
|
|
|
"UPDATE urls SET hits = hits + 1 WHERE short_url = ?1",
|
|
|
|
[shortlink],
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
2023-04-03 17:40:37 -05:00
|
|
|
|
2023-04-03 18:52:17 -05:00
|
|
|
pub fn add_link(shortlink: String, longlink: String, db: &Connection) -> bool {
|
2023-04-03 17:40:37 -05:00
|
|
|
match db.execute(
|
|
|
|
"INSERT INTO urls (long_url, short_url, hits) VALUES (?1, ?2, ?3)",
|
|
|
|
(longlink, shortlink, 0),
|
|
|
|
) {
|
|
|
|
Ok(_) => true,
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 17:58:19 -05:00
|
|
|
|
2023-04-03 18:52:17 -05:00
|
|
|
pub fn delete_link(shortlink: String, db: &Connection) -> () {
|
2023-04-03 17:58:19 -05:00
|
|
|
db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink])
|
|
|
|
.unwrap();
|
|
|
|
}
|
2023-04-03 18:52:17 -05:00
|
|
|
|
|
|
|
pub fn open_db(path: String) -> Connection {
|
|
|
|
let db = Connection::open(path).expect("Unable to open database!");
|
2023-04-08 15:36:33 -05:00
|
|
|
// Create table if it doesn't exist
|
2023-04-03 18:52:17 -05:00
|
|
|
db.execute(
|
|
|
|
"CREATE TABLE IF NOT EXISTS urls (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
long_url TEXT NOT NULL,
|
|
|
|
short_url TEXT NOT NULL,
|
|
|
|
hits INTEGER NOT NULL
|
|
|
|
)",
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
db
|
|
|
|
}
|