2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::PersonId,
|
2021-08-19 20:54:15 +00:00
|
|
|
source::person_block::{PersonBlock, PersonBlockForm},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Blockable,
|
2021-08-19 20:54:15 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use diesel::{dsl::*, result::Error, *};
|
2021-08-19 20:54:15 +00:00
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl PersonBlock {
|
|
|
|
pub fn read(
|
2022-09-26 14:09:32 +00:00
|
|
|
conn: &mut PgConnection,
|
2021-08-19 20:54:15 +00:00
|
|
|
for_person_id: PersonId,
|
|
|
|
for_recipient_id: PersonId,
|
|
|
|
) -> Result<Self, Error> {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::schema::person_block::dsl::*;
|
2021-08-19 20:54:15 +00:00
|
|
|
person_block
|
|
|
|
.filter(person_id.eq(for_person_id))
|
|
|
|
.filter(target_id.eq(for_recipient_id))
|
|
|
|
.first::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Blockable for PersonBlock {
|
|
|
|
type Form = PersonBlockForm;
|
2022-09-26 14:09:32 +00:00
|
|
|
fn block(conn: &mut PgConnection, person_block_form: &PersonBlockForm) -> Result<Self, Error> {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::schema::person_block::dsl::*;
|
2021-08-19 20:54:15 +00:00
|
|
|
insert_into(person_block)
|
|
|
|
.values(person_block_form)
|
|
|
|
.on_conflict((person_id, target_id))
|
|
|
|
.do_update()
|
|
|
|
.set(person_block_form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
2022-09-26 14:09:32 +00:00
|
|
|
fn unblock(conn: &mut PgConnection, person_block_form: &Self::Form) -> Result<usize, Error> {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::schema::person_block::dsl::*;
|
2021-08-19 20:54:15 +00:00
|
|
|
diesel::delete(
|
|
|
|
person_block
|
|
|
|
.filter(person_id.eq(person_block_form.person_id))
|
|
|
|
.filter(target_id.eq(person_block_form.target_id)),
|
|
|
|
)
|
|
|
|
.execute(conn)
|
|
|
|
}
|
|
|
|
}
|