25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

196 satır
7.0KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers;
  4. use App\Models\Card;
  5. use App\Models\CardMovement;
  6. use App\Repositories\BoardColumnRepository;
  7. use App\Repositories\CardMovementRepository;
  8. use App\Repositories\CardRepository;
  9. use App\Repositories\SwimLaneRepository;
  10. use App\Services\AuthService;
  11. use Core\Controller;
  12. use Core\Request;
  13. class CardsController extends Controller
  14. {
  15. private function cards(): CardRepository
  16. {
  17. return new CardRepository(database());
  18. }
  19. private function cardMovements(): CardMovementRepository
  20. {
  21. return new CardMovementRepository(database());
  22. }
  23. private function columns(): BoardColumnRepository
  24. {
  25. return new BoardColumnRepository(database());
  26. }
  27. private function lanes(): SwimLaneRepository
  28. {
  29. return new SwimLaneRepository(database());
  30. }
  31. public function store(Request $request): mixed
  32. {
  33. if (!AuthService::isLoggedIn()) {
  34. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  35. }
  36. $boardId = (int) $request->input('board_id', 0);
  37. $columnId = (int) $request->input('column_id', 0);
  38. $swimLaneId = (int) $request->input('swim_lane_id', 0);
  39. if ($boardId === 0 || $columnId === 0 || $swimLaneId === 0) {
  40. return $this->json(['ok' => false, 'error' => 'board_id, column_id, and swim_lane_id are required']);
  41. }
  42. $now = date('Y-m-d H:i:s');
  43. $username = AuthService::getCurrentUsername();
  44. $nextPos = $this->cards()->maxPosition($columnId, $swimLaneId) + 1;
  45. $card = new Card();
  46. $card->boardId = $boardId;
  47. $card->columnId = $columnId;
  48. $card->swimLaneId = $swimLaneId;
  49. $card->jobNumber = trim((string) $request->input('job_number', ''));
  50. $card->jobName = trim((string) $request->input('job_name', ''));
  51. $card->customerName = trim((string) $request->input('customer_name', ''));
  52. $card->deliveryDate = trim((string) $request->input('delivery_date', '')) ?: null;
  53. $card->quantity = trim((string) $request->input('quantity', ''));
  54. $card->notes = trim((string) $request->input('notes', ''));
  55. $card->fullNote = (string) $request->input('full_note', '');
  56. $card->position = $nextPos;
  57. $card->cellEnteredAt = $now;
  58. $card->createdAt = $now;
  59. $card->createdBy = $username;
  60. $card->updatedAt = $now;
  61. $card->updatedBy = $username;
  62. try {
  63. $this->cards()->insert($card);
  64. } catch (\Throwable $e) {
  65. return $this->json(['ok' => false, 'error' => $e->getMessage()]);
  66. }
  67. return $this->json(['ok' => true] + $card->toJsonArray());
  68. }
  69. public function update(Request $request, int $id): mixed
  70. {
  71. if (!AuthService::isLoggedIn()) {
  72. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  73. }
  74. $card = $this->cards()->findById($id);
  75. if ($card === null) {
  76. return $this->json(['ok' => false, 'error' => 'Not found'], 404);
  77. }
  78. $card->jobNumber = trim((string) $request->input('job_number', ''));
  79. $card->jobName = trim((string) $request->input('job_name', ''));
  80. $card->customerName = trim((string) $request->input('customer_name', ''));
  81. $card->deliveryDate = trim((string) $request->input('delivery_date', '')) ?: null;
  82. $card->quantity = trim((string) $request->input('quantity', ''));
  83. $card->notes = trim((string) $request->input('notes', ''));
  84. $card->fullNote = (string) $request->input('full_note', '');
  85. $card->updatedAt = date('Y-m-d H:i:s');
  86. $card->updatedBy = AuthService::getCurrentUsername();
  87. try {
  88. $this->cards()->update($card);
  89. } catch (\Throwable $e) {
  90. return $this->json(['ok' => false, 'error' => $e->getMessage()]);
  91. }
  92. return $this->json(['ok' => true] + $card->toJsonArray());
  93. }
  94. public function move(Request $request, int $id): mixed
  95. {
  96. if (!AuthService::isLoggedIn()) {
  97. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  98. }
  99. $card = $this->cards()->findById($id);
  100. if ($card === null) {
  101. return $this->json(['ok' => false, 'error' => 'Not found'], 404);
  102. }
  103. $columnId = (int) $request->input('column_id', 0);
  104. $swimLaneId = (int) $request->input('swim_lane_id', 0);
  105. $position = (int) $request->input('position', 0);
  106. $now = date('Y-m-d H:i:s');
  107. $username = AuthService::getCurrentUsername();
  108. $this->cards()->move($id, $columnId, $swimLaneId, $position, $now, $username);
  109. if ($columnId !== $card->columnId || $swimLaneId !== $card->swimLaneId) {
  110. $fromColumn = $this->columns()->find($card->columnId);
  111. $toColumn = $this->columns()->find($columnId);
  112. $fromLane = $this->lanes()->find($card->swimLaneId);
  113. $toLane = $this->lanes()->find($swimLaneId);
  114. $movement = new CardMovement();
  115. $movement->cardId = $id;
  116. $movement->fromColumnId = $card->columnId;
  117. $movement->fromColumnName = $fromColumn['name'] ?? null;
  118. $movement->toColumnId = $columnId;
  119. $movement->toColumnName = $toColumn['name'] ?? null;
  120. $movement->fromSwimLaneId = $card->swimLaneId;
  121. $movement->fromSwimLaneName = $fromLane['name'] ?? null;
  122. $movement->toSwimLaneId = $swimLaneId;
  123. $movement->toSwimLaneName = $toLane['name'] ?? null;
  124. $movement->movedAt = $now;
  125. $movement->movedBy = $username;
  126. $this->cardMovements()->insert($movement);
  127. }
  128. $siblings = trim((string) $request->input('sibling_ids', ''));
  129. if ($siblings !== '') {
  130. foreach (explode(',', $siblings) as $idx => $sibId) {
  131. $sibId = (int) trim($sibId);
  132. if ($sibId > 0) {
  133. $this->cards()->updatePosition($sibId, $idx, $now, $username);
  134. }
  135. }
  136. }
  137. return $this->json(['ok' => true]);
  138. }
  139. public function destroy(int $id): mixed
  140. {
  141. if (!AuthService::isLoggedIn()) {
  142. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  143. }
  144. $this->cardMovements()->deleteByCardId($id);
  145. $this->cards()->delete($id);
  146. return $this->json(['ok' => true]);
  147. }
  148. public function history(int $id): mixed
  149. {
  150. if (!AuthService::isLoggedIn()) {
  151. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  152. }
  153. $movements = array_map(
  154. fn($m) => $m->toJsonArray(),
  155. $this->cardMovements()->findByCardId($id)
  156. );
  157. return $this->json(['ok' => true, 'movements' => $movements]);
  158. }
  159. }

Powered by TurnKey Linux.