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