]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/community_block.rs
c1f35e625dc265702fd19d9b87b8049ea0dd9623
[lemmy.git] / crates / db_schema / src / impls / community_block.rs
1 use crate::{
2   schema::community_block::dsl::{community_block, community_id, person_id},
3   source::community_block::{CommunityBlock, CommunityBlockForm},
4   traits::Blockable,
5   utils::{get_conn, DbPool},
6 };
7 use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
9
10 #[async_trait]
11 impl Blockable for CommunityBlock {
12   type Form = CommunityBlockForm;
13   async fn block(pool: &DbPool, community_block_form: &Self::Form) -> Result<Self, Error> {
14     let conn = &mut get_conn(pool).await?;
15     insert_into(community_block)
16       .values(community_block_form)
17       .on_conflict((person_id, community_id))
18       .do_update()
19       .set(community_block_form)
20       .get_result::<Self>(conn)
21       .await
22   }
23   async fn unblock(pool: &DbPool, community_block_form: &Self::Form) -> Result<usize, Error> {
24     let conn = &mut get_conn(pool).await?;
25     diesel::delete(
26       community_block
27         .filter(person_id.eq(community_block_form.person_id))
28         .filter(community_id.eq(community_block_form.community_id)),
29     )
30     .execute(conn)
31     .await
32   }
33 }