1 use crate::structs::ModRemovePostView;
2 use diesel::{result::Error, *};
4 newtypes::{CommunityId, PersonId},
5 schema::{community, mod_remove_post, person, post},
7 community::{Community, CommunitySafe},
8 moderator::ModRemovePost,
9 person::{Person, PersonSafe},
12 traits::{ToSafe, ViewToVec},
13 utils::limit_and_offset,
16 type ModRemovePostViewTuple = (ModRemovePost, PersonSafe, Post, CommunitySafe);
18 impl ModRemovePostView {
21 community_id: Option<CommunityId>,
22 mod_person_id: Option<PersonId>,
25 ) -> Result<Vec<Self>, Error> {
26 let mut query = mod_remove_post::table
27 .inner_join(person::table)
28 .inner_join(post::table)
29 .inner_join(community::table.on(post::community_id.eq(community::id)))
31 mod_remove_post::all_columns,
32 Person::safe_columns_tuple(),
34 Community::safe_columns_tuple(),
38 if let Some(community_id) = community_id {
39 query = query.filter(post::community_id.eq(community_id));
42 if let Some(mod_person_id) = mod_person_id {
43 query = query.filter(mod_remove_post::mod_person_id.eq(mod_person_id));
46 let (limit, offset) = limit_and_offset(page, limit);
51 .order_by(mod_remove_post::when_.desc())
52 .load::<ModRemovePostViewTuple>(conn)?;
54 Ok(Self::from_tuple_to_vec(res))
58 impl ViewToVec for ModRemovePostView {
59 type DbTuple = ModRemovePostViewTuple;
60 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
64 mod_remove_post: a.0.to_owned(),
65 moderator: a.1.to_owned(),
67 community: a.3.to_owned(),
69 .collect::<Vec<Self>>()