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

61 lines
1.4 KiB
Rust
Raw Normal View History

2021-10-16 13:33:38 +00:00
use crate::{
newtypes::{PersonId, PostReportId},
schema::post_report::dsl::{post_report, resolved, resolver_id, updated},
source::post_report::{PostReport, PostReportForm},
2021-10-16 13:33:38 +00:00
traits::Reportable,
2022-11-09 10:05:00 +00:00
utils::{get_conn, naive_now, DbPool},
2021-10-16 13:33:38 +00:00
};
use diesel::{
dsl::{insert_into, update},
result::Error,
ExpressionMethods,
QueryDsl,
};
2022-11-09 10:05:00 +00:00
use diesel_async::RunQueryDsl;
2020-12-17 03:03:03 +00:00
2022-11-09 10:05:00 +00:00
#[async_trait]
impl Reportable for PostReport {
type Form = PostReportForm;
type IdType = PostReportId;
2022-11-09 10:05:00 +00:00
async fn report(pool: &DbPool, post_report_form: &PostReportForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
2020-12-17 03:03:03 +00:00
insert_into(post_report)
.values(post_report_form)
.get_result::<Self>(conn)
2022-11-09 10:05:00 +00:00
.await
2020-12-17 03:03:03 +00:00
}
2022-11-09 10:05:00 +00:00
async fn resolve(
pool: &DbPool,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
2022-11-09 10:05:00 +00:00
let conn = &mut get_conn(pool).await?;
2020-12-17 03:03:03 +00:00
update(post_report.find(report_id))
.set((
resolved.eq(true),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(conn)
2022-11-09 10:05:00 +00:00
.await
2020-12-17 03:03:03 +00:00
}
2022-11-09 10:05:00 +00:00
async fn unresolve(
pool: &DbPool,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
2022-11-09 10:05:00 +00:00
let conn = &mut get_conn(pool).await?;
2020-12-17 03:03:03 +00:00
update(post_report.find(report_id))
.set((
resolved.eq(false),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(conn)
2022-11-09 10:05:00 +00:00
.await
2020-12-17 03:03:03 +00:00
}
}