mirror of
https://github.com/JGH0/Todo-App-Backend.git
synced 2026-06-03 13:28:47 +02:00
fix category duplicates: validate unique name on create and rename, return proper 409 error instead of SQL 500
This commit is contained in:
@@ -44,6 +44,16 @@ class CategoryController extends BaseController
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for duplicate name per user
|
||||
$existing = $this->categoryModel
|
||||
->where('user_id', $userId)
|
||||
->where('name', $json['name'])
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
return $this->errorResponse('A category with this name already exists.', 409);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'id' => $this->generateUuid(),
|
||||
'user_id' => $userId,
|
||||
@@ -88,6 +98,20 @@ class CategoryController extends BaseController
|
||||
}
|
||||
|
||||
$json = $this->request->getJSON(true);
|
||||
|
||||
// Check for duplicate name on rename (excluding current category)
|
||||
if (!empty($json['name']) && strtolower($json['name']) !== strtolower($category['name'])) {
|
||||
$existing = $this->categoryModel
|
||||
->where('user_id', $userId)
|
||||
->where('name', $json['name'])
|
||||
->where('id !=', $id)
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
return $this->errorResponse('A category with this name already exists.', 409);
|
||||
}
|
||||
}
|
||||
|
||||
$allowedFields = ['name', 'color', 'favorite'];
|
||||
$updateData = array_intersect_key($json, array_flip($allowedFields));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user