1 use crate::structs::ModLockPostView;
2 use diesel::{result::Error, *};
4 newtypes::{CommunityId, PersonId},
5 schema::{community, mod_lock_post, person, post},
7 community::{Community, CommunitySafe},
8 moderator::ModLockPost,
9 person::{Person, PersonSafe},
12 traits::{ToSafe, ViewToVec},
13 utils::limit_and_offset,
16 type ModLockPostViewTuple = (ModLockPost, PersonSafe, Post, CommunitySafe);
18 impl ModLockPostView {
21 community_id: Option<CommunityId>,
22 mod_person_id: Option<PersonId>,
25 ) -> Result<Vec<Self>, Error> {
26 let mut query = mod_lock_post::table
27 .inner_join(person::table)
28 .inner_join(post::table)
29 .inner_join(community::table.on(post::community_id.eq(community::id)))
31 mod_lock_post::all_columns,
32 Person::safe_columns_tuple(),
34 Community::safe_columns_tuple(),
38 if let Some(community_id) = community_id {
39 query = query.filter(post::community_id.eq(community_id));
42 if let Some(mod_person_id) = mod_person_id {
43 query = query.filter(mod_lock_post::mod_person_id.eq(mod_person_id));
46 let (limit, offset) = limit_and_offset(page, limit)?;
51 .order_by(mod_lock_post::when_.desc())
52 .load::<ModLockPostViewTuple>(conn)?;
54 Ok(Self::from_tuple_to_vec(res))
58 impl ViewToVec for ModLockPostView {
59 type DbTuple = ModLockPostViewTuple;
60 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
69 .collect::<Vec<Self>>()