lemmy/crates/db_schema/src/impls/community_block.rs

34 lines
1.1 KiB
Rust
Raw Normal View History

2021-10-16 13:33:38 +00:00
use crate::{
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
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
2022-11-09 10:05:00 +00:00
use diesel_async::RunQueryDsl;
2022-11-09 10:05:00 +00:00
#[async_trait]
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?;
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
}
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?;
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
}
}