1 use crate::structs::PersonBlockView;
2 use diesel::{result::Error, ExpressionMethods, QueryDsl};
3 use diesel_async::RunQueryDsl;
6 schema::{person, person_block},
7 source::person::{Person, PersonSafe},
8 traits::{ToSafe, ViewToVec},
9 utils::{get_conn, DbPool},
12 type PersonBlockViewTuple = (PersonSafe, PersonSafe);
14 impl PersonBlockView {
15 pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result<Vec<Self>, Error> {
16 let conn = &mut get_conn(pool).await?;
17 let person_alias_1 = diesel::alias!(person as person1);
19 let res = person_block::table
20 .inner_join(person::table)
21 .inner_join(person_alias_1)
23 Person::safe_columns_tuple(),
24 person_alias_1.fields(Person::safe_columns_tuple()),
26 .filter(person_block::person_id.eq(person_id))
27 .filter(person_alias_1.field(person::deleted).eq(false))
28 .order_by(person_block::published)
29 .load::<PersonBlockViewTuple>(conn)
32 Ok(Self::from_tuple_to_vec(res))
36 impl ViewToVec for PersonBlockView {
37 type DbTuple = PersonBlockViewTuple;
38 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
45 .collect::<Vec<Self>>()