/
delphin
/
CatRW
Обзор
Документация
Войти
/
delphin
/
CatRW
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
api/model_types.php
109 строк
4 KB
delphin
start project
02 мар 2026, 09:14
Верифицирован
02 мар 2026, 09:14
4b0ee03
Код
Авторство
О чём код?
<?php // /api/model_types.php - исправленная версия header('Content-Type: application/json; charset=utf-8'); require_once __DIR__ . '/../config.php'; require_once __DIR__ . '/Database.php'; require_once __DIR__ . '/ModelTypeService.php'; function sendJson($data, $status = 200) { http_response_code($status); echo json_encode($data, JSON_UNESCAPED_UNICODE); exit; } try { $method = $_SERVER['REQUEST_METHOD']; $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $parts = explode('/', trim($path, '/')); // Получаем code из URL $code = ''; foreach ($parts as $index => $part) { if (isset($parts[$index-1]) && $parts[$index-1] === 'model_types') { $code = $part; break; } } if (!$code && isset($_GET['code'])) { $code = $_GET['code']; } switch($method) { case 'GET': if ($code) { // Получить один тип $type = ModelTypeService::get($code); if ($type) { sendJson(['success' => true, 'data' => $type]); } else { sendJson(['success' => false, 'error' => 'Тип не найден'], 404); } } else { // Получить все типы $types = ModelTypeService::getAll(); sendJson(['success' => true, 'data' => $types]); } break; case 'POST': // Добавить тип $input = json_decode(file_get_contents('php://input'), true); if (!$input && !empty($_POST)) { $input = $_POST; } try { $id = ModelTypeService::add($input); sendJson([ 'success' => true, 'message' => 'Тип добавлен', 'id' => $id ]); } catch (Exception $e) { sendJson(['success' => false, 'error' => $e->getMessage()], 400); } break; case 'PUT': if (!$code) { sendJson(['success' => false, 'error' => 'Код обязателен'], 400); } $input = json_decode(file_get_contents('php://input'), true); if (!$input) { sendJson(['success' => false, 'error' => 'Нет данных'], 400); } try { ModelTypeService::update($code, $input); sendJson(['success' => true, 'message' => 'Тип модели обновлён']); } catch (Exception $e) { sendJson(['success' => false, 'error' => $e->getMessage()], 400); } break; case 'DELETE': if (!$code) { sendJson(['success' => false, 'error' => 'Код обязателен'], 400); } try { ModelTypeService::delete($code); sendJson(['success' => true, 'message' => 'Тип удален']); } catch (Exception $e) { sendJson(['success' => false, 'error' => $e->getMessage()], 400); } break; default: sendJson(['success' => false, 'error' => 'Метод не поддерживается'], 405); } } catch(Exception $e) { error_log("ModelTypes API Error: " . $e->getMessage()); sendJson(['success' => false, 'error' => 'Внутренняя ошибка сервера'], 500); } ?>