KanbanService.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Kanban;
  4. use Illuminate\Database\Eloquent\Collection;
  5. class KanbanService
  6. {
  7. private function baseQuery()
  8. {
  9. return Kanban::with(['createdByUser', 'responsibleUser', 'applicantUnit', 'targetUnit'])
  10. ->withCount('replies');
  11. }
  12. public function getAll(): Collection
  13. {
  14. return $this->baseQuery()
  15. ->orderBy('created_at', 'desc')
  16. ->get();
  17. }
  18. public function getAllForUnit(int $unitId): Collection
  19. {
  20. return $this->baseQuery()
  21. ->visibleToUnit($unitId)
  22. ->orderBy('created_at', 'desc')
  23. ->get();
  24. }
  25. public function findById(int $id): ?Kanban
  26. {
  27. return $this->baseQuery()->find($id);
  28. }
  29. public function create(array $data): Kanban
  30. {
  31. $model = Kanban::create($data);
  32. return $this->findById($model->id);
  33. }
  34. public function update(int $id, array $data): ?Kanban
  35. {
  36. $model = Kanban::find($id);
  37. if (!$model) {
  38. return null;
  39. }
  40. $model->update($data);
  41. return $this->findById($model->id);
  42. }
  43. public function delete(int $id): bool
  44. {
  45. $model = Kanban::find($id);
  46. if (!$model) {
  47. return false;
  48. }
  49. return $model->delete();
  50. }
  51. }