Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

127 строки
4.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, show_card_age, card_age_warning_days,
  53. created_at, created_by, updated_at, updated_by)
  54. VALUES
  55. (:name, :slug, :import_from_printstream, :printstream_job_name, :show_card_age, :card_age_warning_days,
  56. :created_at, :created_by, :updated_at, :updated_by)',
  57. [
  58. 'name' => $board->name,
  59. 'slug' => $board->slug,
  60. 'import_from_printstream' => $board->importFromPrintstream ? 1 : 0,
  61. 'printstream_job_name' => $board->printstreamJobName,
  62. 'show_card_age' => $board->showCardAge ? 1 : 0,
  63. 'card_age_warning_days' => $board->cardAgeWarningDays,
  64. 'created_at' => $board->createdAt,
  65. 'created_by' => $board->createdBy,
  66. 'updated_at' => $board->updatedAt,
  67. 'updated_by' => $board->updatedBy,
  68. ]
  69. );
  70. $row = $this->database->first('SELECT last_insert_rowid() AS id');
  71. $board->id = (int) ($row['id'] ?? 0);
  72. return $board;
  73. }
  74. public function update(Board $board): void
  75. {
  76. $this->database->execute(
  77. 'UPDATE boards
  78. SET name = :name, slug = :slug, import_from_printstream = :import_from_printstream,
  79. printstream_job_name = :printstream_job_name, updated_at = :updated_at, updated_by = :updated_by
  80. WHERE id = :id',
  81. [
  82. 'name' => $board->name,
  83. 'slug' => $board->slug,
  84. 'import_from_printstream' => $board->importFromPrintstream ? 1 : 0,
  85. 'printstream_job_name' => $board->printstreamJobName,
  86. 'updated_at' => $board->updatedAt,
  87. 'updated_by' => $board->updatedBy,
  88. 'id' => $board->id,
  89. ]
  90. );
  91. }
  92. public function updateCardAgeSettings(int $id, bool $showCardAge, int $cardAgeWarningDays, string $updatedAt, string $updatedBy): void
  93. {
  94. $this->database->execute(
  95. 'UPDATE boards
  96. SET show_card_age = :show_card_age, card_age_warning_days = :card_age_warning_days,
  97. updated_at = :updated_at, updated_by = :updated_by
  98. WHERE id = :id',
  99. [
  100. 'show_card_age' => $showCardAge ? 1 : 0,
  101. 'card_age_warning_days' => $cardAgeWarningDays,
  102. 'updated_at' => $updatedAt,
  103. 'updated_by' => $updatedBy,
  104. 'id' => $id,
  105. ]
  106. );
  107. }
  108. }

Powered by TurnKey Linux.