newtypes::PrivateMessageReportId,
schema::{person, private_message, private_message_report},
source::{
- person::{Person, PersonSafe},
+ person::Person,
private_message::PrivateMessage,
private_message_report::PrivateMessageReport,
},
- traits::{ToSafe, ViewToVec},
+ traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool},
};
use typed_builder::TypedBuilder;
type PrivateMessageReportViewTuple = (
PrivateMessageReport,
PrivateMessage,
- PersonSafe,
- PersonSafe,
- Option<PersonSafe>,
+ Person,
+ Person,
+ Option<Person>,
);
impl PrivateMessageReportView {
/// returns the PrivateMessageReportView for the provided report_id
///
/// * `report_id` - the report id to obtain
- pub async fn read(pool: &DbPool, report_id: PrivateMessageReportId) -> Result<Self, Error> {
+ pub async fn read(
+ pool: &mut DbPool<'_>,
+ report_id: PrivateMessageReportId,
+ ) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
.select((
private_message_report::all_columns,
private_message::all_columns,
- Person::safe_columns_tuple(),
- person_alias_1.fields(Person::safe_columns_tuple()),
- person_alias_2
- .fields(Person::safe_columns_tuple())
- .nullable(),
+ person::all_columns,
+ person_alias_1.fields(person::all_columns),
+ person_alias_2.fields(person::all_columns).nullable(),
))
.first::<PrivateMessageReportViewTuple>(conn)
.await?;
}
/// Returns the current unresolved post report count for the communities you mod
- pub async fn get_report_count(pool: &DbPool) -> Result<i64, Error> {
+ pub async fn get_report_count(pool: &mut DbPool<'_>) -> Result<i64, Error> {
use diesel::dsl::count;
let conn = &mut get_conn(pool).await?;
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
-pub struct PrivateMessageReportQuery<'a> {
+pub struct PrivateMessageReportQuery<'a, 'b: 'a> {
#[builder(!default)]
- pool: &'a DbPool,
+ pool: &'a mut DbPool<'b>,
page: Option<i64>,
limit: Option<i64>,
unresolved_only: Option<bool>,
}
-impl<'a> PrivateMessageReportQuery<'a> {
+impl<'a, 'b: 'a> PrivateMessageReportQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<PrivateMessageReportView>, Error> {
let conn = &mut get_conn(self.pool).await?;
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
.select((
private_message_report::all_columns,
private_message::all_columns,
- Person::safe_columns_tuple(),
- person_alias_1.fields(Person::safe_columns_tuple()),
- person_alias_2
- .fields(Person::safe_columns_tuple())
- .nullable(),
+ person::all_columns,
+ person_alias_1.fields(person::all_columns),
+ person_alias_2.fields(person::all_columns).nullable(),
))
.into_boxed();
- if self.unresolved_only.unwrap_or(true) {
+ if self.unresolved_only.unwrap_or(false) {
query = query.filter(private_message_report::resolved.eq(false));
}
let res = query.load::<PrivateMessageReportViewTuple>(conn).await?;
- Ok(PrivateMessageReportView::from_tuple_to_vec(res))
+ Ok(
+ res
+ .into_iter()
+ .map(PrivateMessageReportView::from_tuple)
+ .collect(),
+ )
}
}
-impl ViewToVec for PrivateMessageReportView {
- type DbTuple = PrivateMessageReportViewTuple;
- fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
- items
- .into_iter()
- .map(|a| Self {
- private_message_report: a.0,
- private_message: a.1,
- private_message_creator: a.2,
- creator: a.3,
- resolver: a.4,
- })
- .collect::<Vec<Self>>()
+impl JoinView for PrivateMessageReportView {
+ type JoinTuple = PrivateMessageReportViewTuple;
+ fn from_tuple(a: Self::JoinTuple) -> Self {
+ Self {
+ private_message_report: a.0,
+ private_message: a.1,
+ private_message_creator: a.2,
+ creator: a.3,
+ resolver: a.4,
+ }
}
}
#[serial]
async fn test_crud() {
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
- let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
+ let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
+ .await
+ .unwrap();
let new_person_1 = PersonInsertForm::builder()
.name("timmy_mrv".into())