summaryrefslogtreecommitdiff
path: root/src/db/models.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/models.rs
parent82aefce4f7c5f24730fb5da22ee426988ae4301a (diff)
start to integrate db support with commands
Diffstat (limited to 'src/db/models.rs')
-rw-r--r--src/db/models.rs54
1 files changed, 39 insertions, 15 deletions
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,