/ app / Http / Services / TaxonomyService.php
TaxonomyService.php
  1  <?php
  2  
  3  namespace App\Http\Services;
  4  
  5  use App\Models\Taxonomy;
  6  use Exception;
  7  use Illuminate\Support\Arr;
  8  use Illuminate\Support\Facades\DB;
  9  
 10  class TaxonomyService
 11  {
 12      public static function search(string $searchString, string $sortField, string $sortDirection, array|null $filters)
 13      {
 14          $taxonomyQuery = Taxonomy::where('name', 'like', '%' . $searchString . '%');
 15  
 16          if ($filters && $filters['withTrashed']) {
 17              $taxonomyQuery->withTrashed();
 18          }
 19  
 20          if ($filters && $filters['created_at_since']) {
 21              $taxonomyQuery->where('created_at', '>=', $filters['created_at_since']);
 22          }
 23  
 24          if ($filters && $filters['created_at_to']) {
 25              $taxonomyQuery->where('created_at', '<=', $filters['created_at_to']);
 26          }
 27  
 28          if ($filters && $filters['type']) {
 29              $taxonomyQuery->whereHas('dossierType.father.grandparent', function ($query) use ($filters) {
 30                  $query->where('id', $filters['type']);
 31              });
 32          }
 33  
 34          if ($filters && $filters['mode']) {
 35              $taxonomyQuery->whereHas('dossierType.father', function ($query) use ($filters) {
 36                  $query->where('id', $filters['mode']);
 37              });
 38          }
 39  
 40          if ($filters && $filters['dossier_type']) {
 41              $taxonomyQuery->where('dossier_type_id', $filters['dossier_type']);
 42          }
 43  
 44          return $sortField && $sortDirection
 45              ? $taxonomyQuery->orderBy($sortField, $sortDirection)
 46              : $taxonomyQuery;
 47      }
 48  
 49      public static function create(array $data)
 50      {
 51  
 52          try {
 53              DB::beginTransaction();
 54  
 55              Taxonomy::create([
 56                  'name' => $data['name'],
 57                  'dossier_type_id' => $data['subtype']
 58              ]);
 59  
 60              DB::commit();
 61  
 62              return true;
 63          } catch (Exception $e) {
 64              DB::rollBack();
 65              throw new Exception($e->getMessage());
 66          }
 67      }
 68  
 69      public static function update(int $entityId, array $data)
 70      {
 71          try {
 72  
 73              DB::beginTransaction();
 74              $taxonomy = Taxonomy::findOrFail($entityId);
 75  
 76              $taxonomy->name = $data['name'];
 77              $taxonomy->dossier_type_id = $data['subtype'];
 78              $taxonomy->save();
 79              DB::commit();
 80  
 81              return true;
 82          } catch (Exception $e) {
 83              DB::rollBack();
 84              throw new Exception(__('dossiers.taxonomy.notifications.update.error.message'));
 85          }
 86      }
 87      public static function delete(int $id)
 88      {
 89          return Taxonomy::findOrFail($id)->delete();
 90      }
 91  
 92      /**
 93       * Restore a resource
 94       *
 95       * @return \Illuminate\Http\Response
 96       */
 97      public static function restore(int $userId)
 98      {
 99          try {
100              DB::beginTransaction();
101              Taxonomy::withTrashed()->find($userId)->restore();
102              DB::commit();
103  
104              return true;
105          } catch (Exception $e) {
106              DB::rollBack();
107              throw new Exception(__('dossiers.taxonomy.notifications.restore.error.message'));
108          }
109      }
110  }