Labrinth ID cleanup (#3681)

* Put all ID types in the labrinth::models::ids, and reduce code duplication with them

* Rewrite labrinth::database::models::ids and rename most DB interface ID structs to be prefixed with DB

* Run sqlx prepare

---------

Co-authored-by: Alejandro González <7822554+AlexTMjugador@users.noreply.github.com>
This commit is contained in:
Josiah Glosson
2025-05-22 03:34:36 -05:00
committed by GitHub
parent c6022ad977
commit 9e527ff141
111 changed files with 1477 additions and 1965 deletions

View File

@@ -2,28 +2,28 @@ use super::ids::*;
use chrono::{DateTime, Utc};
pub struct Report {
pub id: ReportId,
pub id: DBReportId,
pub report_type_id: ReportTypeId,
pub project_id: Option<ProjectId>,
pub version_id: Option<VersionId>,
pub user_id: Option<UserId>,
pub project_id: Option<DBProjectId>,
pub version_id: Option<DBVersionId>,
pub user_id: Option<DBUserId>,
pub body: String,
pub reporter: UserId,
pub reporter: DBUserId,
pub created: DateTime<Utc>,
pub closed: bool,
}
pub struct QueryReport {
pub id: ReportId,
pub id: DBReportId,
pub report_type: String,
pub project_id: Option<ProjectId>,
pub version_id: Option<VersionId>,
pub user_id: Option<UserId>,
pub project_id: Option<DBProjectId>,
pub version_id: Option<DBVersionId>,
pub user_id: Option<DBUserId>,
pub body: String,
pub reporter: UserId,
pub reporter: DBUserId,
pub created: DateTime<Utc>,
pub closed: bool,
pub thread_id: ThreadId,
pub thread_id: DBThreadId,
}
impl Report {
@@ -42,13 +42,13 @@ impl Report {
$6, $7
)
",
self.id as ReportId,
self.id as DBReportId,
self.report_type_id as ReportTypeId,
self.project_id.map(|x| x.0 as i64),
self.version_id.map(|x| x.0 as i64),
self.user_id.map(|x| x.0 as i64),
self.body,
self.reporter as UserId
self.reporter as DBUserId
)
.execute(&mut **transaction)
.await?;
@@ -57,7 +57,7 @@ impl Report {
}
pub async fn get<'a, E>(
id: ReportId,
id: DBReportId,
exec: E,
) -> Result<Option<QueryReport>, sqlx::Error>
where
@@ -69,7 +69,7 @@ impl Report {
}
pub async fn get_many<'a, E>(
report_ids: &[ReportId],
report_ids: &[DBReportId],
exec: E,
) -> Result<Vec<QueryReport>, sqlx::Error>
where
@@ -92,16 +92,16 @@ impl Report {
)
.fetch(exec)
.map_ok(|x| QueryReport {
id: ReportId(x.id),
id: DBReportId(x.id),
report_type: x.name,
project_id: x.mod_id.map(ProjectId),
version_id: x.version_id.map(VersionId),
user_id: x.user_id.map(UserId),
project_id: x.mod_id.map(DBProjectId),
version_id: x.version_id.map(DBVersionId),
user_id: x.user_id.map(DBUserId),
body: x.body,
reporter: UserId(x.reporter),
reporter: DBUserId(x.reporter),
created: x.created,
closed: x.closed,
thread_id: ThreadId(x.thread_id)
thread_id: DBThreadId(x.thread_id)
})
.try_collect::<Vec<QueryReport>>()
.await?;
@@ -110,14 +110,14 @@ impl Report {
}
pub async fn remove_full(
id: ReportId,
id: DBReportId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<Option<()>, sqlx::error::Error> {
let result = sqlx::query!(
"
SELECT EXISTS(SELECT 1 FROM reports WHERE id = $1)
",
id as ReportId
id as DBReportId
)
.fetch_one(&mut **transaction)
.await?;
@@ -131,14 +131,14 @@ impl Report {
SELECT id FROM threads
WHERE report_id = $1
",
id as ReportId
id as DBReportId
)
.fetch_optional(&mut **transaction)
.await?;
if let Some(thread_id) = thread_id {
crate::database::models::Thread::remove_full(
ThreadId(thread_id.id),
DBThreadId(thread_id.id),
transaction,
)
.await?;
@@ -148,7 +148,7 @@ impl Report {
"
DELETE FROM reports WHERE id = $1
",
id as ReportId,
id as DBReportId,
)
.execute(&mut **transaction)
.await?;