/ app / Http / Services / TaxonomyTaskService.php
TaxonomyTaskService.php
 1  <?php
 2  
 3  namespace App\Http\Services;
 4  
 5  use App\Models\TaxonomyTask;
 6  
 7  class TaxonomyTaskService
 8  {
 9      public static function search(int $taxonomyStageId, string $searchString, string $sortField, string $sortDirection, array|null $filters)
10      {
11          $taxonomyTaskQuery = TaxonomyTask::query()->where('taxonomy_stage_id', $taxonomyStageId);
12  
13          if ($filters && $filters['withTrashed']) {
14              $taxonomyTaskQuery->withTrashed();
15          }
16  
17          if ($searchString) {
18              $taxonomyTaskQuery
19                  ->where('title', 'like', '%' . $searchString . '%')
20                  ->OrWhere('description', 'like', '%' . $searchString . '%');
21          }
22  
23          if ($filters && $filters['created_at_since']) {
24              $taxonomyTaskQuery->where('created_at', '>=', $filters['created_at_since']);
25          }
26  
27          if ($filters && $filters['created_at_to']) {
28              $taxonomyTaskQuery->where('created_at', '<=', $filters['created_at_to']);
29          }
30  
31          return $sortField && $sortDirection
32              ? $taxonomyTaskQuery->orderBy($sortField, $sortDirection)
33              : $taxonomyTaskQuery;
34      }
35  
36      public static function create(array $data)
37      {
38          return TaxonomyTask::create($data);
39      }
40  
41      public static function update(int $id, array $data)
42      {
43          $taxonomyTask = TaxonomyTask::findOrFail($id);
44  
45          return $taxonomyTask->update($data);
46      }
47  
48      public static function delete(int $id)
49      {
50          return TaxonomyTask::findOrFail($id)->delete();
51      }
52  
53      public static function restore(int $id)
54      {
55          return TaxonomyTask::withTrashed()->findOrFail($id)->restore();
56      }
57  }