]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/aggregates/person_post_aggregates.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / db_schema / src / aggregates / person_post_aggregates.rs
index 2d268d4f007774b826df800a0353a18cd555d65b..1cbaa242215b4cd1df4c8a010171442ab0e80a95 100644 (file)
@@ -1,27 +1,36 @@
 use crate::{
   aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
+  diesel::BoolExpressionMethods,
   newtypes::{PersonId, PostId},
+  schema::person_post_aggregates::dsl::{person_id, person_post_aggregates, post_id},
+  utils::{get_conn, DbPool},
 };
-use diesel::{result::Error, *};
+use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
+use diesel_async::RunQueryDsl;
 
 impl PersonPostAggregates {
-  pub fn upsert(conn: &mut PgConnection, form: &PersonPostAggregatesForm) -> Result<Self, Error> {
-    use crate::schema::person_post_aggregates::dsl::*;
+  pub async fn upsert(
+    pool: &mut DbPool<'_>,
+    form: &PersonPostAggregatesForm,
+  ) -> Result<Self, Error> {
+    let conn = &mut get_conn(pool).await?;
     insert_into(person_post_aggregates)
       .values(form)
       .on_conflict((person_id, post_id))
       .do_update()
       .set(form)
       .get_result::<Self>(conn)
+      .await
   }
-  pub fn read(
-    conn: &mut PgConnection,
+  pub async fn read(
+    pool: &mut DbPool<'_>,
     person_id_: PersonId,
     post_id_: PostId,
   ) -> Result<Self, Error> {
-    use crate::schema::person_post_aggregates::dsl::*;
+    let conn = &mut get_conn(pool).await?;
     person_post_aggregates
       .filter(post_id.eq(post_id_).and(person_id.eq(person_id_)))
       .first::<Self>(conn)
+      .await
   }
 }