/ app / Http / Services / NormService.php
NormService.php
  1  <?php
  2  
  3  namespace App\Http\Services;
  4  
  5  use App\Http\Resources\NormResource;
  6  use App\Models\Norm;
  7  use App\Models\NormScope;
  8  use Exception;
  9  use Illuminate\Support\Collection;
 10  
 11  class NormService
 12  {
 13      /**
 14       * Display a listing of the resource.
 15       *
 16       * @return \Illuminate\Http\Response
 17       */
 18      public static function index()
 19      {
 20          return NormResource::collection(Norm::orderBy('version', 'asc')->paginate(5));
 21      }
 22  
 23      /**
 24       * Create a resource
 25       *
 26       * @return \Illuminate\Http\Response
 27       */
 28      public static function create(array $data)
 29      {
 30          if (Norm::create($data)) {
 31              return true;
 32          } else {
 33              throw new Exception(__('norms.norm.notifications.create.error.message'));
 34          }
 35      }
 36  
 37      /**
 38       * Destroy a resource
 39       *
 40       * @return \Illuminate\Http\Response
 41       */
 42      public static function remove(int $normId)
 43      {
 44          if (Norm::destroy($normId)) {
 45              return true;
 46          } else {
 47              throw new Exception(__('norms.norm.notifications.remove.error.message'));
 48          }
 49      }
 50  
 51      /**
 52       * Update a resource
 53       *
 54       * @return \Illuminate\Http\Response
 55       */
 56      public static function update(array $data, int $entityId)
 57      {
 58          try {
 59              $norm = Norm::findOrFail($entityId);
 60              $norm->update($data);
 61          } catch (Exception $e) {
 62              throw new Exception(__('norms.norm.notifications.update.error.message'));
 63          }
 64          return true;
 65      }
 66  
 67      /**
 68       * Display a listing of the resource.
 69       *
 70       * @return \Illuminate\Http\Response
 71       */
 72      public static function find(int $normId)
 73      {
 74          return new NormResource(Norm::where('id', $normId)->first());
 75      }
 76  
 77      public static function getNorms(?int $normGroupId)
 78      {
 79          return Norm::query()
 80              ->where('norm_group_id', '=', $normGroupId)
 81              ->orderBy('version')
 82              ->get();
 83      }
 84  
 85      public static function getAllNormsSelectables()
 86      {
 87          return Norm::get()
 88              ->map(function ($norm) {
 89                  return [
 90                      'id' => $norm->id,
 91                      'name' => $norm->name,
 92                  ];
 93              });
 94      }
 95  
 96      public static function getWorkbooksByNormAndScope(?int $normId, ?int $normScopeId): Collection
 97      {
 98          if (!$normId) {
 99              return collect();
100          }
101  
102          $norm = Norm::find($normId);
103          $scope = NormScope::find($normScopeId);
104  
105          return $norm->workbooks()->select('id', 'name')
106              ->when($scope, function ($query) use ($scope) {
107                  return $query->whereNotIn('id', $scope->workbooks()->pluck('workbooks.id'));
108              })->get();
109      }
110  
111      public static function applyFilters($query, $filters)
112      {
113          return $query->when($filters['search'], function ($query) use ($filters) {
114              $query->where(fn($query) => $query->whereHas('normGroup', function ($query) use ($filters) {
115                  $query->whereRaw("CONCAT(norm_groups.name, ' ', norms.version) LIKE ?", ['%' . $filters['search'] . '%']);
116              })->orWhere('description', 'like', '%' . $filters['search'] . '%'));
117          })->when($filters['withTrashed'], fn($query) => $query->withTrashed());
118      }
119  
120      public static function restore(int $taskId)
121      {
122          try {
123              Norm::withTrashed()->find($taskId)->restore();
124          } catch (Exception $e) {
125              throw new Exception(__('norms.norm.notifications.restore.error.message'));
126          }
127      }
128  }