From 332eff26a0a358eef52c307b02b7e1ac080dd15f Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Sat, 11 May 2024 17:43:58 -0400 Subject: db: fixup build, async-await --- src/db/models.rs | 61 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 28 deletions(-) (limited to 'src/db/models.rs') diff --git a/src/db/models.rs b/src/db/models.rs index f7bbf8e..1165368 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -5,7 +5,10 @@ use diesel::{ Insertable, Queryable, }; -use diesel_async::{AsyncPgConnection, RunQueryDsl}; +use diesel_async::{ + AsyncPgConnection, + RunQueryDsl, +}; use sha1::Digest; use crate::{ @@ -27,26 +30,24 @@ pub struct Meme { impl Meme { pub async fn image(&self, conn: &mut AsyncPgConnection) -> Option> { - self.image_id - .map(|x: i32| images::table.filter(images::id.eq(x)) - .first(conn) - .await - .map_err(Error::from)) + match self.image_id { + Some(x) => { + Some(images::table.filter(images::id.eq(x)).first(conn).await.map_err(Error::from)) + }, + None => None, + } } pub async fn audio(&self, conn: &mut AsyncPgConnection) -> Option> { - self.audio_id - .map(|x: i32| audio::table.filter(audio::id.eq(x)) - .first(conn) - .await - .map_err(Error::from)) + let Some(x) = self.audio_id else { + return None; + }; + + Some(audio::table.filter(audio::id.eq(x)).first(conn).await.map_err(Error::from)) } pub async fn find(conn: &mut AsyncPgConnection, id: i32) -> Result { - memes::table.find(id) - .get_result(conn) - .await - .map_err(Error::from) + memes::table.find(id).get_result(conn).await.map_err(Error::from) } } @@ -62,7 +63,7 @@ pub struct NewMeme { impl NewMeme { pub async fn save(mut self, conn: &mut AsyncPgConnection, by_user: u64) -> Result { - let metadata = Metadata::create(conn, by_user)?; + let metadata = Metadata::create(conn, by_user).await?; self.metadata_id = metadata.id; @@ -84,7 +85,7 @@ pub struct Audio { } impl Audio { - pub fn create(conn: &mut AsyncPgConnection, data: Vec, by_user: u64) -> Result { + pub async fn create(conn: &mut AsyncPgConnection, data: Vec, by_user: u64) -> Result { let mut data_hash = ::sha1::Sha1::new(); data_hash.update(&data); let data_hash = data_hash.finalize().to_vec(); @@ -95,11 +96,11 @@ impl Audio { .get_results::(conn) .await?; - if let Some(id) = id.first() { + if let Some(id) = <[_]>::first(&id) { return Ok(*id); } - let metadata = Metadata::create(conn, by_user)?; + let metadata = Metadata::create(conn, by_user).await?; let new_audio = NewAudio { data, @@ -135,7 +136,7 @@ pub struct Image { } impl Image { - pub fn create( + pub async fn create( conn: &mut AsyncPgConnection, filename: &str, data: Vec, @@ -151,11 +152,11 @@ impl Image { .get_results::(conn) .await?; - if let Some(id) = id.first() { + if let Some(id) = <[_]>::first(&id) { return Ok(*id); } - let metadata = Metadata::create(conn, by_user)?; + let metadata = Metadata::create(conn, by_user).await?; let new_image = NewImage { data, @@ -191,7 +192,7 @@ pub struct Metadata { } impl Metadata { - pub fn create(conn: &mut AsyncPgConnection, by_user: u64) -> Result { + pub async fn create(conn: &mut AsyncPgConnection, by_user: u64) -> Result { diesel::insert_into(metadata::table) .values(&NewMetadata { created_by: by_user as i64, @@ -201,7 +202,7 @@ impl Metadata { .map_err(Error::from) } - pub fn find(conn: &mut AsyncPgConnection, id: i32) -> Result { + pub async fn find(conn: &mut AsyncPgConnection, id: i32) -> Result { metadata::table.find(id).get_result::(conn).await.map_err(Error::from) } } @@ -222,7 +223,11 @@ pub struct AuditRecord { } impl AuditRecord { - pub fn create(conn: &mut AsyncPgConnection, metadata: i32, by_user: u64) -> Result { + pub async fn create( + conn: &mut AsyncPgConnection, + metadata: i32, + by_user: u64, + ) -> Result { diesel::insert_into(audit_records::table) .values(&NewAuditRecord { updated_by: by_user as i64, @@ -280,7 +285,7 @@ pub struct NewInvocationRecord { } impl InvocationRecord { - pub fn create( + pub async fn create( conn: &mut AsyncPgConnection, user_id: u64, message_id: u64, @@ -299,7 +304,7 @@ impl InvocationRecord { .map_err(Error::from) } - pub fn last(conn: &mut AsyncPgConnection) -> Result { + pub async fn last(conn: &mut AsyncPgConnection) -> Result { invocation_records::table .order(invocation_records::time.desc()) .first(conn) @@ -307,7 +312,7 @@ impl InvocationRecord { .map_err(Error::from) } - pub fn last_n(conn: &mut AsyncPgConnection, n: usize) -> Result> { + pub async fn last_n(conn: &mut AsyncPgConnection, n: usize) -> Result> { invocation_records::table .order(invocation_records::time.desc()) .limit(n as i64) -- cgit v1.3.1