]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/comment_report.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / db_schema / src / impls / comment_report.rs
index 56e917e028f0135effae2521ef156ff9fc20f8d8..ff93915e1c0c7d732f730db733055303088c710f 100644 (file)
@@ -1,11 +1,19 @@
 use crate::{
   newtypes::{CommentReportId, PersonId},
+  schema::comment_report::dsl::{comment_report, resolved, resolver_id, updated},
   source::comment_report::{CommentReport, CommentReportForm},
   traits::Reportable,
-  utils::naive_now,
+  utils::{get_conn, naive_now, DbPool},
 };
-use diesel::{dsl::*, result::Error, *};
+use diesel::{
+  dsl::{insert_into, update},
+  result::Error,
+  ExpressionMethods,
+  QueryDsl,
+};
+use diesel_async::RunQueryDsl;
 
+#[async_trait]
 impl Reportable for CommentReport {
   type Form = CommentReportForm;
   type IdType = CommentReportId;
@@ -13,11 +21,15 @@ impl Reportable for CommentReport {
   ///
   /// * `conn` - the postgres connection
   /// * `comment_report_form` - the filled CommentReportForm to insert
-  fn report(conn: &PgConnection, comment_report_form: &CommentReportForm) -> Result<Self, Error> {
-    use crate::schema::comment_report::dsl::*;
+  async fn report(
+    pool: &mut DbPool<'_>,
+    comment_report_form: &CommentReportForm,
+  ) -> Result<Self, Error> {
+    let conn = &mut get_conn(pool).await?;
     insert_into(comment_report)
       .values(comment_report_form)
       .get_result::<Self>(conn)
+      .await
   }
 
   /// resolve a comment report
@@ -25,19 +37,20 @@ impl Reportable for CommentReport {
   /// * `conn` - the postgres connection
   /// * `report_id` - the id of the report to resolve
   /// * `by_resolver_id` - the id of the user resolving the report
-  fn resolve(
-    conn: &PgConnection,
-    report_id: Self::IdType,
+  async fn resolve(
+    pool: &mut DbPool<'_>,
+    report_id_: Self::IdType,
     by_resolver_id: PersonId,
   ) -> Result<usize, Error> {
-    use crate::schema::comment_report::dsl::*;
-    update(comment_report.find(report_id))
+    let conn = &mut get_conn(pool).await?;
+    update(comment_report.find(report_id_))
       .set((
         resolved.eq(true),
         resolver_id.eq(by_resolver_id),
         updated.eq(naive_now()),
       ))
       .execute(conn)
+      .await
   }
 
   /// unresolve a comment report
@@ -45,18 +58,19 @@ impl Reportable for CommentReport {
   /// * `conn` - the postgres connection
   /// * `report_id` - the id of the report to unresolve
   /// * `by_resolver_id` - the id of the user unresolving the report
-  fn unresolve(
-    conn: &PgConnection,
-    report_id: Self::IdType,
+  async fn unresolve(
+    pool: &mut DbPool<'_>,
+    report_id_: Self::IdType,
     by_resolver_id: PersonId,
   ) -> Result<usize, Error> {
-    use crate::schema::comment_report::dsl::*;
-    update(comment_report.find(report_id))
+    let conn = &mut get_conn(pool).await?;
+    update(comment_report.find(report_id_))
       .set((
         resolved.eq(false),
         resolver_id.eq(by_resolver_id),
         updated.eq(naive_now()),
       ))
       .execute(conn)
+      .await
   }
 }