1 use crate::structs::ModBanFromCommunityView;
2 use diesel::{result::Error, *};
4 newtypes::{CommunityId, PersonId},
5 schema::{community, mod_ban_from_community, person, person_alias_1},
7 community::{Community, CommunitySafe},
8 moderator::ModBanFromCommunity,
9 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
11 traits::{ToSafe, ViewToVec},
12 utils::limit_and_offset,
15 type ModBanFromCommunityViewTuple = (
22 impl ModBanFromCommunityView {
25 community_id: Option<CommunityId>,
26 mod_person_id: Option<PersonId>,
29 ) -> Result<Vec<Self>, Error> {
30 let mut query = mod_ban_from_community::table
31 .inner_join(person::table.on(mod_ban_from_community::mod_person_id.eq(person::id)))
32 .inner_join(community::table)
34 person_alias_1::table.on(mod_ban_from_community::other_person_id.eq(person_alias_1::id)),
37 mod_ban_from_community::all_columns,
38 Person::safe_columns_tuple(),
39 Community::safe_columns_tuple(),
40 PersonAlias1::safe_columns_tuple(),
44 if let Some(mod_person_id) = mod_person_id {
45 query = query.filter(mod_ban_from_community::mod_person_id.eq(mod_person_id));
48 if let Some(community_id) = community_id {
49 query = query.filter(mod_ban_from_community::community_id.eq(community_id));
52 let (limit, offset) = limit_and_offset(page, limit);
57 .order_by(mod_ban_from_community::when_.desc())
58 .load::<ModBanFromCommunityViewTuple>(conn)?;
60 Ok(Self::from_tuple_to_vec(res))
64 impl ViewToVec for ModBanFromCommunityView {
65 type DbTuple = ModBanFromCommunityViewTuple;
66 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
70 mod_ban_from_community: a.0.to_owned(),
71 moderator: a.1.to_owned(),
72 community: a.2.to_owned(),
73 banned_person: a.3.to_owned(),
75 .collect::<Vec<Self>>()