blob: 8657a4f805a9176788e5c92290342065b7ab8af6 (
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
57
58
59
60
61
62
63
|
use std::env;
use std::convert::AsRef;
use diesel::prelude::*;
use diesel::r2d2::{ConnectionManager, ManageConnection};
use super::{Result, Error};
pub use self::models::*;
use self::schema::*;
mod schema;
mod models;
lazy_static! {
static ref DB_URL: String = env::var("DATABASE_URL").expect("no database url in environment").into();
static ref CONN_MGR: ConnectionManager<PgConnection> = ConnectionManager::new(DB_URL.clone());
}
pub fn connection() -> Result<PgConnection> {
CONN_MGR.connect().map_err(Error::from)
}
pub fn find_meme<T: AsRef<str>>(conn: &PgConnection, search: T) -> Result<Meme> {
use diesel::dsl::sql;
use diesel::sql_types::Text;
let search = search.as_ref();
let format_search = format!("%{}%", search);
// TODO: check for injection
memes::table
.filter(memes::title.ilike(&format_search).or(sql("content ILIKE ").bind::<Text, _>(&format_search)))
.limit(1)
.first::<Meme>(conn)
.map_err(Error::from)
}
pub fn rand_text(conn: &PgConnection) -> Result<Meme> {
memes::table
.filter(memes::content.is_not_null())
.order(random.desc())
.first::<Meme>(conn)
.map_err(Error::from)
}
pub fn rand_image(conn: &PgConnection) -> Result<Meme> {
memes::table
.filter(memes::image_id.is_not_null())
.order(random.desc())
.first::<Meme>(conn)
.map_err(Error::from)
}
pub fn rand_audio(conn: &PgConnection) -> Result<Meme> {
memes::table
.filter(memes::audio_id.is_not_null())
.order(random.desc())
.first::<Meme>(conn)
.map_err(Error::from)
}
use diesel::sql_types;
no_arg_sql_function!(random, sql_types::Double, "SQL random() function");
|