]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/post_report.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / db_schema / src / impls / post_report.rs
index f9a1eab200af82cbd9d92097034ee9b5cd1ecdae..b839d62d5d78e2a0848702e65488ff86474ad6e0 100644 (file)
@@ -1,37 +1,37 @@
 use crate::{
   newtypes::{PersonId, PostReportId},
-  source::post_report::*,
+  schema::post_report::dsl::{post_report, resolved, resolver_id, updated},
+  source::post_report::{PostReport, PostReportForm},
   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 PostReport {
   type Form = PostReportForm;
   type IdType = PostReportId;
 
-  /// creates a post report and returns it
-  ///
-  /// * `conn` - the postgres connection
-  /// * `post_report_form` - the filled CommentReportForm to insert
-  fn report(conn: &mut PgConnection, post_report_form: &PostReportForm) -> Result<Self, Error> {
-    use crate::schema::post_report::dsl::*;
+  async fn report(pool: &mut DbPool<'_>, post_report_form: &PostReportForm) -> Result<Self, Error> {
+    let conn = &mut get_conn(pool).await?;
     insert_into(post_report)
       .values(post_report_form)
       .get_result::<Self>(conn)
+      .await
   }
 
-  /// resolve a post report
-  ///
-  /// * `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: &mut PgConnection,
+  async fn resolve(
+    pool: &mut DbPool<'_>,
     report_id: Self::IdType,
     by_resolver_id: PersonId,
   ) -> Result<usize, Error> {
-    use crate::schema::post_report::dsl::*;
+    let conn = &mut get_conn(pool).await?;
     update(post_report.find(report_id))
       .set((
         resolved.eq(true),
@@ -39,19 +39,15 @@ impl Reportable for PostReport {
         updated.eq(naive_now()),
       ))
       .execute(conn)
+      .await
   }
 
-  /// resolve a post report
-  ///
-  /// * `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: &mut PgConnection,
+  async fn unresolve(
+    pool: &mut DbPool<'_>,
     report_id: Self::IdType,
     by_resolver_id: PersonId,
   ) -> Result<usize, Error> {
-    use crate::schema::post_report::dsl::*;
+    let conn = &mut get_conn(pool).await?;
     update(post_report.find(report_id))
       .set((
         resolved.eq(false),
@@ -59,5 +55,6 @@ impl Reportable for PostReport {
         updated.eq(naive_now()),
       ))
       .execute(conn)
+      .await
   }
 }