1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{mod_ban, person, person_alias_1},
7 person::{PersonAlias1, PersonSafe, PersonSafeAlias1, Person},
12 #[derive(Debug, Serialize, Clone)]
13 pub struct ModBanView {
15 pub moderator: PersonSafe,
16 pub banned_person: PersonSafeAlias1,
19 type ModBanViewTuple = (ModBan, PersonSafe, PersonSafeAlias1);
24 mod_person_id: Option<i32>,
27 ) -> Result<Vec<Self>, Error> {
28 let mut query = mod_ban::table
29 .inner_join(person::table.on(mod_ban::mod_person_id.eq(person::id)))
30 .inner_join(person_alias_1::table.on(mod_ban::other_person_id.eq(person_alias_1::id)))
33 Person::safe_columns_tuple(),
34 PersonAlias1::safe_columns_tuple(),
38 if let Some(mod_person_id) = mod_person_id {
39 query = query.filter(mod_ban::mod_person_id.eq(mod_person_id));
42 let (limit, offset) = limit_and_offset(page, limit);
47 .order_by(mod_ban::when_.desc())
48 .load::<ModBanViewTuple>(conn)?;
50 Ok(Self::from_tuple_to_vec(res))
54 impl ViewToVec for ModBanView {
55 type DbTuple = ModBanViewTuple;
56 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
60 mod_ban: a.0.to_owned(),
61 moderator: a.1.to_owned(),
62 banned_person: a.2.to_owned(),
64 .collect::<Vec<Self>>()