| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace App\Services;
- use App\Models\Kanban;
- use Illuminate\Database\Eloquent\Collection;
- class KanbanService
- {
- private function baseQuery()
- {
- return Kanban::with(['createdByUser', 'responsibleUser', 'applicantUnit', 'targetUnit'])
- ->withCount('replies');
- }
- public function getAll(): Collection
- {
- return $this->baseQuery()
- ->orderBy('order', 'asc')
- ->orderBy('created_at', 'desc')
- ->get();
- }
- public function getAllForUnit(int $unitId): Collection
- {
- return $this->baseQuery()
- ->visibleToUnit($unitId)
- ->orderBy('order', 'asc')
- ->orderBy('created_at', 'desc')
- ->get();
- }
- /**
- * Bulk-update phase + order for a set of cards (drag-and-drop persistence).
- * Expects: [['id' => int, 'phase' => string, 'order' => int], ...]
- */
- public function reorder(array $items): void
- {
- foreach ($items as $item) {
- Kanban::where('id', $item['id'])->update([
- 'phase' => $item['phase'],
- 'order' => $item['order'],
- ]);
- }
- }
- public function findById(int $id): ?Kanban
- {
- return $this->baseQuery()->find($id);
- }
- public function create(array $data): Kanban
- {
- $model = Kanban::create($data);
- return $this->findById($model->id);
- }
- public function update(int $id, array $data): ?Kanban
- {
- $model = Kanban::find($id);
- if (!$model) {
- return null;
- }
- $model->update($data);
- return $this->findById($model->id);
- }
- public function delete(int $id): bool
- {
- $model = Kanban::find($id);
- if (!$model) {
- return false;
- }
- return $model->delete();
- }
- }
|