/ app / Http / Services / NormScopeService.php
NormScopeService.php
  1  <?php
  2  
  3  namespace App\Http\Services;
  4  
  5  use App\Http\Resources\NormScopeResource;
  6  use App\Models\NormScope;
  7  use Exception;
  8  use Illuminate\Support\Facades\DB;
  9  
 10  class NormScopeService
 11  {
 12      /**
 13       * Display a listing of the resource.
 14       *
 15       * @return \Illuminate\Http\Response
 16       */
 17      public static function index(int $normId)
 18      {
 19          return NormScopeResource::collection(NormScope::where('norm_id', $normId)->orderBy('name', 'asc')->paginate(15));
 20      }
 21  
 22      /**
 23       * Create a resource
 24       *
 25       * @return \Illuminate\Http\Response
 26       */
 27      public static function create(array $data)
 28      {
 29          DB::beginTransaction();
 30  
 31          try {
 32              $normScope = NormScope::create([
 33                  'name' => $data['name'],
 34                  'description' => $data['description'],
 35                  'norm_id' => $data['norm_id'],
 36              ]);
 37              $normScope->workbooks()->sync($data['workbooksIds']);
 38          } catch (Exception $e) {
 39              logger()->error($e->getMessage());
 40              DB::rollBack();
 41              throw new Exception(__('norms.norm.scope.notifications.create.error.message'));
 42          }
 43  
 44          DB::commit();
 45          return true;
 46      }
 47  
 48      /**
 49       * Destroy a resource
 50       *
 51       * @return \Illuminate\Http\Response
 52       */
 53      public static function remove(int $scopeId)
 54      {
 55          if (NormScope::destroy($scopeId)) {
 56              return true;
 57          } else {
 58              throw new Exception(__('norms.norm.scope.notifications.remove.error.message'));
 59          }
 60      }
 61  
 62      /**
 63       * Update a resource
 64       *
 65       * @return \Illuminate\Http\Response
 66       */
 67      public static function update(array $data, int $entityId)
 68      {
 69          // Params
 70          $scope = NormScope::findOrFail($entityId);
 71          $scope->name = $data['name'];
 72          $scope->description = $data['description'];
 73          $scope->workbooks()->sync($data['workbooksIds']);
 74  
 75          if ($scope->save()) {
 76              return true;
 77          } else {
 78              throw new Exception(__('norms.norm.scropes.notifications.create.error.message'));
 79          }
 80      }
 81  
 82      /**
 83       * Display a listing of the resource.
 84       *
 85       * @return \Illuminate\Http\Response
 86       */
 87      public static function find(int $normId)
 88      {
 89          return new NormScopeResource(NormScope::where('id', $normId)->first());
 90      }
 91  
 92      /**
 93       * Get results for select
 94       *
 95       * @return \Illuminate\Http\Response
 96       */
 97      public static function getForSelect(int $normId)
 98      {
 99          return NormScope::where('norm_id', $normId)->get(['id', 'name', 'description'])->toArray();
100      }
101  
102      public static function getNormScopes(?int $normId)
103      {
104          return NormScope::query()
105              ->select('id', 'name')
106              ->where('norm_id', '=', $normId)
107              ->orderBy('name')
108              ->get();
109      }
110  }