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.

132 lines
5.1KB

  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. public function maxPosition(int $columnId, int $swimLaneId): int
  24. {
  25. $row = $this->database->first(
  26. 'SELECT MAX(position) AS max_pos FROM cards WHERE column_id = :col AND swim_lane_id = :lane',
  27. ['col' => $columnId, 'lane' => $swimLaneId]
  28. );
  29. return (int) ($row['max_pos'] ?? -1);
  30. }
  31. public function insert(Card $card): Card
  32. {
  33. $this->database->execute(
  34. 'INSERT INTO cards
  35. (board_id, column_id, swim_lane_id, job_number, job_name, customer_name, delivery_date,
  36. quantity, notes, full_note, position, created_at, created_by, updated_at, updated_by)
  37. VALUES
  38. (:board_id, :column_id, :swim_lane_id, :job_number, :job_name, :customer_name, :delivery_date,
  39. :quantity, :notes, :full_note, :position, :created_at, :created_by, :updated_at, :updated_by)',
  40. [
  41. 'board_id' => $card->boardId,
  42. 'column_id' => $card->columnId,
  43. 'swim_lane_id' => $card->swimLaneId,
  44. 'job_number' => $card->jobNumber,
  45. 'job_name' => $card->jobName,
  46. 'customer_name' => $card->customerName,
  47. 'delivery_date' => $card->deliveryDate ?: null,
  48. 'quantity' => $card->quantity !== '' ? $card->quantity : null,
  49. 'notes' => $card->notes,
  50. 'full_note' => $card->fullNote,
  51. 'position' => $card->position,
  52. 'created_at' => $card->createdAt,
  53. 'created_by' => $card->createdBy,
  54. 'updated_at' => $card->updatedAt,
  55. 'updated_by' => $card->updatedBy,
  56. ]
  57. );
  58. $row = $this->database->first('SELECT last_insert_rowid() AS id');
  59. $card->id = (int) ($row['id'] ?? 0);
  60. return $card;
  61. }
  62. public function update(Card $card): void
  63. {
  64. $this->database->execute(
  65. 'UPDATE cards
  66. SET job_number = :job_number, job_name = :job_name, customer_name = :customer_name,
  67. delivery_date = :delivery_date, quantity = :quantity, notes = :notes, full_note = :full_note,
  68. updated_at = :updated_at, updated_by = :updated_by
  69. WHERE id = :id',
  70. [
  71. 'job_number' => $card->jobNumber,
  72. 'job_name' => $card->jobName,
  73. 'customer_name' => $card->customerName,
  74. 'delivery_date' => $card->deliveryDate ?: null,
  75. 'quantity' => $card->quantity !== '' ? $card->quantity : null,
  76. 'notes' => $card->notes,
  77. 'full_note' => $card->fullNote,
  78. 'updated_at' => $card->updatedAt,
  79. 'updated_by' => $card->updatedBy,
  80. 'id' => $card->id,
  81. ]
  82. );
  83. }
  84. public function move(int $id, int $columnId, int $swimLaneId, int $position, string $updatedAt, string $updatedBy): void
  85. {
  86. $this->database->execute(
  87. 'UPDATE cards SET column_id = :column_id, swim_lane_id = :swim_lane_id, position = :position,
  88. updated_at = :updated_at, updated_by = :updated_by WHERE id = :id',
  89. ['column_id' => $columnId, 'swim_lane_id' => $swimLaneId, 'position' => $position,
  90. 'updated_at' => $updatedAt, 'updated_by' => $updatedBy, 'id' => $id]
  91. );
  92. }
  93. public function updatePosition(int $id, int $position, string $updatedAt, string $updatedBy): void
  94. {
  95. $this->database->execute(
  96. 'UPDATE cards SET position = :position, updated_at = :updated_at, updated_by = :updated_by WHERE id = :id',
  97. ['position' => $position, 'updated_at' => $updatedAt, 'updated_by' => $updatedBy, 'id' => $id]
  98. );
  99. }
  100. public function deleteByBoardId(int $boardId): void
  101. {
  102. $this->database->execute('DELETE FROM cards WHERE board_id = :board_id', ['board_id' => $boardId]);
  103. }
  104. public function deleteByColumnId(int $columnId): void
  105. {
  106. $this->database->execute('DELETE FROM cards WHERE column_id = :column_id', ['column_id' => $columnId]);
  107. }
  108. public function deleteBySwimLaneId(int $swimLaneId): void
  109. {
  110. $this->database->execute('DELETE FROM cards WHERE swim_lane_id = :swim_lane_id', ['swim_lane_id' => $swimLaneId]);
  111. }
  112. }

Powered by TurnKey Linux.