1 use crate::structs::ModRemoveCommentView;
2 use diesel::{result::Error, *};
4 newtypes::{CommunityId, PersonId},
5 schema::{comment, community, mod_remove_comment, person, person_alias_1, post},
8 community::{Community, CommunitySafe},
9 moderator::ModRemoveComment,
10 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
13 traits::{ToSafe, ViewToVec},
14 utils::limit_and_offset,
17 type ModRemoveCommentViewTuple = (
26 impl ModRemoveCommentView {
29 community_id: Option<CommunityId>,
30 mod_person_id: Option<PersonId>,
33 ) -> Result<Vec<Self>, Error> {
34 let mut query = mod_remove_comment::table
35 .inner_join(person::table)
36 .inner_join(comment::table)
37 .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
38 .inner_join(post::table.on(comment::post_id.eq(post::id)))
39 .inner_join(community::table.on(post::community_id.eq(community::id)))
41 mod_remove_comment::all_columns,
42 Person::safe_columns_tuple(),
44 PersonAlias1::safe_columns_tuple(),
46 Community::safe_columns_tuple(),
50 if let Some(community_id) = community_id {
51 query = query.filter(post::community_id.eq(community_id));
54 if let Some(mod_person_id) = mod_person_id {
55 query = query.filter(mod_remove_comment::mod_person_id.eq(mod_person_id));
58 let (limit, offset) = limit_and_offset(page, limit)?;
63 .order_by(mod_remove_comment::when_.desc())
64 .load::<ModRemoveCommentViewTuple>(conn)?;
66 Ok(Self::from_tuple_to_vec(res))
70 impl ViewToVec for ModRemoveCommentView {
71 type DbTuple = ModRemoveCommentViewTuple;
72 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
76 mod_remove_comment: a.0.to_owned(),
77 moderator: a.1.to_owned(),
78 comment: a.2.to_owned(),
79 commenter: a.3.to_owned(),
81 community: a.5.to_owned(),
83 .collect::<Vec<Self>>()