/ app / Http / Services / StageService.php
StageService.php
 1  <?php
 2  
 3  namespace App\Http\Services;
 4  
 5  use App\Enums\StatusEnum;
 6  use App\Models\Stage;
 7  
 8  class StageService
 9  {
10      public static function search(int $dossierId, string $searchString, string $sortField, string $sortDirection)
11      {
12          $stageQuery = Stage::where('dossier_id', $dossierId)
13              ->where('name', 'like', '%'.$searchString.'%');
14  
15          return $sortField && $sortDirection
16              ? $stageQuery->orderBy($sortField, $sortDirection)
17              : $stageQuery;
18      }
19  
20      public static function create(array $data)
21      {
22          return Stage::create($data);
23      }
24  
25      public static function update(int $id, array $data)
26      {
27          $stage = Stage::findOrFail($id);
28  
29          return $stage->update($data);
30      }
31  
32      public static function delete(int $id)
33      {
34          return Stage::findOrFail($id)->delete();
35      }
36  
37      public static function setStatus(int $stageId, int $statusValue): void
38      {
39          $stage = Stage::findOrFail($stageId);
40  
41          $stage->update(['stage_status_id' => $statusValue]);
42      }
43  
44      public static function getAllStages(int $dossierId)
45      {
46          return Stage::where('dossier_id', $dossierId)->get()->map(function ($stage) {
47              return [
48                  'id' => $stage->id,
49                  'name' => $stage->name,
50              ];
51          });
52      }
53  }