summaryrefslogtreecommitdiff
path: root/src/db/models.rs
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2018-04-06 19:22:55 -0400
committerNathan Perry <avaglir@gmail.com>2018-04-06 19:22:55 -0400
commitd6bea61fa917d257219a43386c88a4f04cf82408 (patch)
treec008923ffd1a934a4d295dddb9f5557a0e96ab4f /src/db/models.rs
parentd209da7a02887f433bfdd44f9b225179bdbb7b75 (diff)
database alteration in-flight
Diffstat (limited to 'src/db/models.rs')
-rw-r--r--src/db/models.rs108
1 files changed, 104 insertions, 4 deletions
diff --git a/src/db/models.rs b/src/db/models.rs
index 7479fff..32ccc4b 100644
--- a/src/db/models.rs
+++ b/src/db/models.rs
@@ -17,11 +17,11 @@ pub struct Meme {
impl Meme {
pub fn image(&self, conn: &PgConnection) -> Option<Result<Image>> {
- self.image_id.map(|x: i32| images::table.find(x).first(conn).map_err(Error::from))
+ self.image_id.map(|x: i32| images::table.filter(images::id.eq(x)).first(conn).map_err(Error::from))
}
pub fn audio(&self, conn: &PgConnection) -> Option<Result<Audio>> {
- self.audio_id.map(|x: i32| audio::table.find(x).first(conn).map_err(Error::from))
+ self.audio_id.map(|x: i32| audio::table.filter(audio::id.eq(x)).first(conn).map_err(Error::from))
}
}
@@ -35,19 +35,65 @@ pub struct NewMeme {
pub metadata_id: i32,
}
+impl NewMeme {
+ pub fn save(mut self, conn: &PgConnection, by_user: u64) -> Result<Meme> {
+ let metadata = Metadata::create(conn, by_user)?;
+
+ self.metadata_id = metadata.id;
+
+ ::diesel::insert_into(memes::table)
+ .values(&self)
+ .get_result::<Meme>(conn)
+ .map_err(Error::from)
+ }
+}
+
#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[table_name="audio"]
pub struct Audio {
pub id: i32,
pub data: Vec<u8>,
+ pub data_hash: Vec<u8>,
pub metadata_id: i32,
}
+impl Audio {
+ pub fn create(conn: &PgConnection, data: Vec<u8>, by_user: u64) -> Result<i32> {
+ let mut data_hash = ::sha1::Sha1::new();
+ data_hash.update(&data);
+ let data_hash = data_hash.digest().bytes().to_vec();
+
+ let id = audio::table
+ .select(audio::id)
+ .filter(audio::data_hash.eq(&data_hash))
+ .get_results::<i32>(conn)?;
+
+ if let Some(id) = id.first() {
+ return Ok(*id);
+ }
+
+ let metadata = Metadata::create(conn, by_user)?;
+
+ let new_audio = NewAudio {
+ data,
+ data_hash,
+ metadata_id: metadata.id,
+ };
+
+ ::diesel::insert_into(audio::table)
+ .values(&new_audio)
+ .returning(audio::id)
+ .get_result(conn)
+ .map_err(Error::from)
+ }
+}
+
#[derive(Insertable, PartialEq, Debug)]
#[table_name="audio"]
pub struct NewAudio {
pub data: Vec<u8>,
+ pub data_hash: Vec<u8>,
pub metadata_id: i32,
}
@@ -57,13 +103,46 @@ pub struct NewAudio {
pub struct Image {
pub id: i32,
pub data: Vec<u8>,
+ pub data_hash: Vec<u8>,
pub metadata_id: i32,
}
+impl Image {
+ pub fn create(conn: &PgConnection, data: Vec<u8>, by_user: u64) -> Result<i32> {
+ let mut data_hash = ::sha1::Sha1::new();
+ data_hash.update(&data);
+ let data_hash = data_hash.digest().bytes().to_vec();
+
+ let id = images::table
+ .select(images::id)
+ .filter(images::data_hash.eq(&data_hash))
+ .get_results::<i32>(conn)?;
+
+ if let Some(id) = id.first() {
+ return Ok(*id);
+ }
+
+ let metadata = Metadata::create(conn, by_user)?;
+
+ let new_image = NewImage {
+ data,
+ data_hash,
+ metadata_id: metadata.id,
+ };
+
+ ::diesel::insert_into(images::table)
+ .values(&new_image)
+ .returning(images::id)
+ .get_result(conn)
+ .map_err(Error::from)
+ }
+}
+
#[derive(Insertable, PartialEq, Debug)]
#[table_name="images"]
pub struct NewImage {
pub data: Vec<u8>,
+ pub data_hash: Vec<u8>,
pub metadata_id: i32,
}
@@ -76,10 +155,20 @@ pub struct Metadata {
pub created_by: i64,
}
+impl Metadata {
+ pub fn create(conn: &PgConnection, by_user: u64) -> Result<Metadata> {
+ ::diesel::insert_into(metadata::table)
+ .values(&NewMetadata {
+ created_by: by_user as i64,
+ })
+ .get_result::<Metadata>(conn)
+ .map_err(Error::from)
+ }
+}
+
#[derive(Insertable, PartialEq, Debug)]
#[table_name="metadata"]
pub struct NewMetadata {
- pub created: NaiveDateTime,
pub created_by: i64,
}
@@ -93,10 +182,21 @@ pub struct AuditRecord {
pub metadata_id: i32,
}
+impl AuditRecord {
+ pub fn create(conn: &PgConnection, metadata: i32, by_user: u64) -> Result<AuditRecord> {
+ ::diesel::insert_into(audit_records::table)
+ .values(&NewAuditRecord {
+ updated_by: by_user as i64,
+ metadata_id: metadata,
+ })
+ .get_result::<AuditRecord>(conn)
+ .map_err(Error::from)
+ }
+}
+
#[derive(Insertable, PartialEq, Debug)]
#[table_name="audit_records"]
pub struct NewAuditRecord {
- pub updated: NaiveDateTime,
pub updated_by: i64,
pub metadata_id: i32,
}