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.

87 line
3.1KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repositories;
  4. use App\Models\BoardColumn;
  5. use Core\Repository;
  6. class BoardColumnRepository extends Repository
  7. {
  8. protected string $table = 'board_columns';
  9. /** @return BoardColumn[] */
  10. public function findByBoardId(int $boardId): array
  11. {
  12. $rows = $this->database->query(
  13. 'SELECT * FROM board_columns WHERE board_id = :board_id ORDER BY position ASC',
  14. ['board_id' => $boardId]
  15. );
  16. return array_map(fn(array $r) => BoardColumn::fromRow($r), $rows);
  17. }
  18. public function maxPosition(int $boardId): int
  19. {
  20. $row = $this->database->first(
  21. 'SELECT MAX(position) AS max_pos FROM board_columns WHERE board_id = :board_id',
  22. ['board_id' => $boardId]
  23. );
  24. return (int) ($row['max_pos'] ?? -1);
  25. }
  26. public function insert(BoardColumn $col): BoardColumn
  27. {
  28. $this->database->execute(
  29. 'INSERT INTO board_columns (board_id, name, position, show_card_count, created_at, created_by, updated_at, updated_by)
  30. VALUES (:board_id, :name, :position, :show_card_count, :created_at, :created_by, :updated_at, :updated_by)',
  31. [
  32. 'board_id' => $col->boardId,
  33. 'name' => $col->name,
  34. 'position' => $col->position,
  35. 'show_card_count' => $col->showCardCount ? 1 : 0,
  36. 'created_at' => $col->createdAt,
  37. 'created_by' => $col->createdBy,
  38. 'updated_at' => $col->updatedAt,
  39. 'updated_by' => $col->updatedBy,
  40. ]
  41. );
  42. $row = $this->database->first('SELECT last_insert_rowid() AS id');
  43. $col->id = (int) ($row['id'] ?? 0);
  44. return $col;
  45. }
  46. public function update(BoardColumn $col): void
  47. {
  48. $this->database->execute(
  49. 'UPDATE board_columns SET name = :name, updated_at = :updated_at, updated_by = :updated_by WHERE id = :id',
  50. ['name' => $col->name, 'updated_at' => $col->updatedAt, 'updated_by' => $col->updatedBy, 'id' => $col->id]
  51. );
  52. }
  53. public function updateShowCardCount(int $id, bool $show, string $updatedAt, string $updatedBy): void
  54. {
  55. $this->database->execute(
  56. 'UPDATE board_columns SET show_card_count = :show_card_count, updated_at = :updated_at, updated_by = :updated_by WHERE id = :id',
  57. ['show_card_count' => $show ? 1 : 0, 'updated_at' => $updatedAt, 'updated_by' => $updatedBy, 'id' => $id]
  58. );
  59. }
  60. public function updatePosition(int $id, int $position, string $updatedAt, string $updatedBy): void
  61. {
  62. $this->database->execute(
  63. 'UPDATE board_columns SET position = :position, updated_at = :updated_at, updated_by = :updated_by WHERE id = :id',
  64. ['position' => $position, 'updated_at' => $updatedAt, 'updated_by' => $updatedBy, 'id' => $id]
  65. );
  66. }
  67. public function deleteByBoardId(int $boardId): void
  68. {
  69. $this->database->execute('DELETE FROM board_columns WHERE board_id = :board_id', ['board_id' => $boardId]);
  70. }
  71. }

Powered by TurnKey Linux.