]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_block_view.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / db_views_actor / src / community_block_view.rs
1 use crate::structs::CommunityBlockView;
2 use diesel::{result::Error, *};
3 use lemmy_db_schema::{
4   newtypes::PersonId,
5   schema::{community, community_block, person},
6   source::{
7     community::{Community, CommunitySafe},
8     person::{Person, PersonSafe},
9   },
10   traits::{ToSafe, ViewToVec},
11 };
12
13 type CommunityBlockViewTuple = (PersonSafe, CommunitySafe);
14
15 impl CommunityBlockView {
16   pub fn for_person(conn: &mut PgConnection, person_id: PersonId) -> Result<Vec<Self>, Error> {
17     let res = community_block::table
18       .inner_join(person::table)
19       .inner_join(community::table)
20       .select((
21         Person::safe_columns_tuple(),
22         Community::safe_columns_tuple(),
23       ))
24       .filter(community_block::person_id.eq(person_id))
25       .order_by(community_block::published)
26       .load::<CommunityBlockViewTuple>(conn)?;
27
28     Ok(Self::from_tuple_to_vec(res))
29   }
30 }
31
32 impl ViewToVec for CommunityBlockView {
33   type DbTuple = CommunityBlockViewTuple;
34   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
35     items
36       .into_iter()
37       .map(|a| Self {
38         person: a.0,
39         community: a.1,
40       })
41       .collect::<Vec<Self>>()
42   }
43 }