]> Untitled Git - lemmy.git/blob - crates/db_views_moderator/src/mod_remove_community_view.rs
Adding check for requests with no id or name, adding max limit. (#2265)
[lemmy.git] / crates / db_views_moderator / src / mod_remove_community_view.rs
1 use crate::structs::ModRemoveCommunityView;
2 use diesel::{result::Error, *};
3 use lemmy_db_schema::{
4   newtypes::PersonId,
5   schema::{community, mod_remove_community, person},
6   source::{
7     community::{Community, CommunitySafe},
8     moderator::ModRemoveCommunity,
9     person::{Person, PersonSafe},
10   },
11   traits::{ToSafe, ViewToVec},
12   utils::limit_and_offset,
13 };
14
15 type ModRemoveCommunityTuple = (ModRemoveCommunity, PersonSafe, CommunitySafe);
16
17 impl ModRemoveCommunityView {
18   pub fn list(
19     conn: &PgConnection,
20     mod_person_id: Option<PersonId>,
21     page: Option<i64>,
22     limit: Option<i64>,
23   ) -> Result<Vec<Self>, Error> {
24     let mut query = mod_remove_community::table
25       .inner_join(person::table)
26       .inner_join(community::table)
27       .select((
28         mod_remove_community::all_columns,
29         Person::safe_columns_tuple(),
30         Community::safe_columns_tuple(),
31       ))
32       .into_boxed();
33
34     if let Some(mod_person_id) = mod_person_id {
35       query = query.filter(mod_remove_community::mod_person_id.eq(mod_person_id));
36     };
37
38     let (limit, offset) = limit_and_offset(page, limit)?;
39
40     let res = query
41       .limit(limit)
42       .offset(offset)
43       .order_by(mod_remove_community::when_.desc())
44       .load::<ModRemoveCommunityTuple>(conn)?;
45
46     Ok(Self::from_tuple_to_vec(res))
47   }
48 }
49
50 impl ViewToVec for ModRemoveCommunityView {
51   type DbTuple = ModRemoveCommunityTuple;
52   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
53     items
54       .iter()
55       .map(|a| Self {
56         mod_remove_community: a.0.to_owned(),
57         moderator: a.1.to_owned(),
58         community: a.2.to_owned(),
59       })
60       .collect::<Vec<Self>>()
61   }
62 }