blob: b0321cb8b0ccfb516588846d1672282b71d9a272 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)
}
}
|