2 aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
3 diesel::BoolExpressionMethods,
4 newtypes::{PersonId, PostId},
5 schema::person_post_aggregates::dsl::*,
6 utils::{get_conn, DbPool},
8 use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
9 use diesel_async::RunQueryDsl;
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)
16 .on_conflict((person_id, post_id))
19 .get_result::<Self>(conn)
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_)))