1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{community, mod_add_community, person, person_alias_1},
6 community::{Community, CommunitySafe},
7 moderator::ModAddCommunity,
8 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
13 #[derive(Debug, Serialize, Clone)]
14 pub struct ModAddCommunityView {
15 pub mod_add_community: ModAddCommunity,
16 pub moderator: PersonSafe,
17 pub community: CommunitySafe,
18 pub modded_person: PersonSafeAlias1,
21 type ModAddCommunityViewTuple = (ModAddCommunity, PersonSafe, CommunitySafe, PersonSafeAlias1);
23 impl ModAddCommunityView {
26 community_id: Option<i32>,
27 mod_person_id: Option<i32>,
30 ) -> Result<Vec<Self>, Error> {
31 let mut query = mod_add_community::table
32 .inner_join(person::table.on(mod_add_community::mod_person_id.eq(person::id)))
33 .inner_join(community::table)
35 person_alias_1::table.on(mod_add_community::other_person_id.eq(person_alias_1::id)),
38 mod_add_community::all_columns,
39 Person::safe_columns_tuple(),
40 Community::safe_columns_tuple(),
41 PersonAlias1::safe_columns_tuple(),
45 if let Some(mod_person_id) = mod_person_id {
46 query = query.filter(mod_add_community::mod_person_id.eq(mod_person_id));
49 if let Some(community_id) = community_id {
50 query = query.filter(mod_add_community::community_id.eq(community_id));
53 let (limit, offset) = limit_and_offset(page, limit);
58 .order_by(mod_add_community::when_.desc())
59 .load::<ModAddCommunityViewTuple>(conn)?;
61 Ok(Self::from_tuple_to_vec(res))
65 impl ViewToVec for ModAddCommunityView {
66 type DbTuple = ModAddCommunityViewTuple;
67 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
71 mod_add_community: a.0.to_owned(),
72 moderator: a.1.to_owned(),
73 community: a.2.to_owned(),
74 modded_person: a.3.to_owned(),
76 .collect::<Vec<Self>>()