Skip to content

Commit

Permalink
allow create categories with same name
Browse files Browse the repository at this point in the history
  • Loading branch information
huangcheng committed Jan 19, 2024
1 parent 903ccb5 commit cda2120
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 12 deletions.
2 changes: 2 additions & 0 deletions migrations/20240119144020_category.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE category
ADD UNIQUE (name);
2 changes: 2 additions & 0 deletions migrations/20240119144020_category.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE category
DROP INDEX name;
12 changes: 0 additions & 12 deletions src/handlers/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,6 @@ pub async fn add_category(
category: &CreateCategory<'_>,
db: &mut Connection<MySQLDb>,
) -> Result<(), ServiceError> {
let id = query(r#"SELECT id FROM category WHERE name = ?"#)
.bind(category.name)
.fetch_optional(&mut ***db)
.await?
.map(|row| row.get::<i64, &str>("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::<i64, &str>("sort_order") {
Expand Down

0 comments on commit cda2120

Please sign in to comment.