aboutsummaryrefslogtreecommitdiff
path: root/src/db/models.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/models.rs')
-rw-r--r--src/db/models.rs132
1 files changed, 69 insertions, 63 deletions
diff --git a/src/db/models.rs b/src/db/models.rs
index cdcdd99..bba25a2 100644
--- a/src/db/models.rs
+++ b/src/db/models.rs
@@ -1,8 +1,8 @@
use chrono::naive::NaiveDateTime;
use diesel::{
+ prelude::*,
Identifiable,
Insertable,
- prelude::*,
Queryable,
};
use sha1::Digest;
@@ -16,21 +16,23 @@ use crate::{
#[derive(Queryable, Identifiable, PartialEq, Debug, Clone)]
#[diesel(table_name = memes)]
pub struct Meme {
- pub id: i32,
- pub title: String,
- pub content: Option<String>,
- pub image_id: Option<i32>,
- pub audio_id: Option<i32>,
+ pub id: i32,
+ pub title: String,
+ pub content: Option<String>,
+ pub image_id: Option<i32>,
+ pub audio_id: Option<i32>,
pub metadata_id: i32,
}
impl Meme {
pub fn image(&self, conn: &mut PgConnection) -> Option<Result<Image>> {
- self.image_id.map(|x: i32| images::table.filter(images::id.eq(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: &mut PgConnection) -> Option<Result<Audio>> {
- self.audio_id.map(|x: i32| audio::table.filter(audio::id.eq(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))
}
pub fn find(conn: &mut PgConnection, id: i32) -> Result<Meme> {
@@ -41,10 +43,10 @@ impl Meme {
#[derive(Insertable, PartialEq, Debug, Clone)]
#[diesel(table_name = memes)]
pub struct NewMeme {
- pub title: String,
- pub content: Option<String>,
- pub image_id: Option<i32>,
- pub audio_id: Option<i32>,
+ pub title: String,
+ pub content: Option<String>,
+ pub image_id: Option<i32>,
+ pub audio_id: Option<i32>,
pub metadata_id: i32,
}
@@ -54,28 +56,27 @@ impl NewMeme {
self.metadata_id = metadata.id;
- ::diesel::insert_into(memes::table)
+ diesel::insert_into(memes::table)
.values(&self)
.get_result::<Meme>(conn)
.map_err(Error::from)
}
}
-
#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[diesel(table_name = audio)]
pub struct Audio {
- pub id: i32,
- pub data: Vec<u8>,
+ pub id: i32,
+ pub data: Vec<u8>,
pub metadata_id: i32,
- pub data_hash: Vec<u8>,
+ pub data_hash: Vec<u8>,
}
impl Audio {
pub fn create(conn: &mut 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 data_hash = data_hash.finalize().to_vec();
let id = audio::table
.select(audio::id)
@@ -94,7 +95,7 @@ impl Audio {
metadata_id: metadata.id,
};
- ::diesel::insert_into(audio::table)
+ diesel::insert_into(audio::table)
.values(&new_audio)
.returning(audio::id)
.get_result(conn)
@@ -105,27 +106,31 @@ impl Audio {
#[derive(Insertable, PartialEq, Debug)]
#[diesel(table_name = audio)]
pub struct NewAudio {
- pub data: Vec<u8>,
+ pub data: Vec<u8>,
pub metadata_id: i32,
- pub data_hash: Vec<u8>,
+ pub data_hash: Vec<u8>,
}
-
#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[diesel(table_name = images)]
pub struct Image {
- pub id: i32,
- pub data: Vec<u8>,
+ pub id: i32,
+ pub data: Vec<u8>,
pub metadata_id: i32,
- pub data_hash: Vec<u8>,
- pub filename: String,
+ pub data_hash: Vec<u8>,
+ pub filename: String,
}
impl Image {
- pub fn create(conn: &mut PgConnection, filename: &str, data: Vec<u8>, by_user: u64) -> Result<i32> {
+ pub fn create(
+ conn: &mut PgConnection,
+ filename: &str,
+ 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 data_hash = data_hash.finalize().to_vec();
let id = images::table
.select(images::id)
@@ -145,7 +150,7 @@ impl Image {
metadata_id: metadata.id,
};
- ::diesel::insert_into(images::table)
+ diesel::insert_into(images::table)
.values(&new_image)
.returning(images::id)
.get_result(conn)
@@ -156,24 +161,23 @@ impl Image {
#[derive(Insertable, PartialEq, Debug)]
#[diesel(table_name = images)]
pub struct NewImage {
- pub data: Vec<u8>,
+ pub data: Vec<u8>,
pub metadata_id: i32,
- pub data_hash: Vec<u8>,
- pub filename: String,
+ pub data_hash: Vec<u8>,
+ pub filename: String,
}
-
#[derive(Queryable, Identifiable, PartialEq, Debug, Clone)]
#[diesel(table_name = metadata)]
pub struct Metadata {
- pub id: i32,
- pub created: NaiveDateTime,
+ pub id: i32,
+ pub created: NaiveDateTime,
pub created_by: i64,
}
impl Metadata {
pub fn create(conn: &mut PgConnection, by_user: u64) -> Result<Metadata> {
- ::diesel::insert_into(metadata::table)
+ diesel::insert_into(metadata::table)
.values(&NewMetadata {
created_by: by_user as i64,
})
@@ -182,9 +186,7 @@ impl Metadata {
}
pub fn find(conn: &mut PgConnection, id: i32) -> Result<Metadata> {
- metadata::table.find(id)
- .get_result::<Metadata>(conn)
- .map_err(Error::from)
+ metadata::table.find(id).get_result::<Metadata>(conn).map_err(Error::from)
}
}
@@ -194,21 +196,20 @@ pub struct NewMetadata {
pub created_by: i64,
}
-
#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[diesel(table_name = audit_records)]
pub struct AuditRecord {
- pub id: i32,
- pub updated: NaiveDateTime,
- pub updated_by: i64,
+ pub id: i32,
+ pub updated: NaiveDateTime,
+ pub updated_by: i64,
pub metadata_id: i32,
}
impl AuditRecord {
pub fn create(conn: &mut PgConnection, metadata: i32, by_user: u64) -> Result<AuditRecord> {
- ::diesel::insert_into(audit_records::table)
+ diesel::insert_into(audit_records::table)
.values(&NewAuditRecord {
- updated_by: by_user as i64,
+ updated_by: by_user as i64,
metadata_id: metadata,
})
.get_result::<AuditRecord>(conn)
@@ -219,52 +220,57 @@ impl AuditRecord {
#[derive(Insertable, PartialEq, Debug)]
#[diesel(table_name = audit_records)]
pub struct NewAuditRecord {
- pub updated_by: i64,
+ pub updated_by: i64,
pub metadata_id: i32,
}
#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[diesel(table_name = tombstones)]
pub struct Tombstone {
- pub id: i32,
- pub deleted: NaiveDateTime,
- pub deleted_by: i64,
+ pub id: i32,
+ pub deleted: NaiveDateTime,
+ pub deleted_by: i64,
pub metadata_id: i32,
- pub meme_id: i32,
+ pub meme_id: i32,
}
-
#[derive(Insertable, PartialEq, Debug)]
#[diesel(table_name = tombstones)]
pub struct NewTombstone {
- pub deleted_by: i64,
+ pub deleted_by: i64,
pub metadata_id: i32,
- pub meme_id: i32,
+ pub meme_id: i32,
}
#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[diesel(table_name = invocation_records)]
pub struct InvocationRecord {
- pub id: i32,
- pub user_id: i64,
+ pub id: i32,
+ pub user_id: i64,
pub message_id: i64,
- pub meme_id: i32,
- pub time: NaiveDateTime,
- pub random: bool,
+ pub meme_id: i32,
+ pub time: NaiveDateTime,
+ pub random: bool,
}
#[derive(Insertable, PartialEq, Debug)]
#[diesel(table_name = invocation_records)]
pub struct NewInvocationRecord {
- pub user_id: i64,
+ pub user_id: i64,
pub message_id: i64,
- pub meme_id: i32,
- pub random: bool,
+ pub meme_id: i32,
+ pub random: bool,
}
impl InvocationRecord {
- pub fn create(conn: &mut PgConnection, user_id: u64, message_id: u64, meme_id: i32, random: bool) -> Result<Self> {
- ::diesel::insert_into(invocation_records::table)
+ pub fn create(
+ conn: &mut PgConnection,
+ user_id: u64,
+ message_id: u64,
+ meme_id: i32,
+ random: bool,
+ ) -> Result<Self> {
+ diesel::insert_into(invocation_records::table)
.values(&NewInvocationRecord {
user_id: user_id as i64,
message_id: message_id as i64,