diff options
Diffstat (limited to 'src/db')
| -rw-r--r-- | src/db/mod.rs | 63 | ||||
| -rw-r--r-- | src/db/models.rs | 65 | ||||
| -rw-r--r-- | src/db/schema.rs | 82 |
3 files changed, 210 insertions, 0 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs new file mode 100644 index 0000000..8ce0011 --- /dev/null +++ b/src/db/mod.rs @@ -0,0 +1,63 @@ +use std::env;
+
+use diesel::prelude::*;
+
+use super::{Result, Error};
+pub use self::models::*;
+
+mod schema;
+mod models;
+
+fn connection() -> Result<PgConnection> {
+ let database_url = env::var("DATABASE_URL")?;
+ PgConnection::establish(&database_url).map_err(Error::from)
+}
+
+pub fn find_text(search: String) -> Result<TextMeme> {
+ use self::schema::text_memes::dsl::*;
+
+ let format_search = format!("%{}%", search);
+
+ let conn = connection()?;
+ text_memes
+ .filter(title.ilike(&format_search).or(content.ilike(&format_search)))
+ .limit(1)
+ .first::<TextMeme>(&conn)
+ .map_err(Error::from)
+}
+
+pub fn find_audio(search: String) -> Result<AudioMeme> {
+ use self::schema::audio_memes::dsl::*;
+
+ let format_search = format!("%{}%", search);
+
+ let conn = connection()?;
+ audio_memes
+ .filter(title.ilike(format_search))
+ .limit(1)
+ .first::<AudioMeme>(&conn)
+ .map_err(Error::from)
+}
+
+pub fn rand_audio() -> Result<AudioMeme> {
+ use self::schema::audio_memes::dsl::*;
+
+ let conn = connection()?;
+ audio_memes
+ .order(random.desc())
+ .first::<AudioMeme>(&conn)
+ .map_err(Error::from)
+}
+
+pub fn rand_text() -> Result<TextMeme> {
+ use self::schema::text_memes::dsl::*;
+
+ let conn = connection()?;
+ text_memes
+ .order(random.desc())
+ .first::<TextMeme>(&conn)
+ .map_err(Error::from)
+}
+
+use diesel::sql_types;
+no_arg_sql_function!(random, sql_types::Double, "SQL random() function");
diff --git a/src/db/models.rs b/src/db/models.rs new file mode 100644 index 0000000..c06c41e --- /dev/null +++ b/src/db/models.rs @@ -0,0 +1,65 @@ +use super::schema::*;
+use chrono::naive::NaiveDateTime;
+
+#[derive(Insertable, Queryable, Identifiable, AsChangeset, Debug, Associations)]
+#[belongs_to(Audio)]
+#[belongs_to(Image)]
+#[belongs_to(TextMeme)]
+#[belongs_to(ImageMeme)]
+#[belongs_to(TextMeme)]
+#[table_name="metadata"]
+pub struct Metadata {
+ pub id: i32,
+ pub created: NaiveDateTime,
+ pub created_by: i64,
+}
+
+#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug, Associations)]
+#[belongs_to(AudioMeme)]
+#[belongs_to(TextMeme)]
+#[table_name="audio"]
+pub struct Audio {
+ pub id: i32,
+ pub data: Vec<u8>,
+ pub metadata_id: i32,
+}
+
+#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug, Associations)]
+#[belongs_to(ImageMeme)]
+#[belongs_to(TextMeme)]
+#[table_name="images"]
+pub struct Image {
+ pub id: i32,
+ pub data: Vec<u8>,
+ pub metadata_id: i32,
+}
+
+#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug)]
+#[table_name="audio_memes"]
+pub struct AudioMeme {
+ pub id: i32,
+ pub title: String,
+ pub audio_id: i32,
+ pub metadata_id: i32,
+}
+
+#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug)]
+#[table_name="text_memes"]
+pub struct TextMeme {
+ pub id: i32,
+ pub title: String,
+ pub content: String,
+ pub image_id: Option<i32>,
+ pub audio_id: Option<i32>,
+ pub metadata_id: i32,
+}
+
+#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug, Associations)]
+#[belongs_to(Metadata)]
+#[table_name="audit_records"]
+pub struct AuditRecord {
+ pub id: i32,
+ pub updated: NaiveDateTime,
+ pub updated_by: i64,
+ pub metadata_id: i32,
+}
diff --git a/src/db/schema.rs b/src/db/schema.rs new file mode 100644 index 0000000..40891a5 --- /dev/null +++ b/src/db/schema.rs @@ -0,0 +1,82 @@ +table! { + audio (id) { + id -> Int4, + data -> Bytea, + metadata_id -> Int4, + } +} + +table! { + audio_memes (id) { + id -> Int4, + title -> Varchar, + audio_id -> Int4, + metadata_id -> Int4, + } +} + +table! { + audit_records (id) { + id -> Int4, + updated -> Timestamp, + updated_by -> Int8, + metadata_id -> Int4, + } +} + +table! { + image_memes (id) { + id -> Int4, + title -> Varchar, + image_id -> Int4, + metadata_id -> Int4, + } +} + +table! { + images (id) { + id -> Int4, + data -> Bytea, + metadata_id -> Int4, + } +} + +table! { + metadata (id) { + id -> Int4, + created -> Timestamp, + created_by -> Int8, + } +} + +table! { + text_memes (id) { + id -> Int4, + title -> Varchar, + content -> Text, + image_id -> Nullable<Int4>, + audio_id -> Nullable<Int4>, + metadata_id -> Int4, + } +} + +joinable!(audio -> metadata (metadata_id)); +joinable!(audio_memes -> audio (audio_id)); +joinable!(audio_memes -> metadata (metadata_id)); +joinable!(audit_records -> metadata (metadata_id)); +joinable!(image_memes -> images (image_id)); +joinable!(image_memes -> metadata (metadata_id)); +joinable!(images -> metadata (metadata_id)); +joinable!(text_memes -> audio (audio_id)); +joinable!(text_memes -> images (image_id)); +joinable!(text_memes -> metadata (metadata_id)); + +allow_tables_to_appear_in_same_query!( + audio, + audio_memes, + audit_records, + image_memes, + images, + metadata, + text_memes, +); |
