]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/community_block.rs
Make functions work with both connection and pool (#3420)
[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: &mut 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(
24     pool: &mut DbPool<'_>,
25     community_block_form: &Self::Form,
26   ) -> Result<usize, Error> {
27     let conn = &mut get_conn(pool).await?;
28     diesel::delete(
29       community_block
30         .filter(person_id.eq(community_block_form.person_id))
31         .filter(community_id.eq(community_block_form.community_id)),
32     )
33     .execute(conn)
34     .await
35   }
36 }