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.

78 lines
2.6KB

  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, created_at, created_by, updated_at, updated_by)
  30. VALUES (:board_id, :name, :position, :created_at, :created_by, :updated_at, :updated_by)',
  31. [
  32. 'board_id' => $col->boardId,
  33. 'name' => $col->name,
  34. 'position' => $col->position,
  35. 'created_at' => $col->createdAt,
  36. 'created_by' => $col->createdBy,
  37. 'updated_at' => $col->updatedAt,
  38. 'updated_by' => $col->updatedBy,
  39. ]
  40. );
  41. $row = $this->database->first('SELECT last_insert_rowid() AS id');
  42. $col->id = (int) ($row['id'] ?? 0);
  43. return $col;
  44. }
  45. public function update(BoardColumn $col): void
  46. {
  47. $this->database->execute(
  48. 'UPDATE board_columns SET name = :name, updated_at = :updated_at, updated_by = :updated_by WHERE id = :id',
  49. ['name' => $col->name, 'updated_at' => $col->updatedAt, 'updated_by' => $col->updatedBy, 'id' => $col->id]
  50. );
  51. }
  52. public function updatePosition(int $id, int $position, string $updatedAt, string $updatedBy): void
  53. {
  54. $this->database->execute(
  55. 'UPDATE board_columns SET position = :position, updated_at = :updated_at, updated_by = :updated_by WHERE id = :id',
  56. ['position' => $position, 'updated_at' => $updatedAt, 'updated_by' => $updatedBy, 'id' => $id]
  57. );
  58. }
  59. public function deleteByBoardId(int $boardId): void
  60. {
  61. $this->database->execute('DELETE FROM board_columns WHERE board_id = :board_id', ['board_id' => $boardId]);
  62. }
  63. }

Powered by TurnKey Linux.