Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

134 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->cellEnteredAt = $now;
  42. $card->createdAt = $now;
  43. $card->createdBy = $username;
  44. $card->updatedAt = $now;
  45. $card->updatedBy = $username;
  46. try {
  47. $this->cards()->insert($card);
  48. } catch (\Throwable $e) {
  49. return $this->json(['ok' => false, 'error' => $e->getMessage()]);
  50. }
  51. return $this->json(['ok' => true] + $card->toJsonArray());
  52. }
  53. public function update(Request $request, int $id): mixed
  54. {
  55. if (!AuthService::isLoggedIn()) {
  56. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  57. }
  58. $card = $this->cards()->findById($id);
  59. if ($card === null) {
  60. return $this->json(['ok' => false, 'error' => 'Not found'], 404);
  61. }
  62. $card->jobNumber = trim((string) $request->input('job_number', ''));
  63. $card->jobName = trim((string) $request->input('job_name', ''));
  64. $card->customerName = trim((string) $request->input('customer_name', ''));
  65. $card->deliveryDate = trim((string) $request->input('delivery_date', '')) ?: null;
  66. $card->quantity = trim((string) $request->input('quantity', ''));
  67. $card->notes = trim((string) $request->input('notes', ''));
  68. $card->fullNote = (string) $request->input('full_note', '');
  69. $card->updatedAt = date('Y-m-d H:i:s');
  70. $card->updatedBy = AuthService::getCurrentUsername();
  71. try {
  72. $this->cards()->update($card);
  73. } catch (\Throwable $e) {
  74. return $this->json(['ok' => false, 'error' => $e->getMessage()]);
  75. }
  76. return $this->json(['ok' => true] + $card->toJsonArray());
  77. }
  78. public function move(Request $request, int $id): mixed
  79. {
  80. if (!AuthService::isLoggedIn()) {
  81. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  82. }
  83. $columnId = (int) $request->input('column_id', 0);
  84. $swimLaneId = (int) $request->input('swim_lane_id', 0);
  85. $position = (int) $request->input('position', 0);
  86. $now = date('Y-m-d H:i:s');
  87. $username = AuthService::getCurrentUsername();
  88. $this->cards()->move($id, $columnId, $swimLaneId, $position, $now, $username);
  89. $siblings = trim((string) $request->input('sibling_ids', ''));
  90. if ($siblings !== '') {
  91. foreach (explode(',', $siblings) as $idx => $sibId) {
  92. $sibId = (int) trim($sibId);
  93. if ($sibId > 0) {
  94. $this->cards()->updatePosition($sibId, $idx, $now, $username);
  95. }
  96. }
  97. }
  98. return $this->json(['ok' => true]);
  99. }
  100. public function destroy(int $id): mixed
  101. {
  102. if (!AuthService::isLoggedIn()) {
  103. return $this->json(['ok' => false, 'error' => 'Unauthorized'], 401);
  104. }
  105. $this->cards()->delete($id);
  106. return $this->json(['ok' => true]);
  107. }
  108. }

Powered by TurnKey Linux.