]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/comment_report.rs
87b9360628cab3664d405799b07b938ac5ce2aed
[lemmy.git] / crates / db_queries / src / source / comment_report.rs
1 use crate::Reportable;
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{
4   naive_now,
5   source::comment_report::{CommentReport, CommentReportForm},
6   PersonId,
7 };
8
9 impl Reportable<CommentReportForm> for CommentReport {
10   /// creates a comment report and returns it
11   ///
12   /// * `conn` - the postgres connection
13   /// * `comment_report_form` - the filled CommentReportForm to insert
14   fn report(conn: &PgConnection, comment_report_form: &CommentReportForm) -> Result<Self, Error> {
15     use lemmy_db_schema::schema::comment_report::dsl::*;
16     insert_into(comment_report)
17       .values(comment_report_form)
18       .get_result::<Self>(conn)
19   }
20
21   /// resolve a comment report
22   ///
23   /// * `conn` - the postgres connection
24   /// * `report_id` - the id of the report to resolve
25   /// * `by_resolver_id` - the id of the user resolving the report
26   fn resolve(
27     conn: &PgConnection,
28     report_id: i32,
29     by_resolver_id: PersonId,
30   ) -> Result<usize, Error> {
31     use lemmy_db_schema::schema::comment_report::dsl::*;
32     update(comment_report.find(report_id))
33       .set((
34         resolved.eq(true),
35         resolver_id.eq(by_resolver_id),
36         updated.eq(naive_now()),
37       ))
38       .execute(conn)
39   }
40
41   /// unresolve a comment report
42   ///
43   /// * `conn` - the postgres connection
44   /// * `report_id` - the id of the report to unresolve
45   /// * `by_resolver_id` - the id of the user unresolving the report
46   fn unresolve(
47     conn: &PgConnection,
48     report_id: i32,
49     by_resolver_id: PersonId,
50   ) -> Result<usize, Error> {
51     use lemmy_db_schema::schema::comment_report::dsl::*;
52     update(comment_report.find(report_id))
53       .set((
54         resolved.eq(false),
55         resolver_id.eq(by_resolver_id),
56         updated.eq(naive_now()),
57       ))
58       .execute(conn)
59   }
60 }