2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::{PersonId, PostReportId},
|
2022-11-09 10:05:00 +00:00
|
|
|
schema::post_report::dsl::*,
|
2021-10-16 13:33:38 +00:00
|
|
|
source::post_report::*,
|
|
|
|
traits::Reportable,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, naive_now, DbPool},
|
2021-10-16 13:33:38 +00:00
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel::{dsl::*, result::Error, ExpressionMethods, QueryDsl};
|
|
|
|
use diesel_async::RunQueryDsl;
|
2020-12-17 03:03:03 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Reportable for PostReport {
|
|
|
|
type Form = PostReportForm;
|
2021-09-28 10:36:17 +00:00
|
|
|
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,
|
2021-09-28 10:36:17 +00:00
|
|
|
report_id: Self::IdType,
|
2021-03-18 20:25:21 +00:00
|
|
|
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,
|
2021-09-28 10:36:17 +00:00
|
|
|
report_id: Self::IdType,
|
2021-03-18 20:25:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|