]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_person_ban_view.rs
A first pass.
[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 };
10 use serde::Serialize;
11
12 #[derive(Debug, Serialize, Clone)]
13 pub struct CommunityPersonBanView {
14   pub community: CommunitySafe,
15   pub person: PersonSafe,
16 }
17
18 impl CommunityPersonBanView {
19   pub fn get(
20     conn: &PgConnection,
21     from_person_id: i32,
22     from_community_id: i32,
23   ) -> Result<Self, Error> {
24     let (community, person) = community_person_ban::table
25       .inner_join(community::table)
26       .inner_join(person::table)
27       .select((
28         Community::safe_columns_tuple(),
29         Person::safe_columns_tuple(),
30       ))
31       .filter(community_person_ban::community_id.eq(from_community_id))
32       .filter(community_person_ban::person_id.eq(from_person_id))
33       .order_by(community_person_ban::published)
34       .first::<(CommunitySafe, PersonSafe)>(conn)?;
35
36     Ok(CommunityPersonBanView { community, person })
37   }
38 }