Required slugs (#516)

This commit is contained in:
Geometrically
2022-12-30 20:03:13 -07:00
committed by GitHub
parent 161dee89ec
commit 88a4f25689

View File

@@ -318,16 +318,11 @@ pub struct EditProject {
pub license_id: Option<String>, pub license_id: Option<String>,
pub client_side: Option<SideType>, pub client_side: Option<SideType>,
pub server_side: Option<SideType>, pub server_side: Option<SideType>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "::serde_with::rust::double_option"
)]
#[validate( #[validate(
length(min = 3, max = 64), length(min = 3, max = 64),
regex = "crate::util::validate::RE_URL_SAFE" regex = "crate::util::validate::RE_URL_SAFE"
)] )]
pub slug: Option<Option<String>>, pub slug: Option<String>,
pub status: Option<ProjectStatus>, pub status: Option<ProjectStatus>,
#[serde( #[serde(
default, default,
@@ -559,16 +554,22 @@ pub async fn project_edit(
FROM team_members tm FROM team_members tm
WHERE tm.team_id = $1 AND tm.accepted WHERE tm.team_id = $1 AND tm.accepted
", ",
project_item.inner.team_id as database::models::ids::TeamId project_item.inner.team_id
as database::models::ids::TeamId
) )
.fetch_many(&mut *transaction) .fetch_many(&mut *transaction)
.try_filter_map(|e| async { Ok(e.right().map(|c| database::models::UserId(c.id))) }) .try_filter_map(|e| async {
Ok(e.right().map(|c| database::models::UserId(c.id)))
})
.try_collect::<Vec<_>>() .try_collect::<Vec<_>>()
.await?; .await?;
NotificationBuilder { NotificationBuilder {
notification_type: Some("status_update".to_string()), notification_type: Some("status_update".to_string()),
title: format!("**{}**'s status has changed!", project_item.inner.title), title: format!(
"**{}**'s status has changed!",
project_item.inner.title
),
text: format!( text: format!(
"The project {}'s status has changed from {} to {}", "The project {}'s status has changed from {} to {}",
project_item.inner.title, project_item.inner.title,
@@ -844,27 +845,25 @@ pub async fn project_edit(
)); ));
} }
if let Some(slug) = slug { let slug_project_id_option: Option<ProjectId> =
let slug_project_id_option: Option<ProjectId> = serde_json::from_str(&format!("\"{}\"", slug)).ok();
serde_json::from_str(&format!("\"{}\"", slug)).ok(); if let Some(slug_project_id) = slug_project_id_option {
if let Some(slug_project_id) = slug_project_id_option { let slug_project_id: database::models::ids::ProjectId =
let slug_project_id: database::models::ids::ProjectId = slug_project_id.into();
slug_project_id.into(); let results = sqlx::query!(
let results = sqlx::query!( "
"
SELECT EXISTS(SELECT 1 FROM mods WHERE id=$1) SELECT EXISTS(SELECT 1 FROM mods WHERE id=$1)
", ",
slug_project_id as database::models::ids::ProjectId slug_project_id as database::models::ids::ProjectId
) )
.fetch_one(&mut *transaction) .fetch_one(&mut *transaction)
.await?; .await?;
if results.exists.unwrap_or(true) { if results.exists.unwrap_or(true) {
return Err(ApiError::InvalidInput( return Err(ApiError::InvalidInput(
"Slug collides with other project's id!" "Slug collides with other project's id!"
.to_string(), .to_string(),
)); ));
}
} }
} }
@@ -874,7 +873,7 @@ pub async fn project_edit(
SET slug = LOWER($1) SET slug = LOWER($1)
WHERE (id = $2) WHERE (id = $2)
", ",
slug.as_deref(), Some(slug),
id as database::models::ids::ProjectId, id as database::models::ids::ProjectId,
) )
.execute(&mut *transaction) .execute(&mut *transaction)