1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{mod_add, user_, user_alias_1},
7 user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
12 #[derive(Debug, Serialize, Clone)]
13 pub struct ModAddView {
15 pub moderator: UserSafe,
16 pub modded_user: UserSafeAlias1,
19 type ModAddViewTuple = (ModAdd, UserSafe, UserSafeAlias1);
24 mod_user_id: Option<i32>,
27 ) -> Result<Vec<Self>, Error> {
28 let mut query = mod_add::table
29 .inner_join(user_::table.on(mod_add::mod_user_id.eq(user_::id)))
30 .inner_join(user_alias_1::table.on(mod_add::other_user_id.eq(user_alias_1::id)))
33 User_::safe_columns_tuple(),
34 UserAlias1::safe_columns_tuple(),
38 if let Some(mod_user_id) = mod_user_id {
39 query = query.filter(mod_add::mod_user_id.eq(mod_user_id));
42 let (limit, offset) = limit_and_offset(page, limit);
47 .order_by(mod_add::when_.desc())
48 .load::<ModAddViewTuple>(conn)?;
50 Ok(Self::from_tuple_to_vec(res))
54 impl ViewToVec for ModAddView {
55 type DbTuple = ModAddViewTuple;
56 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
60 mod_add: a.0.to_owned(),
61 moderator: a.1.to_owned(),
62 modded_user: a.2.to_owned(),
64 .collect::<Vec<Self>>()