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.

142 satır
5.5KB

  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, cell_entered_at, 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, :cell_entered_at, :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. 'cell_entered_at' => $card->cellEnteredAt,
  53. 'created_at' => $card->createdAt,
  54. 'created_by' => $card->createdBy,
  55. 'updated_at' => $card->updatedAt,
  56. 'updated_by' => $card->updatedBy,
  57. ]
  58. );
  59. $row = $this->database->first('SELECT last_insert_rowid() AS id');
  60. $card->id = (int) ($row['id'] ?? 0);
  61. return $card;
  62. }
  63. public function update(Card $card): void
  64. {
  65. $this->database->execute(
  66. 'UPDATE cards
  67. SET job_number = :job_number, job_name = :job_name, customer_name = :customer_name,
  68. delivery_date = :delivery_date, quantity = :quantity, notes = :notes, full_note = :full_note,
  69. updated_at = :updated_at, updated_by = :updated_by
  70. WHERE id = :id',
  71. [
  72. 'job_number' => $card->jobNumber,
  73. 'job_name' => $card->jobName,
  74. 'customer_name' => $card->customerName,
  75. 'delivery_date' => $card->deliveryDate ?: null,
  76. 'quantity' => $card->quantity !== '' ? $card->quantity : null,
  77. 'notes' => $card->notes,
  78. 'full_note' => $card->fullNote,
  79. 'updated_at' => $card->updatedAt,
  80. 'updated_by' => $card->updatedBy,
  81. 'id' => $card->id,
  82. ]
  83. );
  84. }
  85. public function move(int $id, int $columnId, int $swimLaneId, int $position, string $updatedAt, string $updatedBy): void
  86. {
  87. $this->database->execute(
  88. 'UPDATE cards SET column_id = :column_id, swim_lane_id = :swim_lane_id, position = :position,
  89. updated_at = :updated_at, updated_by = :updated_by,
  90. cell_entered_at = CASE
  91. WHEN column_id != :check_column_id OR swim_lane_id != :check_swim_lane_id THEN :cell_entered_at
  92. ELSE cell_entered_at
  93. END
  94. WHERE id = :id',
  95. [
  96. 'column_id' => $columnId, 'swim_lane_id' => $swimLaneId, 'position' => $position,
  97. 'updated_at' => $updatedAt, 'updated_by' => $updatedBy,
  98. 'check_column_id' => $columnId, 'check_swim_lane_id' => $swimLaneId,
  99. 'cell_entered_at' => $updatedAt, 'id' => $id,
  100. ]
  101. );
  102. }
  103. public function updatePosition(int $id, int $position, string $updatedAt, string $updatedBy): void
  104. {
  105. $this->database->execute(
  106. 'UPDATE cards SET position = :position, updated_at = :updated_at, updated_by = :updated_by WHERE id = :id',
  107. ['position' => $position, 'updated_at' => $updatedAt, 'updated_by' => $updatedBy, 'id' => $id]
  108. );
  109. }
  110. public function deleteByBoardId(int $boardId): void
  111. {
  112. $this->database->execute('DELETE FROM cards WHERE board_id = :board_id', ['board_id' => $boardId]);
  113. }
  114. public function deleteByColumnId(int $columnId): void
  115. {
  116. $this->database->execute('DELETE FROM cards WHERE column_id = :column_id', ['column_id' => $columnId]);
  117. }
  118. public function deleteBySwimLaneId(int $swimLaneId): void
  119. {
  120. $this->database->execute('DELETE FROM cards WHERE swim_lane_id = :swim_lane_id', ['swim_lane_id' => $swimLaneId]);
  121. }
  122. }

Powered by TurnKey Linux.