選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

139 行
4.1KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers;
  4. use App\Models\BoardColumn;
  5. use App\Repositories\BoardColumnRepository;
  6. use App\Repositories\CardRepository;
  7. use App\Services\AuthService;
  8. use Core\Controller;
  9. use Core\Request;
  10. class ColumnsController extends Controller
  11. {
  12. private function columns(): BoardColumnRepository
  13. {
  14. return new BoardColumnRepository(database());
  15. }
  16. private function cards(): CardRepository
  17. {
  18. return new CardRepository(database());
  19. }
  20. public function store(Request $request): mixed
  21. {
  22. if (!AuthService::isLoggedIn()) {
  23. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  24. }
  25. $boardId = (int) $request->input('board_id', 0);
  26. $name = trim((string) $request->input('name', ''));
  27. if ($boardId === 0 || $name === '') {
  28. return $this->json(['ok' => false, 'error' => 'board_id and name are required']);
  29. }
  30. $now = date('Y-m-d H:i:s');
  31. $username = AuthService::getCurrentUsername();
  32. $col = new BoardColumn();
  33. $col->boardId = $boardId;
  34. $col->name = $name;
  35. $col->position = $this->columns()->maxPosition($boardId) + 1;
  36. $col->createdAt = $now;
  37. $col->createdBy = $username;
  38. $col->updatedAt = $now;
  39. $col->updatedBy = $username;
  40. $this->columns()->insert($col);
  41. return $this->json(['ok' => true, 'id' => $col->id, 'name' => $col->name, 'position' => $col->position]);
  42. }
  43. public function update(Request $request, int $id): mixed
  44. {
  45. if (!AuthService::isLoggedIn()) {
  46. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  47. }
  48. $name = trim((string) $request->input('name', ''));
  49. if ($name === '') {
  50. return $this->json(['ok' => false, 'error' => 'name is required']);
  51. }
  52. $row = $this->columns()->find($id);
  53. if ($row === null) {
  54. return $this->json(['ok' => false, 'error' => 'Not found'], 404);
  55. }
  56. $col = \App\Models\BoardColumn::fromRow($row);
  57. $col->name = $name;
  58. $col->updatedAt = date('Y-m-d H:i:s');
  59. $col->updatedBy = AuthService::getCurrentUsername();
  60. $this->columns()->update($col);
  61. return $this->json(['ok' => true]);
  62. }
  63. public function toggleCount(Request $request, int $id): mixed
  64. {
  65. if (!AuthService::isLoggedIn()) {
  66. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  67. }
  68. $row = $this->columns()->find($id);
  69. if ($row === null) {
  70. return $this->json(['ok' => false, 'error' => 'Not found'], 404);
  71. }
  72. $show = (string) $request->input('show_card_count', '0') === '1';
  73. $this->columns()->updateShowCardCount($id, $show, date('Y-m-d H:i:s'), AuthService::getCurrentUsername());
  74. return $this->json(['ok' => true, 'show_card_count' => $show]);
  75. }
  76. public function destroy(int $id): mixed
  77. {
  78. if (!AuthService::isLoggedIn()) {
  79. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  80. }
  81. $this->cards()->deleteByColumnId($id);
  82. $this->columns()->delete($id);
  83. return $this->json(['ok' => true]);
  84. }
  85. public function reorder(): mixed
  86. {
  87. if (!AuthService::isLoggedIn()) {
  88. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  89. }
  90. $raw = file_get_contents('php://input');
  91. $items = json_decode((string) $raw, true);
  92. if (!is_array($items)) {
  93. return $this->json(['ok' => false, 'error' => 'Invalid JSON payload']);
  94. }
  95. $now = date('Y-m-d H:i:s');
  96. $username = AuthService::getCurrentUsername();
  97. foreach ($items as $item) {
  98. $colId = (int) ($item['id'] ?? 0);
  99. $position = (int) ($item['position'] ?? 0);
  100. if ($colId > 0) {
  101. $this->columns()->updatePosition($colId, $position, $now, $username);
  102. }
  103. }
  104. return $this->json(['ok' => true]);
  105. }
  106. }

Powered by TurnKey Linux.