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