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