]> Untitled Git - lemmy.git/blob - lemmy_db_views_actor/src/community_user_ban_view.rs
Merge pull request #1328 from LemmyNet/move_views_to_diesel
[lemmy.git] / lemmy_db_views_actor / src / community_user_ban_view.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_queries::ToSafe;
3 use lemmy_db_schema::{
4   schema::{community, community_user_ban, user_},
5   source::{
6     community::{Community, CommunitySafe},
7     user::{UserSafe, User_},
8   },
9 };
10 use serde::Serialize;
11
12 #[derive(Debug, Serialize, Clone)]
13 pub struct CommunityUserBanView {
14   pub community: CommunitySafe,
15   pub user: UserSafe,
16 }
17
18 impl CommunityUserBanView {
19   pub fn get(
20     conn: &PgConnection,
21     from_user_id: i32,
22     from_community_id: i32,
23   ) -> Result<Self, Error> {
24     let (community, user) = community_user_ban::table
25       .inner_join(community::table)
26       .inner_join(user_::table)
27       .select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
28       .filter(community_user_ban::community_id.eq(from_community_id))
29       .filter(community_user_ban::user_id.eq(from_user_id))
30       .order_by(community_user_ban::published)
31       .first::<(CommunitySafe, UserSafe)>(conn)?;
32
33     Ok(CommunityUserBanView { community, user })
34   }
35 }