-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,226 additions
and
642 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use sqlx::PgConnection; | ||
|
||
use crate::types::{ | ||
api::ApiError, | ||
mod_json::ModJson, | ||
models::dependency::{DependencyImportance, FetchedDependency, ModVersionCompare}, | ||
}; | ||
|
||
pub async fn create( | ||
mod_version_id: i32, | ||
json: &ModJson, | ||
conn: &mut PgConnection, | ||
) -> Result<Vec<FetchedDependency>, ApiError> { | ||
let dependencies = json.prepare_dependencies_for_create()?; | ||
if dependencies.is_empty() { | ||
return Ok(vec![]); | ||
} | ||
|
||
let len = dependencies.len(); | ||
let dependent_id = vec![mod_version_id; len]; | ||
let mut dependency_id: Vec<String> = Vec::with_capacity(len); | ||
let mut version: Vec<String> = Vec::with_capacity(len); | ||
let mut compare: Vec<ModVersionCompare> = Vec::with_capacity(len); | ||
let mut importance: Vec<DependencyImportance> = Vec::with_capacity(len); | ||
|
||
for i in dependencies { | ||
dependency_id.push(i.dependency_id); | ||
version.push(i.version); | ||
compare.push(i.compare); | ||
importance.push(i.importance); | ||
} | ||
|
||
sqlx::query_as!( | ||
FetchedDependency, | ||
r#"INSERT INTO dependencies | ||
(dependent_id, dependency_id, version, compare, importance) | ||
SELECT * FROM UNNEST( | ||
$1::int4[], | ||
$2::text[], | ||
$3::text[], | ||
$4::version_compare[], | ||
$5::dependency_importance[] | ||
) | ||
RETURNING | ||
dependent_id as mod_version_id, | ||
dependency_id, | ||
version, | ||
compare as "compare: _", | ||
importance as "importance: _""#, | ||
&dependent_id, | ||
&dependency_id, | ||
&version, | ||
&compare as &[ModVersionCompare], | ||
&importance as &[DependencyImportance] | ||
) | ||
.fetch_all(conn) | ||
.await | ||
.inspect_err(|e| log::error!("Failed to insert dependencies: {}", e)) | ||
.or(Err(ApiError::DbError)) | ||
} | ||
|
||
pub async fn clear(id: i32, conn: &mut PgConnection) -> Result<(), ApiError> { | ||
sqlx::query!( | ||
"DELETE FROM dependencies | ||
WHERE dependent_id = $1", | ||
id | ||
) | ||
.execute(conn) | ||
.await | ||
.inspect_err(|e| log::error!("Failed to clear deps: {}", e)) | ||
.or(Err(ApiError::DbError))?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use sqlx::PgConnection; | ||
|
||
use crate::types::{ | ||
api::ApiError, | ||
mod_json::ModJson, | ||
models::{ | ||
dependency::ModVersionCompare, | ||
incompatibility::{FetchedIncompatibility, IncompatibilityImportance}, | ||
}, | ||
}; | ||
|
||
pub async fn create( | ||
mod_version_id: i32, | ||
json: &ModJson, | ||
conn: &mut PgConnection, | ||
) -> Result<Vec<FetchedIncompatibility>, ApiError> { | ||
let incompats = json.prepare_incompatibilities_for_create()?; | ||
if incompats.is_empty() { | ||
return Ok(vec![]); | ||
} | ||
|
||
let len = incompats.len(); | ||
let mod_id = vec![mod_version_id; len]; | ||
let mut incompatibility_id: Vec<String> = Vec::with_capacity(len); | ||
let mut version: Vec<String> = Vec::with_capacity(len); | ||
let mut compare: Vec<ModVersionCompare> = Vec::with_capacity(len); | ||
let mut importance: Vec<IncompatibilityImportance> = Vec::with_capacity(len); | ||
|
||
for i in incompats { | ||
incompatibility_id.push(i.incompatibility_id); | ||
version.push(i.version); | ||
compare.push(i.compare); | ||
importance.push(i.importance); | ||
} | ||
|
||
sqlx::query_as!( | ||
FetchedIncompatibility, | ||
r#"INSERT INTO incompatibilities | ||
(mod_id, incompatibility_id, version, compare, importance) | ||
SELECT * FROM UNNEST( | ||
$1::int4[], | ||
$2::text[], | ||
$3::text[], | ||
$4::version_compare[], | ||
$5::incompatibility_importance[] | ||
) | ||
RETURNING | ||
mod_id, | ||
incompatibility_id, | ||
version, | ||
compare as "compare: _", | ||
importance as "importance: _""#, | ||
&mod_id, | ||
&incompatibility_id, | ||
&version, | ||
&compare as &[ModVersionCompare], | ||
&importance as &[IncompatibilityImportance] | ||
) | ||
.fetch_all(conn) | ||
.await | ||
.inspect_err(|e| log::error!("Failed to insert dependencies: {}", e)) | ||
.or(Err(ApiError::DbError)) | ||
} | ||
|
||
pub async fn clear(id: i32, conn: &mut PgConnection) -> Result<(), ApiError> { | ||
sqlx::query!( | ||
"DELETE FROM incompatibilities | ||
WHERE mod_id = $1", | ||
id | ||
) | ||
.execute(conn) | ||
.await | ||
.inspect_err(|e| log::error!("Failed to clear incompats: {}", e)) | ||
.or(Err(ApiError::DbError))?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
pub mod auth_tokens; | ||
pub mod dependencies; | ||
pub mod developers; | ||
pub mod github_login_attempts; | ||
pub mod github_web_logins; | ||
pub mod incompatibilities; | ||
pub mod mod_downloads; | ||
pub mod mod_gd_versions; | ||
pub mod mod_tags; | ||
pub mod mod_version_statuses; | ||
pub mod mod_versions; | ||
pub mod mods; | ||
pub mod refresh_tokens; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use sqlx::PgConnection; | ||
|
||
use crate::types::{ | ||
api::ApiError, | ||
mod_json::ModJson, | ||
models::mod_gd_version::{DetailedGDVersion, GDVersionEnum, VerPlatform}, | ||
}; | ||
|
||
pub async fn create( | ||
mod_version_id: i32, | ||
json: &ModJson, | ||
conn: &mut PgConnection, | ||
) -> Result<DetailedGDVersion, ApiError> { | ||
let create = json.gd.to_create_payload(json); | ||
|
||
let gd: Vec<GDVersionEnum> = create.iter().map(|x| x.gd).collect(); | ||
let platform: Vec<VerPlatform> = create.iter().map(|x| x.platform).collect(); | ||
let mod_id = vec![mod_version_id; create.len()]; | ||
|
||
sqlx::query!( | ||
"INSERT INTO mod_gd_versions | ||
(gd, platform, mod_id) | ||
SELECT * FROM UNNEST( | ||
$1::gd_version[], | ||
$2::gd_ver_platform[], | ||
$3::int4[] | ||
)", | ||
&gd as &[GDVersionEnum], | ||
&platform as &[VerPlatform], | ||
&mod_id | ||
) | ||
.execute(conn) | ||
.await | ||
.inspect_err(|e| log::error!("Failed to insert mod_gd_versions: {}", e)) | ||
.or(Err(ApiError::DbError))?; | ||
|
||
Ok(json.gd.clone()) | ||
} | ||
|
||
pub async fn clear(mod_version_id: i32, conn: &mut PgConnection) -> Result<(), ApiError> { | ||
sqlx::query!( | ||
"DELETE FROM mod_gd_versions mgv | ||
WHERE mgv.mod_id = $1", | ||
mod_version_id | ||
) | ||
.execute(&mut *conn) | ||
.await | ||
.inspect_err(|e| log::error!("Failed to remove GD versions: {}", e)) | ||
.or(Err(ApiError::DbError))?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use sqlx::PgConnection; | ||
|
||
use crate::types::{api::ApiError, models::mod_version_status::ModVersionStatusEnum}; | ||
|
||
pub async fn create( | ||
mod_version_id: i32, | ||
status: ModVersionStatusEnum, | ||
info: Option<String>, | ||
conn: &mut PgConnection, | ||
) -> Result<i32, ApiError> { | ||
sqlx::query!( | ||
"INSERT INTO mod_version_statuses | ||
(mod_version_id, status, info, admin_id) | ||
VALUES ($1, $2, $3, NULL) | ||
RETURNING id", | ||
mod_version_id, | ||
status as ModVersionStatusEnum, | ||
info | ||
) | ||
.fetch_one(conn) | ||
.await | ||
.inspect_err(|e| log::error!("Failed to create status: {}", e)) | ||
.or(Err(ApiError::DbError)) | ||
.map(|i| i.id) | ||
} |
Oops, something went wrong.