Ability to add new links

This commit is contained in:
SinTan1729
2023-04-03 17:40:37 -05:00
parent 618fd0e53a
commit 7f6ba5175f
5 changed files with 93 additions and 7 deletions

View File

@@ -16,7 +16,6 @@ pub fn find_url(shortlink: &str) -> String {
longlink = link.unwrap();
}
add_hit(shortlink);
longlink
}
@@ -37,7 +36,7 @@ pub fn getall() -> Vec<String> {
links
}
fn add_hit(shortlink: &str) -> () {
pub fn add_hit(shortlink: &str) -> () {
let db = Connection::open("./urls.sqlite").expect("Unable to open database!");
db.execute(
"UPDATE urls SET hits = hits + 1 WHERE short_url = ?1",
@@ -45,3 +44,15 @@ fn add_hit(shortlink: &str) -> () {
)
.unwrap();
}
pub fn add_link(shortlink: String, longlink: String) -> bool {
let db = Connection::open("./urls.sqlite").expect("Unable to open database!");
match db.execute(
"INSERT INTO urls (long_url, short_url, hits) VALUES (?1, ?2, ?3)",
(longlink, shortlink, 0),
) {
Ok(_) => true,
Err(_) => false,
}
}