1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{mod_add, person, person_alias_1},
7 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
11 use serde::{Deserialize, Serialize};
13 #[derive(Debug, Serialize, Deserialize, Clone)]
14 pub struct ModAddView {
16 pub moderator: PersonSafe,
17 pub modded_person: PersonSafeAlias1,
20 type ModAddViewTuple = (ModAdd, PersonSafe, PersonSafeAlias1);
25 mod_person_id: Option<PersonId>,
28 ) -> Result<Vec<Self>, Error> {
29 let mut query = mod_add::table
30 .inner_join(person::table.on(mod_add::mod_person_id.eq(person::id)))
31 .inner_join(person_alias_1::table.on(mod_add::other_person_id.eq(person_alias_1::id)))
34 Person::safe_columns_tuple(),
35 PersonAlias1::safe_columns_tuple(),
39 if let Some(mod_person_id) = mod_person_id {
40 query = query.filter(mod_add::mod_person_id.eq(mod_person_id));
43 let (limit, offset) = limit_and_offset(page, limit);
48 .order_by(mod_add::when_.desc())
49 .load::<ModAddViewTuple>(conn)?;
51 Ok(Self::from_tuple_to_vec(res))
55 impl ViewToVec for ModAddView {
56 type DbTuple = ModAddViewTuple;
57 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
61 mod_add: a.0.to_owned(),
62 moderator: a.1.to_owned(),
63 modded_person: a.2.to_owned(),
65 .collect::<Vec<Self>>()