Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

116 рядки
4.1KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repositories;
  4. use App\Models\Job;
  5. use Core\Repository;
  6. class JobRepository extends Repository
  7. {
  8. protected string $table = 'job';
  9. protected string $primaryKey = 'id';
  10. public function count(): int
  11. {
  12. $row = $this->database->first('SELECT COUNT(*) AS total FROM job');
  13. return (int) ($row['total'] ?? 0);
  14. }
  15. /** @return list<array<string, mixed>> */
  16. public function allWithDetails(): array
  17. {
  18. return $this->database->query(
  19. 'SELECT j.id, j.campaign_id, j.job_type_id, j.attribute_values,
  20. j.created_at, j.updated_at,
  21. ct.name AS campaign_type_name,
  22. jt.name AS job_type_name,
  23. jt.attributes AS job_type_attributes
  24. FROM job j
  25. INNER JOIN campaign c ON j.campaign_id = c.id
  26. INNER JOIN campaign_type ct ON c.campaign_type_id = ct.id
  27. INNER JOIN job_type jt ON j.job_type_id = jt.id
  28. ORDER BY j.id DESC'
  29. );
  30. }
  31. /** @return list<array<string, mixed>> */
  32. public function allWithDetailsForCampaign(int $campaignId): array
  33. {
  34. return $this->database->query(
  35. 'SELECT j.id, j.campaign_id, j.job_type_id, j.attribute_values,
  36. j.created_at, j.updated_at,
  37. ct.name AS campaign_type_name,
  38. jt.name AS job_type_name,
  39. jt.attributes AS job_type_attributes
  40. FROM job j
  41. INNER JOIN campaign c ON j.campaign_id = c.id
  42. INNER JOIN campaign_type ct ON c.campaign_type_id = ct.id
  43. INNER JOIN job_type jt ON j.job_type_id = jt.id
  44. WHERE j.campaign_id = :campaign_id
  45. ORDER BY j.id DESC',
  46. ['campaign_id' => $campaignId]
  47. );
  48. }
  49. public function findWithDetails(int $id): ?array
  50. {
  51. return $this->database->first(
  52. 'SELECT j.id, j.campaign_id, j.job_type_id, j.attribute_values,
  53. j.created_at, j.updated_at,
  54. ct.name AS campaign_type_name,
  55. jt.name AS job_type_name,
  56. jt.attributes AS job_type_attributes
  57. FROM job j
  58. INNER JOIN campaign c ON j.campaign_id = c.id
  59. INNER JOIN campaign_type ct ON c.campaign_type_id = ct.id
  60. INNER JOIN job_type jt ON j.job_type_id = jt.id
  61. WHERE j.id = :id',
  62. ['id' => $id]
  63. );
  64. }
  65. /** Used after INSERT to recover the generated id for audit logging. */
  66. public function findLatestByCampaignAndType(int $campaignId, int $jobTypeId): ?array
  67. {
  68. return $this->database->first(
  69. 'SELECT TOP (1) * FROM job
  70. WHERE campaign_id = :campaign_id AND job_type_id = :job_type_id
  71. ORDER BY id DESC',
  72. ['campaign_id' => $campaignId, 'job_type_id' => $jobTypeId]
  73. );
  74. }
  75. public function create(Job $job): bool
  76. {
  77. return $this->database->execute(
  78. 'INSERT INTO job (campaign_id, job_type_id, attribute_values)
  79. VALUES (:campaign_id, :job_type_id, :attribute_values)',
  80. [
  81. 'campaign_id' => $job->campaignId,
  82. 'job_type_id' => $job->jobTypeId,
  83. 'attribute_values' => json_encode($job->attributeValues, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
  84. ]
  85. );
  86. }
  87. public function update(Job $job): bool
  88. {
  89. return $this->database->execute(
  90. 'UPDATE job
  91. SET campaign_id = :campaign_id,
  92. job_type_id = :job_type_id,
  93. attribute_values = :attribute_values,
  94. updated_at = CURRENT_TIMESTAMP
  95. WHERE id = :id',
  96. [
  97. 'campaign_id' => $job->campaignId,
  98. 'job_type_id' => $job->jobTypeId,
  99. 'attribute_values' => json_encode($job->attributeValues, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
  100. 'id' => $job->id,
  101. ]
  102. );
  103. }
  104. }

Powered by TurnKey Linux.