summaryrefslogtreecommitdiff
path: root/src/db/mod.rs
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2018-04-05 20:53:37 -0400
committerNathan Perry <avaglir@gmail.com>2018-04-05 20:53:37 -0400
commit1fda857d25c3d33e593951eef3ce713fa69a7025 (patch)
tree23f714c3fc7c7233e498307480109f3ab2f779e3 /src/db/mod.rs
parent82aefce4f7c5f24730fb5da22ee426988ae4301a (diff)
start to integrate db support with commands
Diffstat (limited to 'src/db/mod.rs')
-rw-r--r--src/db/mod.rs69
1 files changed, 43 insertions, 26 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs
index 8ce0011..e1c3a55 100644
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -1,61 +1,78 @@
use std::env;
use diesel::prelude::*;
+use diesel::r2d2::{ConnectionManager, ManageConnection};
use super::{Result, Error};
pub use self::models::*;
+use self::schema::*;
mod schema;
mod models;
-fn connection() -> Result<PgConnection> {
- let database_url = env::var("DATABASE_URL")?;
- PgConnection::establish(&database_url).map_err(Error::from)
+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 find_text(search: String) -> Result<TextMeme> {
- use self::schema::text_memes::dsl::*;
+pub fn connection() -> Result<PgConnection> {
+ CONN_MGR.connect().map_err(Error::from)
+}
+
+pub trait AssociatedData {
+ type Associated;
+
+ fn associated_data(&self, conn: &PgConnection) -> Result<Self::Associated>;
+}
+pub fn find_text(conn: &PgConnection, search: String) -> Result<TextMeme> {
let format_search = format!("%{}%", search);
- let conn = connection()?;
- text_memes
- .filter(title.ilike(&format_search).or(content.ilike(&format_search)))
+ text_memes::table
+ .filter(text_memes::title.ilike(&format_search).or(text_memes::content.ilike(&format_search)))
.limit(1)
- .first::<TextMeme>(&conn)
+ .first::<TextMeme>(conn)
.map_err(Error::from)
}
-pub fn find_audio(search: String) -> Result<AudioMeme> {
- use self::schema::audio_memes::dsl::*;
-
+pub fn find_audio(conn: &PgConnection, search: String) -> Result<AudioMeme> {
let format_search = format!("%{}%", search);
- let conn = connection()?;
- audio_memes
- .filter(title.ilike(format_search))
+ audio_memes::table
+ .filter(audio_memes::title.ilike(format_search))
.limit(1)
- .first::<AudioMeme>(&conn)
+ .first::<AudioMeme>(conn)
.map_err(Error::from)
}
-pub fn rand_audio() -> Result<AudioMeme> {
- use self::schema::audio_memes::dsl::*;
+pub fn find_image(conn: &PgConnection, search: String) -> Result<ImageMeme> {
+ let format_search = format!("%{}%", search);
- let conn = connection()?;
- audio_memes
+ image_memes::table
+ .filter(image_memes::title.ilike(format_search))
+ .limit(1)
+ .first::<ImageMeme>(conn)
+ .map_err(Error::from)
+}
+
+pub fn rand_text(conn: &PgConnection) -> Result<TextMeme> {
+ text_memes::table
.order(random.desc())
- .first::<AudioMeme>(&conn)
+ .first::<TextMeme>(conn)
.map_err(Error::from)
}
-pub fn rand_text() -> Result<TextMeme> {
- use self::schema::text_memes::dsl::*;
+pub fn rand_image(conn: &PgConnection) -> Result<ImageMeme> {
+ image_memes::table
+ .order(random.desc())
+ .first::<ImageMeme>(conn)
+ .map_err(Error::from)
+}
- let conn = connection()?;
- text_memes
+pub fn rand_audio(conn: &PgConnection) -> Result<AudioMeme> {
+ audio_memes::table
.order(random.desc())
- .first::<TextMeme>(&conn)
+ .first::<AudioMeme>(conn)
.map_err(Error::from)
}