diff options
Diffstat (limited to 'src/db/mod.rs')
| -rw-r--r-- | src/db/mod.rs | 75 |
1 files changed, 57 insertions, 18 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs index 87b1a8d..0a6ec9c 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -6,11 +6,13 @@ use std::{ use anyhow::anyhow; use chrono::{ - Date, DateTime, + NaiveDate, + TimeZone, Utc, }; use deadpool_postgres::{ + GenericClient, Pool as RawPgConnMgr, PoolConfig, }; @@ -30,14 +32,13 @@ use diesel_async::{ }; use serenity::FutureExt; +pub use self::models::*; +use self::schema::*; use crate::{ Error, Result, }; -pub use self::models::*; -use self::schema::*; - mod models; mod schema; @@ -177,6 +178,52 @@ pub async fn query_meme<T: AsRef<str>>( Ok(result) } +pub async fn del_memes_by_userid( + _conn: &mut AsyncPgConnection, + user_id: u64, + deleted_by: u64, +) -> Result<Vec<u64>> { + let user_id = user_id as i64; + let deleted_by = deleted_by as i64; + + let raw_conn = raw_connection().await?; + + let result = raw_conn + .query( + r#" + WITH + meme_deletes AS ( + DELETE FROM memes + WHERE metadata_id IN (SELECT id FROM metadata WHERE created_by = $1) + RETURNING id, metadata_id + ) + , image_deletes AS ( + DELETE FROM images + WHERE (SELECT COUNT(*) FROM memes WHERE memes.image_id = images.id) = 0 + RETURNING id, metadata_id + ) + , audio_deletes AS ( + DELETE FROM audio + WHERE (SELECT COUNT(*) FROM memes WHERE memes.audio_id = audio.id) = 0 + RETURNING id, metadata_id + ) + , tombstone_inserts AS ( + INSERT INTO tombstones (meme_id, metadata_id, deleted_at, deleted_by) + SELECT id, metadata_id, NOW(), $2 FROM meme_deletes + RETURNING meme_id, metadata_id + ) + + SELECT meme_id + FROM tombstone_inserts + ; + "#, + &[&user_id, &deleted_by], + ) + .await?; + + Ok(result.into_iter().map(|x| x.get::<_, i32>(0) as u64).collect()) +} + pub async fn delete_meme<T: AsRef<str>>( conn: &mut AsyncPgConnection, search: T, @@ -372,10 +419,10 @@ pub struct Stats { pub audio_meme_invocations: usize, pub random_meme_invocations: usize, - pub most_active_day: Date<Utc>, + pub most_active_day: NaiveDate, pub most_active_day_count: usize, - pub most_audio_active_day: Date<Utc>, + pub most_audio_active_day: NaiveDate, pub most_audio_active_count: usize, pub most_random_meme_user: u64, @@ -394,10 +441,7 @@ pub struct Stats { } pub async fn stats(conn: &mut AsyncPgConnection) -> Result<Stats> { - use chrono::{ - NaiveDate, - NaiveDateTime, - }; + use chrono::NaiveDateTime; use diesel::dsl::{ count, count_star, @@ -405,12 +449,7 @@ pub async fn stats(conn: &mut AsyncPgConnection) -> Result<Stats> { #[inline] fn to_utc(ndt: NaiveDateTime) -> DateTime<Utc> { - DateTime::from_utc(ndt, Utc {}) - } - - #[inline] - fn to_utc_date(nd: NaiveDate) -> Date<Utc> { - Date::from_utc(nd, Utc {}) + Utc.from_utc_datetime(&ndt) } let total_count: i64 = @@ -471,7 +510,7 @@ pub async fn stats(conn: &mut AsyncPgConnection) -> Result<Stats> { ) .await?; - let most_active_day = to_utc_date(row.get(0)); + let most_active_day = row.get(0); let most_active_day_count: i64 = row.get(1); let row = raw_conn @@ -488,7 +527,7 @@ pub async fn stats(conn: &mut AsyncPgConnection) -> Result<Stats> { ) .await?; - let most_active_audio_day = to_utc_date(row.get(0)); + let most_active_audio_day = row.get(0); let most_active_audio_day_count: i64 = row.get(1); let row = raw_conn |
