2022-09-27 16:45:46 +00:00
|
|
|
use crate::{
|
|
|
|
aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
|
2022-11-09 10:05:00 +00:00
|
|
|
diesel::BoolExpressionMethods,
|
2022-09-27 16:45:46 +00:00
|
|
|
newtypes::{PersonId, PostId},
|
2022-11-19 04:33:54 +00:00
|
|
|
schema::person_post_aggregates::dsl::{person_id, person_post_aggregates, post_id},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, DbPool},
|
2022-09-27 16:45:46 +00:00
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
|
|
|
|
use diesel_async::RunQueryDsl;
|
2022-09-27 16:45:46 +00:00
|
|
|
|
|
|
|
impl PersonPostAggregates {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn upsert(pool: &DbPool, form: &PersonPostAggregatesForm) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-09-27 16:45:46 +00:00
|
|
|
insert_into(person_post_aggregates)
|
|
|
|
.values(form)
|
|
|
|
.on_conflict((person_id, post_id))
|
|
|
|
.do_update()
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-09-27 16:45:46 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn read(pool: &DbPool, person_id_: PersonId, post_id_: PostId) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-09-27 16:45:46 +00:00
|
|
|
person_post_aggregates
|
|
|
|
.filter(post_id.eq(post_id_).and(person_id.eq(person_id_)))
|
|
|
|
.first::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-09-27 16:45:46 +00:00
|
|
|
}
|
|
|
|
}
|