1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-12-27 16:08:37 -06:00
chhoto-url/actix/src/database.rs

96 lines
2.7 KiB
Rust
Raw Normal View History

2024-04-03 20:40:26 -05:00
// SPDX-FileCopyrightText: 2023 Sayantan Santra <sayantan.santra689@gmail.com>
// SPDX-License-Identifier: MIT
2023-04-03 15:46:22 -05:00
use rusqlite::Connection;
2024-03-31 15:38:59 -05:00
use serde::Serialize;
// Struct for encoding a DB row
2024-03-31 15:38:59 -05:00
#[derive(Serialize)]
2024-03-31 16:07:33 -05:00
pub struct DBRow {
2024-03-31 15:38:59 -05:00
shortlink: String,
longlink: String,
hits: i64,
}
2023-04-02 22:26:23 -05:00
// Find a single URL
pub fn find_url(shortlink: &str, db: &Connection) -> Option<String> {
2023-04-03 15:46:22 -05:00
let mut statement = db
.prepare_cached("SELECT long_url FROM urls WHERE short_url = ?1")
.expect("Error preparing SQL statement for find_url.");
2023-04-03 15:46:22 -05:00
statement
.query_row([shortlink], |row| row.get("long_url"))
.ok()
2023-04-02 22:26:23 -05:00
}
2023-04-03 11:55:27 -05:00
// Get all URLs in DB
2024-03-31 16:07:33 -05:00
pub fn getall(db: &Connection) -> Vec<DBRow> {
let mut statement = db
.prepare_cached("SELECT * FROM urls")
.expect("Error preparing SQL statement for getall.");
2023-04-03 11:55:27 -05:00
let mut data = statement
.query([])
.expect("Error executing query for getall.");
2023-04-03 11:55:27 -05:00
2024-03-31 16:07:33 -05:00
let mut links: Vec<DBRow> = Vec::new();
while let Some(row) = data.next().expect("Error reading fetched rows.") {
2024-03-31 16:07:33 -05:00
let row_struct = DBRow {
shortlink: row
.get("short_url")
.expect("Error reading shortlink from row."),
longlink: row
.get("long_url")
.expect("Error reading shortlink from row."),
hits: row.get("hits").expect("Error reading shortlink from row."),
2024-03-31 15:38:59 -05:00
};
links.push(row_struct);
2023-04-03 11:55:27 -05:00
}
links
}
2023-04-03 15:46:22 -05:00
// Add a hit when site is visited
2023-04-26 14:40:54 -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],
)
.expect("Error updating hit count.");
2023-04-03 15:46:22 -05:00
}
2023-04-03 17:40:37 -05:00
// Insert a new link
2023-04-03 18:52:17 -05:00
pub fn add_link(shortlink: String, longlink: String, db: &Connection) -> bool {
2023-04-26 14:40:54 -05:00
db.execute(
2023-04-03 17:40:37 -05:00
"INSERT INTO urls (long_url, short_url, hits) VALUES (?1, ?2, ?3)",
(longlink, shortlink, 0),
2023-04-26 14:40:54 -05:00
)
.is_ok()
2023-04-03 17:40:37 -05:00
}
2023-04-03 17:58:19 -05:00
// Delete and existing link
pub fn delete_link(shortlink: String, db: &Connection) -> bool {
if let Ok(delta) = db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink]) {
delta > 0
} else {
false
}
2023-04-03 17:58:19 -05:00
}
2023-04-03 18:52:17 -05:00
// Open the DB, and create schema if missing
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
)",
[],
)
.expect("Unable to initialize empty database.");
2023-04-03 18:52:17 -05:00
db
}