]> Untitled Git - lemmy.git/blob - lemmy_db_queries/src/source/comment_report.rs
Merge pull request #1328 from LemmyNet/move_views_to_diesel
[lemmy.git] / lemmy_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 };
7
8 impl Reportable<CommentReportForm> for CommentReport {
9   /// creates a comment report and returns it
10   ///
11   /// * `conn` - the postgres connection
12   /// * `comment_report_form` - the filled CommentReportForm to insert
13   fn report(conn: &PgConnection, comment_report_form: &CommentReportForm) -> Result<Self, Error> {
14     use lemmy_db_schema::schema::comment_report::dsl::*;
15     insert_into(comment_report)
16       .values(comment_report_form)
17       .get_result::<Self>(conn)
18   }
19
20   /// resolve a comment report
21   ///
22   /// * `conn` - the postgres connection
23   /// * `report_id` - the id of the report to resolve
24   /// * `by_resolver_id` - the id of the user resolving the report
25   fn resolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result<usize, Error> {
26     use lemmy_db_schema::schema::comment_report::dsl::*;
27     update(comment_report.find(report_id))
28       .set((
29         resolved.eq(true),
30         resolver_id.eq(by_resolver_id),
31         updated.eq(naive_now()),
32       ))
33       .execute(conn)
34   }
35
36   /// unresolve a comment report
37   ///
38   /// * `conn` - the postgres connection
39   /// * `report_id` - the id of the report to unresolve
40   /// * `by_resolver_id` - the id of the user unresolving the report
41   fn unresolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result<usize, Error> {
42     use lemmy_db_schema::schema::comment_report::dsl::*;
43     update(comment_report.find(report_id))
44       .set((
45         resolved.eq(false),
46         resolver_id.eq(by_resolver_id),
47         updated.eq(naive_now()),
48       ))
49       .execute(conn)
50   }
51 }