]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/comment_report.rs
Add diesel_async, get rid of blocking function (#2510)
[lemmy.git] / crates / db_schema / src / impls / comment_report.rs
1 use crate::{
2   newtypes::{CommentReportId, PersonId},
3   schema::comment_report::dsl::*,
4   source::comment_report::{CommentReport, CommentReportForm},
5   traits::Reportable,
6   utils::{get_conn, naive_now, DbPool},
7 };
8 use diesel::{dsl::*, result::Error, ExpressionMethods, QueryDsl};
9 use diesel_async::RunQueryDsl;
10
11 #[async_trait]
12 impl Reportable for CommentReport {
13   type Form = CommentReportForm;
14   type IdType = CommentReportId;
15   /// creates a comment report and returns it
16   ///
17   /// * `conn` - the postgres connection
18   /// * `comment_report_form` - the filled CommentReportForm to insert
19   async fn report(pool: &DbPool, comment_report_form: &CommentReportForm) -> Result<Self, Error> {
20     let conn = &mut get_conn(pool).await?;
21     insert_into(comment_report)
22       .values(comment_report_form)
23       .get_result::<Self>(conn)
24       .await
25   }
26
27   /// resolve a comment report
28   ///
29   /// * `conn` - the postgres connection
30   /// * `report_id` - the id of the report to resolve
31   /// * `by_resolver_id` - the id of the user resolving the report
32   async fn resolve(
33     pool: &DbPool,
34     report_id_: Self::IdType,
35     by_resolver_id: PersonId,
36   ) -> Result<usize, Error> {
37     let conn = &mut get_conn(pool).await?;
38     update(comment_report.find(report_id_))
39       .set((
40         resolved.eq(true),
41         resolver_id.eq(by_resolver_id),
42         updated.eq(naive_now()),
43       ))
44       .execute(conn)
45       .await
46   }
47
48   /// unresolve a comment report
49   ///
50   /// * `conn` - the postgres connection
51   /// * `report_id` - the id of the report to unresolve
52   /// * `by_resolver_id` - the id of the user unresolving the report
53   async fn unresolve(
54     pool: &DbPool,
55     report_id_: Self::IdType,
56     by_resolver_id: PersonId,
57   ) -> Result<usize, Error> {
58     let conn = &mut get_conn(pool).await?;
59     update(comment_report.find(report_id_))
60       .set((
61         resolved.eq(false),
62         resolver_id.eq(by_resolver_id),
63         updated.eq(naive_now()),
64       ))
65       .execute(conn)
66       .await
67   }
68 }