]> Untitled Git - lemmy.git/blob - lemmy_db_views_moderator/src/mod_remove_comment_view.rs
Merge pull request #1328 from LemmyNet/move_views_to_diesel
[lemmy.git] / lemmy_db_views_moderator / src / mod_remove_comment_view.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
3 use lemmy_db_schema::{
4   schema::{comment, community, mod_remove_comment, post, user_, user_alias_1},
5   source::{
6     comment::Comment,
7     community::{Community, CommunitySafe},
8     moderator::ModRemoveComment,
9     post::Post,
10     user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
11   },
12 };
13 use serde::Serialize;
14
15 #[derive(Debug, Serialize, Clone)]
16 pub struct ModRemoveCommentView {
17   pub mod_remove_comment: ModRemoveComment,
18   pub moderator: UserSafe,
19   pub comment: Comment,
20   pub commenter: UserSafeAlias1,
21   pub post: Post,
22   pub community: CommunitySafe,
23 }
24
25 type ModRemoveCommentViewTuple = (
26   ModRemoveComment,
27   UserSafe,
28   Comment,
29   UserSafeAlias1,
30   Post,
31   CommunitySafe,
32 );
33
34 impl ModRemoveCommentView {
35   pub fn list(
36     conn: &PgConnection,
37     community_id: Option<i32>,
38     mod_user_id: Option<i32>,
39     page: Option<i64>,
40     limit: Option<i64>,
41   ) -> Result<Vec<Self>, Error> {
42     let mut query = mod_remove_comment::table
43       .inner_join(user_::table)
44       .inner_join(comment::table)
45       .inner_join(user_alias_1::table.on(comment::creator_id.eq(user_alias_1::id)))
46       .inner_join(post::table.on(comment::post_id.eq(post::id)))
47       .inner_join(community::table.on(post::community_id.eq(community::id)))
48       .select((
49         mod_remove_comment::all_columns,
50         User_::safe_columns_tuple(),
51         comment::all_columns,
52         UserAlias1::safe_columns_tuple(),
53         post::all_columns,
54         Community::safe_columns_tuple(),
55       ))
56       .into_boxed();
57
58     if let Some(community_id) = community_id {
59       query = query.filter(post::community_id.eq(community_id));
60     };
61
62     if let Some(mod_user_id) = mod_user_id {
63       query = query.filter(mod_remove_comment::mod_user_id.eq(mod_user_id));
64     };
65
66     let (limit, offset) = limit_and_offset(page, limit);
67
68     let res = query
69       .limit(limit)
70       .offset(offset)
71       .order_by(mod_remove_comment::when_.desc())
72       .load::<ModRemoveCommentViewTuple>(conn)?;
73
74     Ok(Self::from_tuple_to_vec(res))
75   }
76 }
77
78 impl ViewToVec for ModRemoveCommentView {
79   type DbTuple = ModRemoveCommentViewTuple;
80   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
81     items
82       .iter()
83       .map(|a| Self {
84         mod_remove_comment: a.0.to_owned(),
85         moderator: a.1.to_owned(),
86         comment: a.2.to_owned(),
87         commenter: a.3.to_owned(),
88         post: a.4.to_owned(),
89         community: a.5.to_owned(),
90       })
91       .collect::<Vec<Self>>()
92   }
93 }