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 {
16 pub fn for_community(conn: &PgConnection, community_id: CommunityId) -> Result<Vec<Self>, Error> {
17 let res = community_follower::table
18 .inner_join(community::table)
19 .inner_join(person::table)
21 Community::safe_columns_tuple(),
22 Person::safe_columns_tuple(),
24 .filter(community_follower::community_id.eq(community_id))
25 .order_by(community::title)
26 .load::<CommunityFollowerViewTuple>(conn)?;
28 Ok(Self::from_tuple_to_vec(res))
31 pub fn for_person(conn: &PgConnection, person_id: PersonId) -> Result<Vec<Self>, Error> {
32 let res = community_follower::table
33 .inner_join(community::table)
34 .inner_join(person::table)
36 Community::safe_columns_tuple(),
37 Person::safe_columns_tuple(),
39 .filter(community_follower::person_id.eq(person_id))
40 .order_by(community::title)
41 .load::<CommunityFollowerViewTuple>(conn)?;
43 Ok(Self::from_tuple_to_vec(res))
47 impl ViewToVec for CommunityFollowerView {
48 type DbTuple = CommunityFollowerViewTuple;
49 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
56 .collect::<Vec<Self>>()