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