]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/person_post_aggregates.rs
Add diesel_async, get rid of blocking function (#2510)
[lemmy.git] / crates / db_schema / src / aggregates / person_post_aggregates.rs
1 use crate::{
2   aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
3   diesel::BoolExpressionMethods,
4   newtypes::{PersonId, PostId},
5   schema::person_post_aggregates::dsl::*,
6   utils::{get_conn, DbPool},
7 };
8 use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
9 use diesel_async::RunQueryDsl;
10
11 impl PersonPostAggregates {
12   pub async fn upsert(pool: &DbPool, form: &PersonPostAggregatesForm) -> Result<Self, Error> {
13     let conn = &mut get_conn(pool).await?;
14     insert_into(person_post_aggregates)
15       .values(form)
16       .on_conflict((person_id, post_id))
17       .do_update()
18       .set(form)
19       .get_result::<Self>(conn)
20       .await
21   }
22   pub async fn read(pool: &DbPool, person_id_: PersonId, post_id_: PostId) -> Result<Self, Error> {
23     let conn = &mut get_conn(pool).await?;
24     person_post_aggregates
25       .filter(post_id.eq(post_id_).and(person_id.eq(person_id_)))
26       .first::<Self>(conn)
27       .await
28   }
29 }