1 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},
13 use serde::{Deserialize, Serialize};
15 #[derive(Debug, Serialize, Deserialize, Clone)]
16 pub struct ModAddCommunityView {
17 pub mod_add_community: ModAddCommunity,
18 pub moderator: PersonSafe,
19 pub community: CommunitySafe,
20 pub modded_person: PersonSafeAlias1,
23 type ModAddCommunityViewTuple = (ModAddCommunity, PersonSafe, CommunitySafe, PersonSafeAlias1);
25 impl ModAddCommunityView {
28 community_id: Option<CommunityId>,
29 mod_person_id: Option<PersonId>,
32 ) -> Result<Vec<Self>, Error> {
33 let mut query = mod_add_community::table
34 .inner_join(person::table.on(mod_add_community::mod_person_id.eq(person::id)))
35 .inner_join(community::table)
37 person_alias_1::table.on(mod_add_community::other_person_id.eq(person_alias_1::id)),
40 mod_add_community::all_columns,
41 Person::safe_columns_tuple(),
42 Community::safe_columns_tuple(),
43 PersonAlias1::safe_columns_tuple(),
47 if let Some(mod_person_id) = mod_person_id {
48 query = query.filter(mod_add_community::mod_person_id.eq(mod_person_id));
51 if let Some(community_id) = community_id {
52 query = query.filter(mod_add_community::community_id.eq(community_id));
55 let (limit, offset) = limit_and_offset(page, limit);
60 .order_by(mod_add_community::when_.desc())
61 .load::<ModAddCommunityViewTuple>(conn)?;
63 Ok(Self::from_tuple_to_vec(res))
67 impl ViewToVec for ModAddCommunityView {
68 type DbTuple = ModAddCommunityViewTuple;
69 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
73 mod_add_community: a.0.to_owned(),
74 moderator: a.1.to_owned(),
75 community: a.2.to_owned(),
76 modded_person: a.3.to_owned(),
78 .collect::<Vec<Self>>()