|
- <?php
-
- declare(strict_types=1);
-
- namespace App\Controllers;
-
- use App\Models\BoardColumn;
- use App\Repositories\BoardColumnRepository;
- use App\Repositories\CardRepository;
- use App\Services\AuthService;
- use Core\Controller;
- use Core\Request;
-
- class ColumnsController extends Controller
- {
- private function columns(): BoardColumnRepository
- {
- return new BoardColumnRepository(database());
- }
-
- private function cards(): CardRepository
- {
- return new CardRepository(database());
- }
-
- public function store(Request $request): mixed
- {
- if (!AuthService::isLoggedIn()) {
- return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
- }
-
- $boardId = (int) $request->input('board_id', 0);
- $name = trim((string) $request->input('name', ''));
-
- if ($boardId === 0 || $name === '') {
- return $this->json(['ok' => false, 'error' => 'board_id and name are required']);
- }
-
- $now = date('Y-m-d H:i:s');
- $username = AuthService::getCurrentUsername();
-
- $col = new BoardColumn();
- $col->boardId = $boardId;
- $col->name = $name;
- $col->position = $this->columns()->maxPosition($boardId) + 1;
- $col->createdAt = $now;
- $col->createdBy = $username;
- $col->updatedAt = $now;
- $col->updatedBy = $username;
-
- $this->columns()->insert($col);
-
- return $this->json(['ok' => true, 'id' => $col->id, 'name' => $col->name, 'position' => $col->position]);
- }
-
- public function update(Request $request, int $id): mixed
- {
- if (!AuthService::isLoggedIn()) {
- return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
- }
-
- $name = trim((string) $request->input('name', ''));
- if ($name === '') {
- return $this->json(['ok' => false, 'error' => 'name is required']);
- }
-
- $row = $this->columns()->find($id);
- if ($row === null) {
- return $this->json(['ok' => false, 'error' => 'Not found'], 404);
- }
-
- $col = \App\Models\BoardColumn::fromRow($row);
- $col->name = $name;
- $col->updatedAt = date('Y-m-d H:i:s');
- $col->updatedBy = AuthService::getCurrentUsername();
-
- $this->columns()->update($col);
-
- return $this->json(['ok' => true]);
- }
-
- public function destroy(int $id): mixed
- {
- if (!AuthService::isLoggedIn()) {
- return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
- }
-
- $this->cards()->deleteByColumnId($id);
- $this->columns()->delete($id);
-
- return $this->json(['ok' => true]);
- }
-
- public function reorder(): mixed
- {
- if (!AuthService::isLoggedIn()) {
- return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
- }
-
- $raw = file_get_contents('php://input');
- $items = json_decode((string) $raw, true);
-
- if (!is_array($items)) {
- return $this->json(['ok' => false, 'error' => 'Invalid JSON payload']);
- }
-
- $now = date('Y-m-d H:i:s');
- $username = AuthService::getCurrentUsername();
-
- foreach ($items as $item) {
- $colId = (int) ($item['id'] ?? 0);
- $position = (int) ($item['position'] ?? 0);
- if ($colId > 0) {
- $this->columns()->updatePosition($colId, $position, $now, $username);
- }
- }
-
- return $this->json(['ok' => true]);
- }
- }
|