aboutsummaryrefslogtreecommitdiff
path: root/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/db')
-rw-r--r--src/db/mod.rs69
-rw-r--r--src/db/models.rs54
2 files changed, 82 insertions, 41 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)
}
diff --git a/src/db/models.rs b/src/db/models.rs
index 85cba2a..c07a12a 100644
--- a/src/db/models.rs
+++ b/src/db/models.rs
@@ -1,5 +1,9 @@
-use super::schema::*;
use chrono::naive::NaiveDateTime;
+use diesel::prelude::*;
+
+use super::schema::*;
+use super::AssociatedData;
+use ::{Result, Error};
#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug)]
#[table_name="text_memes"]
@@ -12,6 +16,18 @@ pub struct TextMeme {
pub metadata_id: i32,
}
+impl AssociatedData for TextMeme {
+ type Associated = (Option<Image>, Option<Audio>);
+
+ fn associated_data(&self, conn: &PgConnection) -> Result<Self::Associated> {
+ let image = self.image_id.map(|x: i32| images::table.find(x).first(conn)).transpose()?;
+ let audio = self.audio_id.map(|x: i32| audio::table.find(x).first(conn)).transpose()?;
+
+ Ok((image, audio))
+ }
+}
+
+
#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug)]
#[table_name="image_memes"]
pub struct ImageMeme {
@@ -21,6 +37,15 @@ pub struct ImageMeme {
pub metadata_id: i32,
}
+impl AssociatedData for ImageMeme {
+ type Associated = Image;
+
+ fn associated_data(&self, conn: &PgConnection) -> Result<Self::Associated> {
+ images::table.find(self.image_id).first(conn).map_err(Error::from)
+ }
+}
+
+
#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug)]
#[table_name="audio_memes"]
pub struct AudioMeme {
@@ -30,9 +55,16 @@ pub struct AudioMeme {
pub metadata_id: i32,
}
-#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug, Associations)]
-#[belongs_to(AudioMeme)]
-#[belongs_to(TextMeme)]
+impl AssociatedData for AudioMeme {
+ type Associated = Audio;
+
+ fn associated_data(&self, conn: &PgConnection) -> Result<Self::Associated> {
+ audio::table.find(self.audio_id).first(conn).map_err(Error::from)
+ }
+}
+
+
+#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug)]
#[table_name="audio"]
pub struct Audio {
pub id: i32,
@@ -40,9 +72,7 @@ pub struct Audio {
pub metadata_id: i32,
}
-#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug, Associations)]
-#[belongs_to(ImageMeme)]
-#[belongs_to(TextMeme)]
+#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug)]
#[table_name="images"]
pub struct Image {
pub id: i32,
@@ -50,12 +80,7 @@ pub struct Image {
pub metadata_id: i32,
}
-#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug, Associations)]
-#[belongs_to(Audio)]
-#[belongs_to(Image)]
-#[belongs_to(TextMeme)]
-#[belongs_to(ImageMeme)]
-#[belongs_to(TextMeme)]
+#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug)]
#[table_name="metadata"]
pub struct Metadata {
pub id: i32,
@@ -63,8 +88,7 @@ pub struct Metadata {
pub created_by: i64,
}
-#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug, Associations)]
-#[belongs_to(Metadata)]
+#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug)]
#[table_name="audit_records"]
pub struct AuditRecord {
pub id: i32,