KanbanService.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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('order', 'asc')
  16. ->orderBy('created_at', 'desc')
  17. ->get();
  18. }
  19. public function getAllForUnit(int $unitId): Collection
  20. {
  21. return $this->baseQuery()
  22. ->visibleToUnit($unitId)
  23. ->orderBy('order', 'asc')
  24. ->orderBy('created_at', 'desc')
  25. ->get();
  26. }
  27. /**
  28. * Bulk-update phase + order for a set of cards (drag-and-drop persistence).
  29. * Expects: [['id' => int, 'phase' => string, 'order' => int], ...]
  30. */
  31. public function reorder(array $items): void
  32. {
  33. foreach ($items as $item) {
  34. Kanban::where('id', $item['id'])->update([
  35. 'phase' => $item['phase'],
  36. 'order' => $item['order'],
  37. ]);
  38. }
  39. }
  40. public function findById(int $id): ?Kanban
  41. {
  42. return $this->baseQuery()->find($id);
  43. }
  44. public function create(array $data): Kanban
  45. {
  46. $model = Kanban::create($data);
  47. return $this->findById($model->id);
  48. }
  49. public function update(int $id, array $data): ?Kanban
  50. {
  51. $model = Kanban::find($id);
  52. if (!$model) {
  53. return null;
  54. }
  55. $model->update($data);
  56. return $this->findById($model->id);
  57. }
  58. public function delete(int $id): bool
  59. {
  60. $model = Kanban::find($id);
  61. if (!$model) {
  62. return false;
  63. }
  64. return $model->delete();
  65. }
  66. }