aboutsummaryrefslogtreecommitdiff
path: root/src/db/mod.rs
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-05-12 23:48:04 -0700
committerFuwn <[email protected]>2021-05-12 23:48:04 -0700
commitc7a282bd3f96c0e3cc0807aabd5b7104df6effc9 (patch)
tree64818c0add5762ba1ae83c8d9e7aa7bdb605f6f2 /src/db/mod.rs
downloadlime-main.tar.xz
lime-main.zip
feat: :star:HEADmain
Diffstat (limited to 'src/db/mod.rs')
-rw-r--r--src/db/mod.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs
new file mode 100644
index 0000000..b0321cb
--- /dev/null
+++ b/src/db/mod.rs
@@ -0,0 +1,56 @@
+// Copyright (C) 2021-2021 Fuwn
+// SPDX-License-Identifier: GPL-3.0-only
+
+pub mod models;
+mod schema;
+
+use diesel::{insert_into, prelude::*, update};
+
+use crate::db::models::{Link, LinkForm};
+
+fn establish_connection() -> SqliteConnection {
+ let database_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| "lime.sqlite3".to_string());
+ SqliteConnection::establish(&database_url)
+ .unwrap_or_else(|_| panic!("error connecting to {}", database_url))
+}
+
+pub fn insert_link(form: LinkForm) -> Result<(), Box<dyn std::error::Error>> {
+ use schema::links::dsl::*;
+
+ if find_link(form.short, false).is_err() {
+ insert_into(links)
+ .values((long.eq(form.long), short.eq(form.short), ip.eq(form.ip)))
+ .execute(&establish_connection())?;
+ } else {
+ bail!("short url already exists");
+ }
+
+ Ok(())
+}
+
+pub fn find_link(short_c: &str, tick: bool) -> Result<Link, Box<dyn std::error::Error>> {
+ use schema::links::dsl::*;
+
+ let results = links
+ .filter(short.eq(short_c))
+ .load::<Link>(&establish_connection())
+ .unwrap();
+
+ if results.is_empty() {
+ bail!("no entry found with the short url: /{}", short_c)
+ } else {
+ let long_c = results[0].clone();
+
+ if tick {
+ update(links.find(&long_c.short))
+ .set(uses.eq(long_c.uses + 1))
+ .execute(&establish_connection())?;
+ }
+
+ if long_c.disabled {
+ bail!("short url disabled")
+ }
+
+ Ok(long_c)
+ }
+}