/ app / Http / Services / SkillService.php
SkillService.php
  1  <?php
  2  
  3  namespace App\Http\Services;
  4  
  5  use Exception;
  6  use App\Models\EvaluatorSkill;
  7  use Illuminate\Support\Facades\DB;
  8  use Carbon\Carbon;
  9  
 10  class SkillService
 11  {
 12      /**
 13       * Display a listing of the resource.
 14       *
 15       * @return \Illuminate\Http\Response
 16       */
 17      public static function find(int $skillId)
 18      {
 19          return EvaluatorSkill::where('id', $skillId)->first();
 20      }
 21  
 22      /**
 23       * Create resources
 24       *
 25       * @return \Illuminate\Http\Response
 26       */
 27      public static function searchSkills(string $searchString, string $sortField, string $sortDirection, ?array $filters, int $userId)
 28      {
 29          $skillsQuery = EvaluatorSkill::query();
 30  
 31          $skillsQuery->whereRelation('evaluator', 'id', $userId)
 32              ->whereHas('normScope', function ($query) use ($searchString) {
 33                  $query->where('name', 'like', '%' . $searchString . '%');
 34              });
 35  
 36          if ($filters && $filters['created_at_since']) {
 37              $skillsQuery->where('created_at', '>=', $filters['created_at_since']);
 38          }
 39  
 40          if ($filters && $filters['created_at_to']) {
 41              $skillsQuery->where('created_at', '<=', $filters['created_at_to']);
 42          }
 43  
 44          if ($filters && $filters['norm_ids']) {
 45              $skillsQuery->whereHas('normScope.norm', function ($query) use ($filters) {
 46                  $query->whereIn('id', $filters['norm_ids']);
 47              });
 48          }
 49  
 50          if ($sortField && $sortDirection) {
 51              $skillsQuery->orderBy($sortField, $sortDirection);
 52          }
 53  
 54          return $skillsQuery;
 55          // return EvaluatorSkill::whereRelation('evaluator', 'id', $userId)
 56          //     ->whereHas('normScope', function ($query) use ($searchString) {
 57          //         $query->where('name', 'like', '%'.$searchString.'%');
 58          //     })
 59          //     ->orderBy($sortField, $sortDirection);
 60      }
 61  
 62      /**
 63       * Create a resource
 64       *
 65       * @return \Illuminate\Http\Response
 66       */
 67      public static function create(array $skillData): bool|Exception
 68      {
 69          try {
 70              DB::beginTransaction();
 71  
 72              EvaluatorSkill::create([
 73                  'evaluator_id' => $skillData['evaluator_id'],
 74                  'norm_scope_id' => $skillData['norm_scope_id'],
 75                  'obtained_in' => $skillData['obtained_in'],
 76              ]);
 77  
 78              DB::commit();
 79  
 80              return true;
 81          } catch (Exception $e) {
 82              DB::rollBack();
 83              throw new Exception($e->getMessage());
 84          }
 85      }
 86  
 87      /**
 88       * Destroy a resource
 89       *
 90       * @return \Illuminate\Http\Response
 91       */
 92      public static function delete(int $skillId)
 93      {
 94          try {
 95              DB::beginTransaction();
 96  
 97              EvaluatorSkill::findOrFail($skillId)->delete();
 98  
 99              DB::commit();
100  
101              return true;
102          } catch (Exception $e) {
103              DB::rollBack();
104              throw new Exception(__('tasks.notifications.remove.error.message'));
105          }
106      }
107  }