2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
2022-11-19 04:33:54 +00:00
|
|
|
schema::community_block::dsl::{community_block, community_id, person_id},
|
2021-10-16 13:33:38 +00:00
|
|
|
source::community_block::{CommunityBlock, CommunityBlockForm},
|
|
|
|
traits::Blockable,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, DbPool},
|
2021-10-16 13:33:38 +00:00
|
|
|
};
|
2022-11-19 04:33:54 +00:00
|
|
|
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2021-08-19 20:54:15 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-19 20:54:15 +00:00
|
|
|
impl Blockable for CommunityBlock {
|
|
|
|
type Form = CommunityBlockForm;
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn block(pool: &DbPool, community_block_form: &Self::Form) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-08-19 20:54:15 +00:00
|
|
|
insert_into(community_block)
|
|
|
|
.values(community_block_form)
|
|
|
|
.on_conflict((person_id, community_id))
|
|
|
|
.do_update()
|
|
|
|
.set(community_block_form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-08-19 20:54:15 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn unblock(pool: &DbPool, community_block_form: &Self::Form) -> Result<usize, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-08-19 20:54:15 +00:00
|
|
|
diesel::delete(
|
|
|
|
community_block
|
|
|
|
.filter(person_id.eq(community_block_form.person_id))
|
|
|
|
.filter(community_id.eq(community_block_form.community_id)),
|
|
|
|
)
|
|
|
|
.execute(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-08-19 20:54:15 +00:00
|
|
|
}
|
|
|
|
}
|