1
0
Files
TaskBoard/backend/api/project.php
Falknat 250eac70a7 Управление проектами
Добавил возможность удаления, создание и редактирование проектов.
2026-01-18 10:19:34 +07:00

163 lines
6.3 KiB
PHP

<?php
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'POST') {
$data = RestApi::getInput();
$action = $data['action'] ?? null;
$user_id = RestApi::getCurrentUserId();
// Получение данных проекта (проект + колонки + отделы)
if ($action === 'get_project_data') {
$project_id = $data['id_project'] ?? null;
$result = Project::getProjectData($project_id);
if ($result) {
RestApi::response(['success' => true, 'data' => $result]);
} else {
RestApi::response(['success' => false, 'errors' => ['project' => 'Проект не найден']], 404);
}
}
// ==================== CRUD ПРОЕКТОВ ====================
// Создание проекта
if ($action === 'create') {
$name = trim($data['name'] ?? '');
if (!$name) {
RestApi::response(['success' => false, 'errors' => ['name' => 'Укажите название проекта']], 400);
}
$result = Project::create($name, $user_id);
RestApi::response($result, $result['success'] ? 200 : 400);
}
// Обновление проекта
if ($action === 'update') {
$id = $data['id'] ?? null;
$name = trim($data['name'] ?? '');
if (!$id || !$name) {
RestApi::response(['success' => false, 'errors' => ['data' => 'Укажите ID и название']], 400);
}
$result = Project::update($id, $name, $user_id);
RestApi::response($result, $result['success'] ? 200 : 403);
}
// Удаление проекта
if ($action === 'delete') {
$id = $data['id'] ?? null;
if (!$id) {
RestApi::response(['success' => false, 'errors' => ['id' => 'Укажите ID проекта']], 400);
}
$result = Project::delete($id, $user_id);
RestApi::response($result, $result['success'] ? 200 : 403);
}
// Обновление порядка проектов
if ($action === 'update_order') {
$ids = $data['ids'] ?? [];
if (empty($ids)) {
RestApi::response(['success' => false, 'errors' => ['ids' => 'Укажите массив ID']], 400);
}
$result = Project::updateOrder($ids, $user_id);
RestApi::response($result);
}
// ==================== CRUD КОЛОНОК ====================
// Добавление колонки
if ($action === 'add_column') {
$project_id = $data['project_id'] ?? null;
$name = trim($data['name'] ?? '');
$color = $data['color'] ?? '#6366f1';
if (!$project_id || !$name) {
RestApi::response(['success' => false, 'errors' => ['data' => 'Укажите project_id и name']], 400);
}
$result = Project::addColumn($project_id, $name, $color, $user_id);
RestApi::response($result, $result['success'] ? 200 : 403);
}
// Обновление колонки
if ($action === 'update_column') {
$id = $data['id'] ?? null;
$name = isset($data['name']) ? trim($data['name']) : null;
$color = $data['color'] ?? null;
if (!$id) {
RestApi::response(['success' => false, 'errors' => ['id' => 'Укажите ID колонки']], 400);
}
$result = Project::updateColumn($id, $name, $color, $user_id);
RestApi::response($result, $result['success'] ? 200 : 403);
}
// Получение количества задач в колонке (для подтверждения удаления)
if ($action === 'get_column_tasks_count') {
$id = $data['id'] ?? null;
if (!$id) {
RestApi::response(['success' => false, 'errors' => ['id' => 'Укажите ID колонки']], 400);
}
$count = Project::getColumnTasksCount($id);
RestApi::response(['success' => true, 'count' => $count]);
}
// Удаление колонки
if ($action === 'delete_column') {
$id = $data['id'] ?? null;
if (!$id) {
RestApi::response(['success' => false, 'errors' => ['id' => 'Укажите ID колонки']], 400);
}
$result = Project::deleteColumn($id, $user_id);
RestApi::response($result, $result['success'] ? 200 : 403);
}
// Обновление порядка колонок
if ($action === 'update_columns_order') {
$project_id = $data['project_id'] ?? null;
$ids = $data['ids'] ?? [];
if (!$project_id || empty($ids)) {
RestApi::response(['success' => false, 'errors' => ['data' => 'Укажите project_id и ids']], 400);
}
$result = Project::updateColumnsOrder($project_id, $ids, $user_id);
RestApi::response($result, $result['success'] ? 200 : 403);
}
// Установка финальной колонки
if ($action === 'set_ready_column') {
$project_id = $data['project_id'] ?? null;
$column_id = $data['column_id'] ?? null;
if (!$project_id || !$column_id) {
RestApi::response(['success' => false, 'errors' => ['data' => 'Укажите project_id и column_id']], 400);
}
$result = Project::setReadyColumn($project_id, $column_id, $user_id);
RestApi::response($result, $result['success'] ? 200 : 403);
}
// Метод не указан
if (!$action) {
RestApi::response(['success' => false, 'error' => 'Укажите метод'], 400);
}
}
if ($method === 'GET') {
// Получение всех проектов
// ?active=ID — дополнительно вернуть данные активного проекта
$project = new Project();
$projects = $project->getAll();
$active_id = $_GET['active'] ?? null;
if ($active_id) {
// Возвращаем список проектов + данные активного
$activeData = Project::getProjectData((int)$active_id);
RestApi::response([
'success' => true,
'data' => [
'projects' => $projects,
'active' => $activeData
]
]);
} else {
// Только список проектов
RestApi::response(['success' => true, 'data' => $projects]);
}
}
?>