| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- use App\Models\Question;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- class QuestionSearchController extends Controller
- {
- public function __invoke(Request $request): JsonResponse
- {
- $query = Question::query();
- if ($search = $request->string('q')->toString()) {
- $query->search($search);
- }
- if ($kpCode = $request->string('kp_code')->toString()) {
- $query->where('kp_code', $kpCode);
- }
- if ($type = $request->string('question_type')->toString()) {
- $query->where('question_type', $type);
- }
- $stageGrade = $this->normalizeStageGrade((int) $request->input('grade'));
- if ($stageGrade !== null) {
- $query->where('grade', $stageGrade);
- }
- $min = $request->float('difficulty_min');
- $max = $request->float('difficulty_max');
- if ($min !== null && $max !== null) {
- $query->whereBetween('difficulty', [$min, $max]);
- }
- $perPage = max(1, min(50, (int) $request->input('per_page', 20)));
- return response()->json($query->paginate($perPage));
- }
- private function normalizeStageGrade(int $grade): ?int
- {
- if ($grade <= 0) {
- return null;
- }
- return $grade <= 9 ? 2 : 3;
- }
- }
|