1 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},
14 use serde::{Deserialize, Serialize};
16 #[derive(Debug, Serialize, Deserialize, Clone)]
17 pub struct ModLockPostView {
18 pub mod_lock_post: ModLockPost,
19 pub moderator: PersonSafe,
21 pub community: CommunitySafe,
24 type ModLockPostViewTuple = (ModLockPost, PersonSafe, Post, CommunitySafe);
26 impl ModLockPostView {
29 community_id: Option<CommunityId>,
30 mod_person_id: Option<PersonId>,
33 ) -> Result<Vec<Self>, Error> {
34 let mut query = mod_lock_post::table
35 .inner_join(person::table)
36 .inner_join(post::table)
37 .inner_join(community::table.on(post::community_id.eq(community::id)))
39 mod_lock_post::all_columns,
40 Person::safe_columns_tuple(),
42 Community::safe_columns_tuple(),
46 if let Some(community_id) = community_id {
47 query = query.filter(post::community_id.eq(community_id));
50 if let Some(mod_person_id) = mod_person_id {
51 query = query.filter(mod_lock_post::mod_person_id.eq(mod_person_id));
54 let (limit, offset) = limit_and_offset(page, limit);
59 .order_by(mod_lock_post::when_.desc())
60 .load::<ModLockPostViewTuple>(conn)?;
62 Ok(Self::from_tuple_to_vec(res))
66 impl ViewToVec for ModLockPostView {
67 type DbTuple = ModLockPostViewTuple;
68 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
72 mod_lock_post: a.0.to_owned(),
73 moderator: a.1.to_owned(),
75 community: a.3.to_owned(),
77 .collect::<Vec<Self>>()