From cda2120fe3f9b8e543a8d906969d6bd9d9bf7158 Mon Sep 17 00:00:00 2001 From: HUANG Cheng Date: Fri, 19 Jan 2024 22:51:44 +0800 Subject: [PATCH] allow create categories with same name --- migrations/20240119144020_category.down.sql | 2 ++ migrations/20240119144020_category.up.sql | 2 ++ src/handlers/category.rs | 12 ------------ 3 files changed, 4 insertions(+), 12 deletions(-) create mode 100644 migrations/20240119144020_category.down.sql create mode 100644 migrations/20240119144020_category.up.sql diff --git a/migrations/20240119144020_category.down.sql b/migrations/20240119144020_category.down.sql new file mode 100644 index 0000000..64277b2 --- /dev/null +++ b/migrations/20240119144020_category.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE category +ADD UNIQUE (name); diff --git a/migrations/20240119144020_category.up.sql b/migrations/20240119144020_category.up.sql new file mode 100644 index 0000000..3983ffd --- /dev/null +++ b/migrations/20240119144020_category.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE category +DROP INDEX name; diff --git a/src/handlers/category.rs b/src/handlers/category.rs index 7dc662d..5b64c35 100644 --- a/src/handlers/category.rs +++ b/src/handlers/category.rs @@ -386,18 +386,6 @@ pub async fn add_category( category: &CreateCategory<'_>, db: &mut Connection, ) -> Result<(), ServiceError> { - let id = query(r#"SELECT id FROM category WHERE name = ?"#) - .bind(category.name) - .fetch_optional(&mut ***db) - .await? - .map(|row| row.get::("id")); - - if id.is_some() { - return Err(ServiceError::AlreadyExists(String::from( - "Category already exists", - ))); - } - let order = match category.parent_id { Some(parent_id) => match query( r#"SELECT MAX(c1.sort_order) AS sort_order FROM category AS c1 INNER JOIN category AS c2 ON c1.parent_id = c2.id WHERE c1.parent_id = ?"#).bind(parent_id).fetch_one(&mut ***db).await { Ok(row) => match row.try_get::("sort_order") {