1 use diesel::{result::Error, *};
5 schema::{community, mod_remove_community, person},
7 community::{Community, CommunitySafe},
8 moderator::ModRemoveCommunity,
9 person::{Person, PersonSafe},
11 traits::{ToSafe, ViewToVec},
13 use serde::{Deserialize, Serialize};
15 #[derive(Debug, Serialize, Deserialize, Clone)]
16 pub struct ModRemoveCommunityView {
17 pub mod_remove_community: ModRemoveCommunity,
18 pub moderator: PersonSafe,
19 pub community: CommunitySafe,
22 type ModRemoveCommunityTuple = (ModRemoveCommunity, PersonSafe, CommunitySafe);
24 impl ModRemoveCommunityView {
27 mod_person_id: Option<PersonId>,
30 ) -> Result<Vec<Self>, Error> {
31 let mut query = mod_remove_community::table
32 .inner_join(person::table)
33 .inner_join(community::table)
35 mod_remove_community::all_columns,
36 Person::safe_columns_tuple(),
37 Community::safe_columns_tuple(),
41 if let Some(mod_person_id) = mod_person_id {
42 query = query.filter(mod_remove_community::mod_person_id.eq(mod_person_id));
45 let (limit, offset) = limit_and_offset(page, limit);
50 .order_by(mod_remove_community::when_.desc())
51 .load::<ModRemoveCommunityTuple>(conn)?;
53 Ok(Self::from_tuple_to_vec(res))
57 impl ViewToVec for ModRemoveCommunityView {
58 type DbTuple = ModRemoveCommunityTuple;
59 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
63 mod_remove_community: a.0.to_owned(),
64 moderator: a.1.to_owned(),
65 community: a.2.to_owned(),
67 .collect::<Vec<Self>>()