1 use crate::structs::ModAddCommunityView;
2 use diesel::{result::Error, *};
4 newtypes::{CommunityId, PersonId},
5 schema::{community, mod_add_community, person, person_alias_1},
7 community::{Community, CommunitySafe},
8 moderator::ModAddCommunity,
9 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
11 traits::{ToSafe, ViewToVec},
12 utils::limit_and_offset,
15 type ModAddCommunityViewTuple = (ModAddCommunity, PersonSafe, CommunitySafe, PersonSafeAlias1);
17 impl ModAddCommunityView {
20 community_id: Option<CommunityId>,
21 mod_person_id: Option<PersonId>,
24 ) -> Result<Vec<Self>, Error> {
25 let mut query = mod_add_community::table
26 .inner_join(person::table.on(mod_add_community::mod_person_id.eq(person::id)))
27 .inner_join(community::table)
29 person_alias_1::table.on(mod_add_community::other_person_id.eq(person_alias_1::id)),
32 mod_add_community::all_columns,
33 Person::safe_columns_tuple(),
34 Community::safe_columns_tuple(),
35 PersonAlias1::safe_columns_tuple(),
39 if let Some(mod_person_id) = mod_person_id {
40 query = query.filter(mod_add_community::mod_person_id.eq(mod_person_id));
43 if let Some(community_id) = community_id {
44 query = query.filter(mod_add_community::community_id.eq(community_id));
47 let (limit, offset) = limit_and_offset(page, limit)?;
52 .order_by(mod_add_community::when_.desc())
53 .load::<ModAddCommunityViewTuple>(conn)?;
55 Ok(Self::from_tuple_to_vec(res))
59 impl ViewToVec for ModAddCommunityView {
60 type DbTuple = ModAddCommunityViewTuple;
61 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
65 mod_add_community: a.0.to_owned(),
66 moderator: a.1.to_owned(),
67 community: a.2.to_owned(),
68 modded_person: a.3.to_owned(),
70 .collect::<Vec<Self>>()