You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.7KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers;
  4. use App\Models\Card;
  5. use App\Repositories\CardRepository;
  6. use App\Services\AuthService;
  7. use Core\Controller;
  8. use Core\Request;
  9. class CardsController extends Controller
  10. {
  11. private function cards(): CardRepository
  12. {
  13. return new CardRepository(database());
  14. }
  15. public function store(Request $request): mixed
  16. {
  17. if (!AuthService::isLoggedIn()) {
  18. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  19. }
  20. $boardId = (int) $request->input('board_id', 0);
  21. $columnId = (int) $request->input('column_id', 0);
  22. $swimLaneId = (int) $request->input('swim_lane_id', 0);
  23. if ($boardId === 0 || $columnId === 0 || $swimLaneId === 0) {
  24. return $this->json(['ok' => false, 'error' => 'board_id, column_id, and swim_lane_id are required']);
  25. }
  26. $now = date('Y-m-d H:i:s');
  27. $username = AuthService::getCurrentUsername();
  28. $nextPos = $this->cards()->maxPosition($columnId, $swimLaneId) + 1;
  29. $card = new Card();
  30. $card->boardId = $boardId;
  31. $card->columnId = $columnId;
  32. $card->swimLaneId = $swimLaneId;
  33. $card->jobNumber = trim((string) $request->input('job_number', ''));
  34. $card->jobName = trim((string) $request->input('job_name', ''));
  35. $card->customerName = trim((string) $request->input('customer_name', ''));
  36. $card->deliveryDate = trim((string) $request->input('delivery_date', '')) ?: null;
  37. $card->quantity = trim((string) $request->input('quantity', ''));
  38. $card->notes = trim((string) $request->input('notes', ''));
  39. $card->fullNote = (string) $request->input('full_note', '');
  40. $card->position = $nextPos;
  41. $card->createdAt = $now;
  42. $card->createdBy = $username;
  43. $card->updatedAt = $now;
  44. $card->updatedBy = $username;
  45. try {
  46. $this->cards()->insert($card);
  47. } catch (\Throwable $e) {
  48. return $this->json(['ok' => false, 'error' => $e->getMessage()]);
  49. }
  50. return $this->json(['ok' => true] + $card->toJsonArray());
  51. }
  52. public function update(Request $request, int $id): mixed
  53. {
  54. if (!AuthService::isLoggedIn()) {
  55. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  56. }
  57. $card = $this->cards()->findById($id);
  58. if ($card === null) {
  59. return $this->json(['ok' => false, 'error' => 'Not found'], 404);
  60. }
  61. $card->jobNumber = trim((string) $request->input('job_number', ''));
  62. $card->jobName = trim((string) $request->input('job_name', ''));
  63. $card->customerName = trim((string) $request->input('customer_name', ''));
  64. $card->deliveryDate = trim((string) $request->input('delivery_date', '')) ?: null;
  65. $card->quantity = trim((string) $request->input('quantity', ''));
  66. $card->notes = trim((string) $request->input('notes', ''));
  67. $card->fullNote = (string) $request->input('full_note', '');
  68. $card->updatedAt = date('Y-m-d H:i:s');
  69. $card->updatedBy = AuthService::getCurrentUsername();
  70. try {
  71. $this->cards()->update($card);
  72. } catch (\Throwable $e) {
  73. return $this->json(['ok' => false, 'error' => $e->getMessage()]);
  74. }
  75. return $this->json(['ok' => true] + $card->toJsonArray());
  76. }
  77. public function move(Request $request, int $id): mixed
  78. {
  79. if (!AuthService::isLoggedIn()) {
  80. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  81. }
  82. $columnId = (int) $request->input('column_id', 0);
  83. $swimLaneId = (int) $request->input('swim_lane_id', 0);
  84. $position = (int) $request->input('position', 0);
  85. $now = date('Y-m-d H:i:s');
  86. $username = AuthService::getCurrentUsername();
  87. $this->cards()->move($id, $columnId, $swimLaneId, $position, $now, $username);
  88. $siblings = trim((string) $request->input('sibling_ids', ''));
  89. if ($siblings !== '') {
  90. foreach (explode(',', $siblings) as $idx => $sibId) {
  91. $sibId = (int) trim($sibId);
  92. if ($sibId > 0) {
  93. $this->cards()->updatePosition($sibId, $idx, $now, $username);
  94. }
  95. }
  96. }
  97. return $this->json(['ok' => true]);
  98. }
  99. public function destroy(int $id): mixed
  100. {
  101. if (!AuthService::isLoggedIn()) {
  102. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  103. }
  104. $this->cards()->delete($id);
  105. return $this->json(['ok' => true]);
  106. }
  107. }

Powered by TurnKey Linux.