1 use crate::structs::CommunityFollowerView;
2 use diesel::{result::Error, *};
4 newtypes::{CommunityId, PersonId},
5 schema::{community, community_follower, person},
7 community::{Community, CommunitySafe},
8 person::{Person, PersonSafe},
10 traits::{ToSafe, ViewToVec},
13 type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe);
15 impl CommunityFollowerView {
17 conn: &mut PgConnection,
18 community_id: CommunityId,
19 ) -> Result<Vec<Self>, Error> {
20 let res = community_follower::table
21 .inner_join(community::table)
22 .inner_join(person::table)
24 Community::safe_columns_tuple(),
25 Person::safe_columns_tuple(),
27 .filter(community_follower::community_id.eq(community_id))
28 .order_by(community::title)
29 .load::<CommunityFollowerViewTuple>(conn)?;
31 Ok(Self::from_tuple_to_vec(res))
34 pub fn for_person(conn: &mut PgConnection, person_id: PersonId) -> Result<Vec<Self>, Error> {
35 let res = community_follower::table
36 .inner_join(community::table)
37 .inner_join(person::table)
39 Community::safe_columns_tuple(),
40 Person::safe_columns_tuple(),
42 .filter(community_follower::person_id.eq(person_id))
43 .filter(community::deleted.eq(false))
44 .filter(community::removed.eq(false))
45 .order_by(community::title)
46 .load::<CommunityFollowerViewTuple>(conn)?;
48 Ok(Self::from_tuple_to_vec(res))
52 impl ViewToVec for CommunityFollowerView {
53 type DbTuple = CommunityFollowerViewTuple;
54 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
61 .collect::<Vec<Self>>()