mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-06-02 21:49:22 +00:00
37 lines
1 KiB
Rust
37 lines
1 KiB
Rust
use crate::{
|
|
aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
|
|
diesel::BoolExpressionMethods,
|
|
newtypes::{PersonId, PostId},
|
|
schema::person_post_aggregates::dsl::{person_id, person_post_aggregates, post_id},
|
|
utils::{DbPool, GetConn},
|
|
};
|
|
use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
|
|
use diesel_async::RunQueryDsl;
|
|
|
|
impl PersonPostAggregates {
|
|
pub async fn upsert(
|
|
mut pool: &mut impl GetConn,
|
|
form: &PersonPostAggregatesForm,
|
|
) -> Result<Self, Error> {
|
|
let conn = &mut *pool.get_conn().await?;
|
|
insert_into(person_post_aggregates)
|
|
.values(form)
|
|
.on_conflict((person_id, post_id))
|
|
.do_update()
|
|
.set(form)
|
|
.get_result::<Self>(conn)
|
|
.await
|
|
}
|
|
pub async fn read(
|
|
mut pool: &mut impl GetConn,
|
|
person_id_: PersonId,
|
|
post_id_: PostId,
|
|
) -> Result<Self, Error> {
|
|
let conn = &mut *pool.get_conn().await?;
|
|
person_post_aggregates
|
|
.filter(post_id.eq(post_id_).and(person_id.eq(person_id_)))
|
|
.first::<Self>(conn)
|
|
.await
|
|
}
|
|
}
|