/
delphin
/
CatRW
Обзор
Документация
Войти
/
delphin
/
CatRW
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
api/models.php
168 строк
6 KB
delphin
start project
02 мар 2026, 09:14
Верифицирован
02 мар 2026, 09:14
4b0ee03
Код
Авторство
О чём код?
<?php // /api/models.php - API для управления моделями (админка) require_once __DIR__ . '/../config.php'; require_once __DIR__ . '/Database.php'; require_once __DIR__ . '/ModelsService.php'; // Переименовываем CatalogService header('Content-Type: application/json; charset=utf-8'); function sendJson($data, $status = 200) { http_response_code($status); echo json_encode($data, JSON_UNESCAPED_UNICODE); exit; } try { $method = $_SERVER['REQUEST_METHOD']; $service = new ModelsService(); $action = $_GET['action'] ?? ''; $id = $_GET['id'] ?? 0; error_log("Models API: method=$method, action=$action, id=$id"); switch ($action) { case 'get': if ($id > 0) { $model = $service->getModel($id); if ($model) { sendJson(['success' => true, 'data' => $model]); } else { sendJson(['success' => false, 'error' => 'Модель не найдена'], 404); } } else { sendJson(['success' => false, 'error' => 'ID обязателен'], 400); } break; case 'list': $page = (int)($_GET['page'] ?? 1); $perPage = (int)($_GET['per_page'] ?? 20); $filters = []; // Фильтры для админки if (isset($_GET['type_code'])) $filters['type_code'] = $_GET['type_code']; if (isset($_GET['scale_code'])) $filters['scale_code'] = $_GET['scale_code']; if (isset($_GET['manufacturer_id'])) $filters['manufacturer_id'] = (int)$_GET['manufacturer_id']; if (isset($_GET['era_code'])) $filters['era_code'] = $_GET['era_code']; if (isset($_GET['status'])) $filters['status'] = $_GET['status']; $result = $service->getModels($filters, $page, $perPage); sendJson([ 'success' => true, 'data' => $result['models'], 'total' => $result['total'], 'page' => $result['page'], 'pages' => $result['pages'], 'perPage' => $result['perPage'] ]); break; case 'search': $query = $_GET['param'] ?? ''; $models = $service->search($query); sendJson(['success' => true, 'data' => $models]); break; case 'create': $input = json_decode(file_get_contents('php://input'), true); if (!$input && !empty($_POST)) { $input = $_POST; } if (empty($input['name']) || empty($input['type_code'])) { sendJson(['success' => false, 'error' => 'Название и тип обязательны'], 400); } $newId = $service->addModel($input); sendJson(['success' => true, 'id' => $newId, 'message' => 'Модель добавлена']); break; case 'update': if ($id <= 0) { sendJson(['success' => false, 'error' => 'ID обязателен'], 400); } $input = json_decode(file_get_contents('php://input'), true); if (!$input) { sendJson(['success' => false, 'error' => 'Нет данных'], 400); } $service->updateModel($id, $input); sendJson(['success' => true, 'message' => 'Модель обновлена']); break; case 'delete': if ($id <= 0) { sendJson(['success' => false, 'error' => 'ID обязателен'], 400); } $result = $service->deleteModel($id); sendJson(['success' => true, 'message' => $result['message']]); break; // Справочники для админки case 'types': sendJson(['success' => true, 'data' => $service->getTypes()]); break; case 'scales': sendJson(['success' => true, 'data' => $service->getScales()]); break; case 'eras': sendJson(['success' => true, 'data' => $service->getEras()]); break; case 'stats': sendJson(['success' => true, 'data' => $service->getStats()]); break; case 'manufacturers_count': $counts = $service->getManufacturersModelsCount(); sendJson(['success' => true, 'data' => $counts]); break; case 'manufacturers_count_by_ids': $ids = $_GET['ids'] ?? ''; if (empty($ids)) { sendJson(['success' => true, 'data' => []]); } else { $idArray = explode(',', $ids); $idArray = array_map('intval', $idArray); $idArray = array_filter($idArray); $counts = $service->getManufacturersCountByIds($idArray); sendJson(['success' => true, 'data' => $counts]); } break; case 'manufacturers_count_only': $counts = $service->getManufacturersModelsCountOnly(); sendJson(['success' => true, 'counts' => $counts]); break; // УДАЛЕНО: model_count_by_manufacturer - теперь в manufacturers.php case 'manufacturer_stats': $id = (int)($_GET['id'] ?? 0); if ($id <= 0) { sendJson(['success' => false, 'error' => 'ID обязателен'], 400); } $stats = $service->getManufacturerStats($id); sendJson(['success' => true, 'stats' => $stats]); break; default: sendJson(['success' => false, 'error' => 'Неизвестное действие: ' . $action], 400); } } catch (Exception $e) { error_log("Models API Error: " . $e->getMessage()); sendJson(['success' => false, 'error' => $e->getMessage()], 500); } ?>