1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{community, mod_add_community, user_, user_alias_1},
6 community::{Community, CommunitySafe},
7 moderator::ModAddCommunity,
8 user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
13 #[derive(Debug, Serialize, Clone)]
14 pub struct ModAddCommunityView {
15 pub mod_add_community: ModAddCommunity,
16 pub moderator: UserSafe,
17 pub community: CommunitySafe,
18 pub modded_user: UserSafeAlias1,
21 type ModAddCommunityViewTuple = (ModAddCommunity, UserSafe, CommunitySafe, UserSafeAlias1);
23 impl ModAddCommunityView {
26 community_id: Option<i32>,
27 mod_user_id: Option<i32>,
30 ) -> Result<Vec<Self>, Error> {
31 let mut query = mod_add_community::table
32 .inner_join(user_::table.on(mod_add_community::mod_user_id.eq(user_::id)))
33 .inner_join(community::table)
34 .inner_join(user_alias_1::table.on(mod_add_community::other_user_id.eq(user_::id)))
36 mod_add_community::all_columns,
37 User_::safe_columns_tuple(),
38 Community::safe_columns_tuple(),
39 UserAlias1::safe_columns_tuple(),
43 if let Some(mod_user_id) = mod_user_id {
44 query = query.filter(mod_add_community::mod_user_id.eq(mod_user_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_user: a.3.to_owned(),
74 .collect::<Vec<Self>>()