1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{ToSafe, ViewToVec};
4 schema::{community, community_moderator, person},
6 community::{Community, CommunitySafe},
7 person::{Person, PersonSafe},
12 #[derive(Debug, Serialize, Clone)]
13 pub struct CommunityModeratorView {
14 pub community: CommunitySafe,
15 pub moderator: PersonSafe,
18 type CommunityModeratorViewTuple = (CommunitySafe, PersonSafe);
20 impl CommunityModeratorView {
21 pub fn for_community(conn: &PgConnection, community_id: i32) -> Result<Vec<Self>, Error> {
22 let res = community_moderator::table
23 .inner_join(community::table)
24 .inner_join(person::table)
26 Community::safe_columns_tuple(),
27 Person::safe_columns_tuple(),
29 .filter(community_moderator::community_id.eq(community_id))
30 .order_by(community_moderator::published)
31 .load::<CommunityModeratorViewTuple>(conn)?;
33 Ok(Self::from_tuple_to_vec(res))
36 pub fn for_person(conn: &PgConnection, person_id: i32) -> Result<Vec<Self>, Error> {
37 let res = community_moderator::table
38 .inner_join(community::table)
39 .inner_join(person::table)
41 Community::safe_columns_tuple(),
42 Person::safe_columns_tuple(),
44 .filter(community_moderator::person_id.eq(person_id))
45 .order_by(community_moderator::published)
46 .load::<CommunityModeratorViewTuple>(conn)?;
48 Ok(Self::from_tuple_to_vec(res))
52 impl ViewToVec for CommunityModeratorView {
53 type DbTuple = CommunityModeratorViewTuple;
54 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
58 community: a.0.to_owned(),
59 moderator: a.1.to_owned(),
61 .collect::<Vec<Self>>()