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.

106 lines
3.4KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repositories;
  4. use App\Models\Board;
  5. use Core\Repository;
  6. class BoardRepository extends Repository
  7. {
  8. protected string $table = 'boards';
  9. public function findBySlug(string $slug): ?Board
  10. {
  11. $row = $this->database->first(
  12. 'SELECT * FROM boards WHERE slug = :slug',
  13. ['slug' => $slug]
  14. );
  15. return $row ? Board::fromRow($row) : null;
  16. }
  17. /** @return Board[] */
  18. public function getAll(): array
  19. {
  20. $rows = $this->database->query('SELECT * FROM boards ORDER BY name ASC');
  21. return array_map(fn(array $r) => Board::fromRow($r), $rows);
  22. }
  23. public function slugExists(string $slug, int $excludeId = 0): bool
  24. {
  25. if ($excludeId > 0) {
  26. $row = $this->database->first(
  27. 'SELECT COUNT(*) AS cnt FROM boards WHERE slug = :slug AND id != :id',
  28. ['slug' => $slug, 'id' => $excludeId]
  29. );
  30. } else {
  31. $row = $this->database->first(
  32. 'SELECT COUNT(*) AS cnt FROM boards WHERE slug = :slug',
  33. ['slug' => $slug]
  34. );
  35. }
  36. return (int) ($row['cnt'] ?? 0) > 0;
  37. }
  38. public function uniqueSlug(string $base, int $excludeId = 0): string
  39. {
  40. $candidate = $base;
  41. $suffix = 2;
  42. while ($this->slugExists($candidate, $excludeId)) {
  43. $candidate = $base . '-' . $suffix;
  44. $suffix++;
  45. }
  46. return $candidate;
  47. }
  48. public function insert(Board $board): Board
  49. {
  50. $this->database->execute(
  51. 'INSERT INTO boards
  52. (name, slug, import_from_printstream, printstream_job_name, created_at, created_by, updated_at, updated_by)
  53. VALUES
  54. (:name, :slug, :import_from_printstream, :printstream_job_name, :created_at, :created_by, :updated_at, :updated_by)',
  55. [
  56. 'name' => $board->name,
  57. 'slug' => $board->slug,
  58. 'import_from_printstream' => $board->importFromPrintstream ? 1 : 0,
  59. 'printstream_job_name' => $board->printstreamJobName,
  60. 'created_at' => $board->createdAt,
  61. 'created_by' => $board->createdBy,
  62. 'updated_at' => $board->updatedAt,
  63. 'updated_by' => $board->updatedBy,
  64. ]
  65. );
  66. $row = $this->database->first('SELECT last_insert_rowid() AS id');
  67. $board->id = (int) ($row['id'] ?? 0);
  68. return $board;
  69. }
  70. public function update(Board $board): void
  71. {
  72. $this->database->execute(
  73. 'UPDATE boards
  74. SET name = :name, slug = :slug, import_from_printstream = :import_from_printstream,
  75. printstream_job_name = :printstream_job_name, updated_at = :updated_at, updated_by = :updated_by
  76. WHERE id = :id',
  77. [
  78. 'name' => $board->name,
  79. 'slug' => $board->slug,
  80. 'import_from_printstream' => $board->importFromPrintstream ? 1 : 0,
  81. 'printstream_job_name' => $board->printstreamJobName,
  82. 'updated_at' => $board->updatedAt,
  83. 'updated_by' => $board->updatedBy,
  84. 'id' => $board->id,
  85. ]
  86. );
  87. }
  88. }

Powered by TurnKey Linux.