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::{PersonAlias1, PersonSafe, PersonSafeAlias1, Person},
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)
34 .inner_join(person_alias_1::table.on(mod_add_community::other_person_id.eq(person_alias_1::id)))
36 mod_add_community::all_columns,
37 Person::safe_columns_tuple(),
38 Community::safe_columns_tuple(),
39 PersonAlias1::safe_columns_tuple(),
43 if let Some(mod_person_id) = mod_person_id {
44 query = query.filter(mod_add_community::mod_person_id.eq(mod_person_id));
47 if let Some(community_id) = community_id {
48 query = query.filter(mod_add_community::community_id.eq(community_id));
51 let (limit, offset) = limit_and_offset(page, limit);
56 .order_by(mod_add_community::when_.desc())
57 .load::<ModAddCommunityViewTuple>(conn)?;
59 Ok(Self::from_tuple_to_vec(res))
63 impl ViewToVec for ModAddCommunityView {
64 type DbTuple = ModAddCommunityViewTuple;
65 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
69 mod_add_community: a.0.to_owned(),
70 moderator: a.1.to_owned(),
71 community: a.2.to_owned(),
72 modded_person: a.3.to_owned(),
74 .collect::<Vec<Self>>()