Moving folders
This commit is contained in:
83
api/src/Controllers/ItemController.php
Normal file
83
api/src/Controllers/ItemController.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Models\ListModel;
|
||||
use App\Models\ItemModel;
|
||||
use App\Router;
|
||||
use Exception;
|
||||
|
||||
class ItemController
|
||||
{
|
||||
public static function create(string $listUuid): void
|
||||
{
|
||||
$input = Router::getJsonInput();
|
||||
|
||||
if (empty($input['name'])) {
|
||||
Router::sendResponse(['error' => 'Name is required'], 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$list = ListModel::findByUuid($listUuid);
|
||||
|
||||
if (!$list) {
|
||||
Router::sendResponse(['error' => 'List not found'], 404);
|
||||
}
|
||||
|
||||
$item = ItemModel::create($list['id'], $input);
|
||||
Router::sendResponse($item, 201);
|
||||
} catch (Exception $e) {
|
||||
Router::sendResponse(['error' => 'Failed to create item'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public static function update(string $listUuid, string $itemId): void
|
||||
{
|
||||
$input = Router::getJsonInput();
|
||||
|
||||
if (empty($input)) {
|
||||
Router::sendResponse(['error' => 'No data provided'], 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$list = ListModel::findByUuid($listUuid);
|
||||
|
||||
if (!$list) {
|
||||
Router::sendResponse(['error' => 'List not found'], 404);
|
||||
}
|
||||
|
||||
$item = ItemModel::update($list['id'], (int) $itemId, $input);
|
||||
|
||||
if (!$item) {
|
||||
Router::sendResponse(['error' => 'Item not found'], 404);
|
||||
}
|
||||
|
||||
Router::sendResponse($item, 200);
|
||||
} catch (Exception $e) {
|
||||
Router::sendResponse(['error' => 'Failed to update item'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public static function delete(string $listUuid, string $itemId): void
|
||||
{
|
||||
try {
|
||||
$list = ListModel::findByUuid($listUuid);
|
||||
|
||||
if (!$list) {
|
||||
Router::sendResponse(['error' => 'List not found'], 404);
|
||||
}
|
||||
|
||||
$deleted = ItemModel::delete($list['id'], (int) $itemId);
|
||||
|
||||
if (!$deleted) {
|
||||
Router::sendResponse(['error' => 'Item not found'], 404);
|
||||
}
|
||||
|
||||
http_response_code(204);
|
||||
exit;
|
||||
} catch (Exception $e) {
|
||||
Router::sendResponse(['error' => 'Failed to delete item'], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user