Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

164 строки
6.3KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repositories;
  4. use App\Models\Card;
  5. use Core\Repository;
  6. class CardRepository extends Repository
  7. {
  8. protected string $table = 'cards';
  9. public function findById(int $id): ?Card
  10. {
  11. $row = $this->database->first('SELECT * FROM cards WHERE id = :id', ['id' => $id]);
  12. return $row ? Card::fromRow($row) : null;
  13. }
  14. /** @return Card[] */
  15. public function findByBoardId(int $boardId): array
  16. {
  17. $rows = $this->database->query(
  18. 'SELECT * FROM cards WHERE board_id = :board_id ORDER BY swim_lane_id ASC, column_id ASC, position ASC',
  19. ['board_id' => $boardId]
  20. );
  21. return array_map(fn(array $r) => Card::fromRow($r), $rows);
  22. }
  23. /** @return Card[] */
  24. public function findByColumnId(int $columnId): array
  25. {
  26. $rows = $this->database->query(
  27. 'SELECT * FROM cards WHERE column_id = :column_id ORDER BY swim_lane_id ASC, position ASC',
  28. ['column_id' => $columnId]
  29. );
  30. return array_map(fn(array $r) => Card::fromRow($r), $rows);
  31. }
  32. /** @return Card[] */
  33. public function findBySwimLaneId(int $swimLaneId): array
  34. {
  35. $rows = $this->database->query(
  36. 'SELECT * FROM cards WHERE swim_lane_id = :swim_lane_id ORDER BY column_id ASC, position ASC',
  37. ['swim_lane_id' => $swimLaneId]
  38. );
  39. return array_map(fn(array $r) => Card::fromRow($r), $rows);
  40. }
  41. public function maxPosition(int $columnId, int $swimLaneId): int
  42. {
  43. $row = $this->database->first(
  44. 'SELECT MAX(position) AS max_pos FROM cards WHERE column_id = :col AND swim_lane_id = :lane',
  45. ['col' => $columnId, 'lane' => $swimLaneId]
  46. );
  47. return (int) ($row['max_pos'] ?? -1);
  48. }
  49. public function insert(Card $card): Card
  50. {
  51. $this->database->execute(
  52. 'INSERT INTO cards
  53. (board_id, column_id, swim_lane_id, job_number, job_name, customer_name, delivery_date,
  54. quantity, notes, full_note, position, cell_entered_at, created_at, created_by, updated_at, updated_by)
  55. VALUES
  56. (:board_id, :column_id, :swim_lane_id, :job_number, :job_name, :customer_name, :delivery_date,
  57. :quantity, :notes, :full_note, :position, :cell_entered_at, :created_at, :created_by, :updated_at, :updated_by)',
  58. [
  59. 'board_id' => $card->boardId,
  60. 'column_id' => $card->columnId,
  61. 'swim_lane_id' => $card->swimLaneId,
  62. 'job_number' => $card->jobNumber,
  63. 'job_name' => $card->jobName,
  64. 'customer_name' => $card->customerName,
  65. 'delivery_date' => $card->deliveryDate ?: null,
  66. 'quantity' => $card->quantity !== '' ? $card->quantity : null,
  67. 'notes' => $card->notes,
  68. 'full_note' => $card->fullNote,
  69. 'position' => $card->position,
  70. 'cell_entered_at' => $card->cellEnteredAt,
  71. 'created_at' => $card->createdAt,
  72. 'created_by' => $card->createdBy,
  73. 'updated_at' => $card->updatedAt,
  74. 'updated_by' => $card->updatedBy,
  75. ]
  76. );
  77. $row = $this->database->first('SELECT last_insert_rowid() AS id');
  78. $card->id = (int) ($row['id'] ?? 0);
  79. return $card;
  80. }
  81. public function update(Card $card): void
  82. {
  83. $this->database->execute(
  84. 'UPDATE cards
  85. SET job_number = :job_number, job_name = :job_name, customer_name = :customer_name,
  86. delivery_date = :delivery_date, quantity = :quantity, notes = :notes, full_note = :full_note,
  87. updated_at = :updated_at, updated_by = :updated_by
  88. WHERE id = :id',
  89. [
  90. 'job_number' => $card->jobNumber,
  91. 'job_name' => $card->jobName,
  92. 'customer_name' => $card->customerName,
  93. 'delivery_date' => $card->deliveryDate ?: null,
  94. 'quantity' => $card->quantity !== '' ? $card->quantity : null,
  95. 'notes' => $card->notes,
  96. 'full_note' => $card->fullNote,
  97. 'updated_at' => $card->updatedAt,
  98. 'updated_by' => $card->updatedBy,
  99. 'id' => $card->id,
  100. ]
  101. );
  102. }
  103. public function move(int $id, int $columnId, int $swimLaneId, int $position, string $updatedAt, string $updatedBy): void
  104. {
  105. $this->database->execute(
  106. 'UPDATE cards SET column_id = :column_id, swim_lane_id = :swim_lane_id, position = :position,
  107. updated_at = :updated_at, updated_by = :updated_by,
  108. cell_entered_at = CASE
  109. WHEN column_id != :check_column_id OR swim_lane_id != :check_swim_lane_id THEN :cell_entered_at
  110. ELSE cell_entered_at
  111. END
  112. WHERE id = :id',
  113. [
  114. 'column_id' => $columnId, 'swim_lane_id' => $swimLaneId, 'position' => $position,
  115. 'updated_at' => $updatedAt, 'updated_by' => $updatedBy,
  116. 'check_column_id' => $columnId, 'check_swim_lane_id' => $swimLaneId,
  117. 'cell_entered_at' => $updatedAt, 'id' => $id,
  118. ]
  119. );
  120. }
  121. public function updatePosition(int $id, int $position, string $updatedAt, string $updatedBy): void
  122. {
  123. $this->database->execute(
  124. 'UPDATE cards SET position = :position, updated_at = :updated_at, updated_by = :updated_by WHERE id = :id',
  125. ['position' => $position, 'updated_at' => $updatedAt, 'updated_by' => $updatedBy, 'id' => $id]
  126. );
  127. }
  128. public function deleteByBoardId(int $boardId): void
  129. {
  130. $this->database->execute('DELETE FROM cards WHERE board_id = :board_id', ['board_id' => $boardId]);
  131. }
  132. public function deleteByColumnId(int $columnId): void
  133. {
  134. $this->database->execute('DELETE FROM cards WHERE column_id = :column_id', ['column_id' => $columnId]);
  135. }
  136. public function deleteBySwimLaneId(int $swimLaneId): void
  137. {
  138. $this->database->execute('DELETE FROM cards WHERE swim_lane_id = :swim_lane_id', ['swim_lane_id' => $swimLaneId]);
  139. }
  140. }

Powered by TurnKey Linux.