]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_person_ban_view.rs
Strictly typing DB id fields. Fixes #1498
[lemmy.git] / crates / db_views_actor / src / community_person_ban_view.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_queries::ToSafe;
3 use lemmy_db_schema::{
4   schema::{community, community_person_ban, person},
5   source::{
6     community::{Community, CommunitySafe},
7     person::{Person, PersonSafe},
8   },
9   CommunityId,
10   PersonId,
11 };
12 use serde::Serialize;
13
14 #[derive(Debug, Serialize, Clone)]
15 pub struct CommunityPersonBanView {
16   pub community: CommunitySafe,
17   pub person: PersonSafe,
18 }
19
20 impl CommunityPersonBanView {
21   pub fn get(
22     conn: &PgConnection,
23     from_person_id: PersonId,
24     from_community_id: CommunityId,
25   ) -> Result<Self, Error> {
26     let (community, person) = community_person_ban::table
27       .inner_join(community::table)
28       .inner_join(person::table)
29       .select((
30         Community::safe_columns_tuple(),
31         Person::safe_columns_tuple(),
32       ))
33       .filter(community_person_ban::community_id.eq(from_community_id))
34       .filter(community_person_ban::person_id.eq(from_person_id))
35       .order_by(community_person_ban::published)
36       .first::<(CommunitySafe, PersonSafe)>(conn)?;
37
38     Ok(CommunityPersonBanView { community, person })
39   }
40 }