diff options
Diffstat (limited to 'src/db/models.rs')
| -rw-r--r-- | src/db/models.rs | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/src/db/models.rs b/src/db/models.rs index bba25a2..f7bbf8e 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -5,6 +5,7 @@ use diesel::{ Insertable, Queryable, }; +use diesel_async::{AsyncPgConnection, RunQueryDsl}; use sha1::Digest; use crate::{ @@ -25,18 +26,27 @@ pub struct Meme { } impl Meme { - pub fn image(&self, conn: &mut PgConnection) -> Option<Result<Image>> { + pub async fn image(&self, conn: &mut AsyncPgConnection) -> Option<Result<Image>> { self.image_id - .map(|x: i32| images::table.filter(images::id.eq(x)).first(conn).map_err(Error::from)) + .map(|x: i32| images::table.filter(images::id.eq(x)) + .first(conn) + .await + .map_err(Error::from)) } - pub fn audio(&self, conn: &mut PgConnection) -> Option<Result<Audio>> { + pub async fn audio(&self, conn: &mut AsyncPgConnection) -> Option<Result<Audio>> { self.audio_id - .map(|x: i32| audio::table.filter(audio::id.eq(x)).first(conn).map_err(Error::from)) + .map(|x: i32| audio::table.filter(audio::id.eq(x)) + .first(conn) + .await + .map_err(Error::from)) } - pub fn find(conn: &mut PgConnection, id: i32) -> Result<Meme> { - memes::table.find(id).get_result(conn).map_err(Error::from) + pub async fn find(conn: &mut AsyncPgConnection, id: i32) -> Result<Meme> { + memes::table.find(id) + .get_result(conn) + .await + .map_err(Error::from) } } @@ -51,7 +61,7 @@ pub struct NewMeme { } impl NewMeme { - pub fn save(mut self, conn: &mut PgConnection, by_user: u64) -> Result<Meme> { + pub async fn save(mut self, conn: &mut AsyncPgConnection, by_user: u64) -> Result<Meme> { let metadata = Metadata::create(conn, by_user)?; self.metadata_id = metadata.id; @@ -59,6 +69,7 @@ impl NewMeme { diesel::insert_into(memes::table) .values(&self) .get_result::<Meme>(conn) + .await .map_err(Error::from) } } @@ -73,7 +84,7 @@ pub struct Audio { } impl Audio { - pub fn create(conn: &mut PgConnection, data: Vec<u8>, by_user: u64) -> Result<i32> { + pub fn create(conn: &mut AsyncPgConnection, data: Vec<u8>, by_user: u64) -> Result<i32> { let mut data_hash = ::sha1::Sha1::new(); data_hash.update(&data); let data_hash = data_hash.finalize().to_vec(); @@ -81,7 +92,8 @@ impl Audio { let id = audio::table .select(audio::id) .filter(audio::data_hash.eq(&data_hash)) - .get_results::<i32>(conn)?; + .get_results::<i32>(conn) + .await?; if let Some(id) = id.first() { return Ok(*id); @@ -99,6 +111,7 @@ impl Audio { .values(&new_audio) .returning(audio::id) .get_result(conn) + .await .map_err(Error::from) } } @@ -123,7 +136,7 @@ pub struct Image { impl Image { pub fn create( - conn: &mut PgConnection, + conn: &mut AsyncPgConnection, filename: &str, data: Vec<u8>, by_user: u64, @@ -135,7 +148,8 @@ impl Image { let id = images::table .select(images::id) .filter(images::data_hash.eq(&data_hash)) - .get_results::<i32>(conn)?; + .get_results::<i32>(conn) + .await?; if let Some(id) = id.first() { return Ok(*id); @@ -154,6 +168,7 @@ impl Image { .values(&new_image) .returning(images::id) .get_result(conn) + .await .map_err(Error::from) } } @@ -176,17 +191,18 @@ pub struct Metadata { } impl Metadata { - pub fn create(conn: &mut PgConnection, by_user: u64) -> Result<Metadata> { + pub fn create(conn: &mut AsyncPgConnection, by_user: u64) -> Result<Metadata> { diesel::insert_into(metadata::table) .values(&NewMetadata { created_by: by_user as i64, }) .get_result::<Metadata>(conn) + .await .map_err(Error::from) } - pub fn find(conn: &mut PgConnection, id: i32) -> Result<Metadata> { - metadata::table.find(id).get_result::<Metadata>(conn).map_err(Error::from) + pub fn find(conn: &mut AsyncPgConnection, id: i32) -> Result<Metadata> { + metadata::table.find(id).get_result::<Metadata>(conn).await.map_err(Error::from) } } @@ -206,13 +222,14 @@ pub struct AuditRecord { } impl AuditRecord { - pub fn create(conn: &mut PgConnection, metadata: i32, by_user: u64) -> Result<AuditRecord> { + pub fn create(conn: &mut AsyncPgConnection, 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) + .await .map_err(Error::from) } } @@ -264,7 +281,7 @@ pub struct NewInvocationRecord { impl InvocationRecord { pub fn create( - conn: &mut PgConnection, + conn: &mut AsyncPgConnection, user_id: u64, message_id: u64, meme_id: i32, @@ -278,21 +295,24 @@ impl InvocationRecord { random, }) .get_result::<InvocationRecord>(conn) + .await .map_err(Error::from) } - pub fn last(conn: &mut PgConnection) -> Result<Self> { + pub fn last(conn: &mut AsyncPgConnection) -> Result<Self> { invocation_records::table .order(invocation_records::time.desc()) .first(conn) + .await .map_err(Error::from) } - pub fn last_n(conn: &mut PgConnection, n: usize) -> Result<Vec<Self>> { + pub fn last_n(conn: &mut AsyncPgConnection, n: usize) -> Result<Vec<Self>> { invocation_records::table .order(invocation_records::time.desc()) .limit(n as i64) .load(conn) + .await .map_err(Error::from) } } |
