mirror of
https://github.com/JGH0/Todo-App-Backend.git
synced 2026-06-03 13:28:47 +02:00
implement full backend requirements: pagination, filtering, sorting, meta responses, JWT auth, model validation, request logging, API key management
- BaseController: paginatedResponse() helper with meta (page/perPage/total/lastPage/hasMore), getSortParams(), getFilterParams(), encodeJwt()/decodeJwt(), logActivity() helper, validateWithModel() - TodoController: paginated/sortable/filterable index, model-based validation, boolean conversion on write, activity logging - CategoryController: same pagination/sort/filter patterns + duplicate-name check (409) - ProjectController: paginated index + activity logging - RecurringTaskController: paginated/sortable/filterable index + junction-table category linking - AuthController: JWT register/login/refresh endpoints (firebase/php-jwt v7) - Routes: JWT routes added as public endpoints - Models: all have proper validationRules with exact error messages (field-level, user-facing) - ApiAuthFilter: scoped API key auth + UserThemeController generateUuid visibility fix - composer.json: add firebase/php-jwt ^7.0
This commit is contained in:
@@ -14,37 +14,44 @@ class CategoryController extends BaseController
|
||||
$this->categoryModel = new CategoryModel();
|
||||
}
|
||||
|
||||
const SORTABLE = ['name', 'created_at'];
|
||||
const FILTERABLE = ['favorite'];
|
||||
|
||||
/**
|
||||
* Get all categories for the authenticated user
|
||||
* GET /api/v1/categories
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$userId = $this->getUserId();
|
||||
$categories = $this->categoryModel->where('user_id', $userId)->findAll();
|
||||
$userId = $this->getUserId();
|
||||
$filters = $this->getFilterParams(self::FILTERABLE);
|
||||
$sorts = $this->getSortParams(self::SORTABLE);
|
||||
|
||||
return $this->successResponse($categories, 'Categories retrieved successfully');
|
||||
$builder = $this->categoryModel->where('user_id', $userId);
|
||||
$this->applyFilters($builder, $filters);
|
||||
|
||||
if (empty($sorts)) {
|
||||
$builder->orderBy('name', 'ASC');
|
||||
} else {
|
||||
$this->applySort($builder, $sorts);
|
||||
}
|
||||
|
||||
return $this->paginatedResponse($builder, 'Categories retrieved successfully');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new category
|
||||
* POST /api/v1/categories
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$userId = $this->getUserId();
|
||||
$json = $this->request->getJSON(true);
|
||||
|
||||
$rules = [
|
||||
'name' => 'required|max_length[255]',
|
||||
'color' => 'required|max_length[7]',
|
||||
];
|
||||
|
||||
if (!$this->validateRequest($rules)) {
|
||||
if (!$this->validateWithModel($this->categoryModel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for duplicate name per user
|
||||
$json = $this->request->getJSON(true);
|
||||
|
||||
// Custom duplicate check (per user)
|
||||
$existing = $this->categoryModel
|
||||
->where('user_id', $userId)
|
||||
->where('name', $json['name'])
|
||||
@@ -55,26 +62,29 @@ class CategoryController extends BaseController
|
||||
}
|
||||
|
||||
$data = [
|
||||
'id' => $this->generateUuid(),
|
||||
'user_id' => $userId,
|
||||
'name' => $json['name'],
|
||||
'color' => $json['color'],
|
||||
'favorite' => $json['favorite'] ?? false,
|
||||
'id' => $this->generateUuid(),
|
||||
'user_id' => $userId,
|
||||
'name' => $json['name'],
|
||||
'color' => $json['color'],
|
||||
'favorite' => !empty($json['favorite']),
|
||||
];
|
||||
|
||||
$this->categoryModel->insert($data);
|
||||
$category = $this->categoryModel->find($data['id']);
|
||||
|
||||
$this->logActivity('category_created', 'category', $data['id'], [
|
||||
'name' => $data['name'],
|
||||
]);
|
||||
|
||||
return $this->successResponse($category, 'Category created successfully', 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific category
|
||||
* GET /api/v1/categories/{id}
|
||||
*/
|
||||
public function show($id = null)
|
||||
{
|
||||
$userId = $this->getUserId();
|
||||
$userId = $this->getUserId();
|
||||
$category = $this->categoryModel->where('id', $id)->where('user_id', $userId)->first();
|
||||
|
||||
if (!$category) {
|
||||
@@ -85,12 +95,11 @@ class CategoryController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a category
|
||||
* PUT /api/v1/categories/{id}
|
||||
*/
|
||||
public function update($id = null)
|
||||
{
|
||||
$userId = $this->getUserId();
|
||||
$userId = $this->getUserId();
|
||||
$category = $this->categoryModel->where('id', $id)->where('user_id', $userId)->first();
|
||||
|
||||
if (!$category) {
|
||||
@@ -99,7 +108,7 @@ class CategoryController extends BaseController
|
||||
|
||||
$json = $this->request->getJSON(true);
|
||||
|
||||
// Check for duplicate name on rename (excluding current category)
|
||||
// Duplicate check on rename
|
||||
if (!empty($json['name']) && strtolower($json['name']) !== strtolower($category['name'])) {
|
||||
$existing = $this->categoryModel
|
||||
->where('user_id', $userId)
|
||||
@@ -113,25 +122,33 @@ class CategoryController extends BaseController
|
||||
}
|
||||
|
||||
$allowedFields = ['name', 'color', 'favorite'];
|
||||
$updateData = array_intersect_key($json, array_flip($allowedFields));
|
||||
$updateData = array_intersect_key($json, array_flip($allowedFields));
|
||||
|
||||
if (empty($updateData)) {
|
||||
return $this->errorResponse('No valid fields to update');
|
||||
}
|
||||
|
||||
// Convert boolean
|
||||
if (array_key_exists('favorite', $updateData)) {
|
||||
$updateData['favorite'] = !empty($updateData['favorite']);
|
||||
}
|
||||
|
||||
$this->categoryModel->update($id, $updateData);
|
||||
$category = $this->categoryModel->find($id);
|
||||
|
||||
$this->logActivity('category_updated', 'category', $id, [
|
||||
'name' => $category['name'] ?? 'Unknown',
|
||||
]);
|
||||
|
||||
return $this->successResponse($category, 'Category updated successfully');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a category
|
||||
* DELETE /api/v1/categories/{id}
|
||||
*/
|
||||
public function delete($id = null)
|
||||
{
|
||||
$userId = $this->getUserId();
|
||||
$userId = $this->getUserId();
|
||||
$category = $this->categoryModel->where('id', $id)->where('user_id', $userId)->first();
|
||||
|
||||
if (!$category) {
|
||||
@@ -140,18 +157,10 @@ class CategoryController extends BaseController
|
||||
|
||||
$this->categoryModel->delete($id);
|
||||
|
||||
$this->logActivity('category_deleted', 'category', $id, [
|
||||
'name' => $category['name'] ?? 'Unknown',
|
||||
]);
|
||||
|
||||
return $this->successResponse(null, 'Category deleted successfully');
|
||||
}
|
||||
|
||||
private function generateUuid(): string
|
||||
{
|
||||
return sprintf(
|
||||
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0x0fff) | 0x4000,
|
||||
mt_rand(0, 0x3fff) | 0x8000,
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user