You've already forked AstralRinth
forked from didirus/AstralRinth
Final V2 Changes (#212)
* Redo dependencies, add rejection reasons, make notifications more readable * Fix errors, add dependency route, finish PR * Fix clippy errors
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
use super::ApiError;
|
||||
use crate::auth::get_user_from_headers;
|
||||
use crate::database;
|
||||
use crate::models;
|
||||
use crate::models::projects::{Dependency, DependencyType};
|
||||
use crate::models::teams::Permissions;
|
||||
use crate::util::auth::get_user_from_headers;
|
||||
use crate::util::validate::validation_errors_to_string;
|
||||
use actix_web::{delete, get, patch, web, HttpRequest, HttpResponse};
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::PgPool;
|
||||
use validator::Validate;
|
||||
@@ -189,8 +188,9 @@ pub fn convert_version(
|
||||
.dependencies
|
||||
.into_iter()
|
||||
.map(|d| Dependency {
|
||||
version_id: d.0.into(),
|
||||
dependency_type: DependencyType::from_str(&*d.1),
|
||||
version_id: d.version_id.map(|x| x.into()),
|
||||
project_id: d.project_id.map(|x| x.into()),
|
||||
dependency_type: DependencyType::from_str(&*d.dependency_type),
|
||||
})
|
||||
.collect(),
|
||||
game_versions: data
|
||||
@@ -206,15 +206,14 @@ pub fn convert_version(
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref RE_URL_SAFE: Regex = Regex::new(r"^[a-zA-Z0-9_-]*$").unwrap();
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Validate)]
|
||||
pub struct EditVersion {
|
||||
#[validate(length(min = 3, max = 256))]
|
||||
pub name: Option<String>,
|
||||
#[validate(length(min = 1, max = 64), regex = "RE_URL_SAFE")]
|
||||
#[validate(
|
||||
length(min = 1, max = 64),
|
||||
regex = "crate::util::validate::RE_URL_SAFE"
|
||||
)]
|
||||
pub version_number: Option<String>,
|
||||
#[validate(length(max = 65536))]
|
||||
pub changelog: Option<String>,
|
||||
@@ -236,7 +235,9 @@ pub async fn version_edit(
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let user = get_user_from_headers(req.headers(), &**pool).await?;
|
||||
|
||||
new_version.validate()?;
|
||||
new_version
|
||||
.validate()
|
||||
.map_err(|err| ApiError::ValidationError(validation_errors_to_string(err, None)))?;
|
||||
|
||||
let version_id = info.into_inner().0;
|
||||
let id = version_id.into();
|
||||
@@ -332,21 +333,17 @@ pub async fn version_edit(
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
for dependency in dependencies {
|
||||
let dependency_id: database::models::ids::VersionId =
|
||||
dependency.version_id.clone().into();
|
||||
let builders = dependencies
|
||||
.iter()
|
||||
.map(|x| database::models::version_item::DependencyBuilder {
|
||||
project_id: x.project_id.clone().map(|x| x.into()),
|
||||
version_id: x.version_id.clone().map(|x| x.into()),
|
||||
dependency_type: x.dependency_type.to_string(),
|
||||
})
|
||||
.collect::<Vec<database::models::version_item::DependencyBuilder>>();
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO dependencies (dependent_id, dependency_id, dependency_type)
|
||||
VALUES ($1, $2, $3)
|
||||
",
|
||||
id as database::models::ids::VersionId,
|
||||
dependency_id as database::models::ids::VersionId,
|
||||
dependency.dependency_type.as_str()
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
for dependency in builders {
|
||||
dependency.insert(version_item.id, &mut transaction).await?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -533,7 +530,11 @@ pub async fn version_delete(
|
||||
}
|
||||
}
|
||||
|
||||
let result = database::models::Version::remove_full(id.into(), &**pool).await?;
|
||||
let mut transaction = pool.begin().await?;
|
||||
|
||||
let result = database::models::Version::remove_full(id.into(), &mut transaction).await?;
|
||||
|
||||
transaction.commit().await?;
|
||||
|
||||
if result.is_some() {
|
||||
Ok(HttpResponse::NoContent().body(""))
|
||||
|
||||
Reference in New Issue
Block a user