Sid Gifari From Gifari Industries - BD Cyber Security Team
Home
/
home
/
onlinesk
/
api
/
routes
/
✏️
Editing: skills.php
<?php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use App\Models\User; use App\Models\Skill; /* |-------------------------------------------------------------------------- | SKILLS API (USER SKILLS MANAGEMENT) |-------------------------------------------------------------------------- | | Modul omogućava: | - GET /skills/mine | - POST /skills/mine (sync) | - POST /skills/mine/toggle (attach/detach jedan skill) | | Radi sa user_skills pivot tabelom. | Ako skills ili user_skills tabela NE postoji → 404 graceful fail. | */ Route::middleware(['auth:sanctum'])->group(function () { /* |-------------------------------------------------------------------------- | GET /skills/mine |-------------------------------------------------------------------------- | Vrati VEĆ DODELJENE skillove korisniku. |-------------------------------------------------------------------------- */ Route::get('/skills/mine', function (Request $r) { abort_unless( Schema::hasTable('skills') && Schema::hasTable('user_skills'), 404, 'Skills are not configured.' ); $uid = $r->user()->id; $rows = Skill::select('skills.id', 'skills.name', 'skills.slug', 'user_skills.level') ->join('user_skills', 'user_skills.skill_id', '=', 'skills.id') ->where('user_skills.user_id', $uid) ->orderBy('skills.name') ->get(); return response()->json([ 'data' => $rows ], 200); }); /* |-------------------------------------------------------------------------- | POST /skills/mine |-------------------------------------------------------------------------- | SNIMA KOMPLETNU LISTU skillova za korisnika (sync). |-------------------------------------------------------------------------- | | Podržava: | skills: [ | { id: 1, level: 3 }, | { id: 5, level: 1 } | ] | */ Route::post('/skills/mine', function (Request $r) { abort_unless( Schema::hasTable('skills') && Schema::hasTable('user_skills'), 404, 'Skills are not configured.' ); $data = $r->validate([ 'skills' => 'required|array|max:200', 'skills.*.id' => 'required|integer|exists:skills,id', 'skills.*.level' => 'nullable|integer|min:1|max:5', ]); $uid = $r->user()->id; /** @var \App\Models\User $user */ $user = User::findOrFail($uid); // priprema za sync $syncList = []; foreach ($data['skills'] as $s) { $syncList[$s['id']] = ['level' => $s['level'] ?? null]; } if (method_exists($user, 'skills')) { // koristi Eloquent relaciju ako postoji $user->skills()->sync($syncList); } else { // fallback ako model nema relaciju (kompatibilno sa tvojim starim API-jem!) DB::table('user_skills')->where('user_id', $uid)->delete(); foreach ($syncList as $sid => $pivot) { DB::table('user_skills')->insert([ 'user_id' => $uid, 'skill_id' => $sid, 'level' => $pivot['level'], 'created_at' => now(), 'updated_at' => now(), ]); } } return ['ok' => true]; }); /* |-------------------------------------------------------------------------- | POST /skills/mine/toggle |-------------------------------------------------------------------------- | Attach / detach pojedinačnog skill-a. |-------------------------------------------------------------------------- | | Example: | { "skill_id": 5, "level": 3, "attach": true } | */ Route::post('/skills/mine/toggle', function (Request $r) { abort_unless( Schema::hasTable('skills') && Schema::hasTable('user_skills'), 404, 'Skills are not configured.' ); $data = $r->validate([ 'skill_id' => 'required|integer|exists:skills,id', 'level' => 'nullable|integer|min:1|max:5', 'attach' => 'required|boolean', ]); $uid = $r->user()->id; if ($data['attach']) { DB::table('user_skills')->updateOrInsert( ['user_id' => $uid, 'skill_id' => $data['skill_id']], [ 'level' => $data['level'] ?? null, 'updated_at' => now(), 'created_at' => now(), ] ); } else { DB::table('user_skills')->where([ 'user_id' => $uid, 'skill_id' => $data['skill_id'], ])->delete(); } return ['ok' => true]; }); });
💾 Save
❌ Cancel