|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Models\Resource; |
| 6 | +use App\Models\Subject; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use Inertia\Inertia; |
| 9 | +use Inertia\Response; |
| 10 | + |
| 11 | +class NoteController extends Controller |
| 12 | +{ |
| 13 | + public function index(Request $request): Response |
| 14 | + { |
| 15 | + $course = $request->query('curriculum') ?? $request->query('course'); |
| 16 | + if (! in_array($course, ['hsc', 'ssc'], true)) { |
| 17 | + $course = $request->cookie('preferred_course') === 'ssc' ? 'ssc' : 'hsc'; |
| 18 | + } |
| 19 | + |
| 20 | + $group = $request->query('group', 'science'); |
| 21 | + if (! in_array($group, ['science', 'commerce', 'humanities'], true)) { |
| 22 | + $group = 'science'; |
| 23 | + } |
| 24 | + |
| 25 | + $sort = $request->query('sort', 'top'); |
| 26 | + if (! in_array($sort, ['top', 'latest'], true)) { |
| 27 | + $sort = 'top'; |
| 28 | + } |
| 29 | + |
| 30 | + $type = $request->query('type', 'all'); |
| 31 | + $subjectId = $request->query('subject_id'); |
| 32 | + $nodeId = $request->query('node_id'); |
| 33 | + $search = trim($request->query('search', '')); |
| 34 | + |
| 35 | + $query = Resource::query() |
| 36 | + ->approved() |
| 37 | + ->with([ |
| 38 | + 'user:id,name,username,image_path,institution,is_verified', |
| 39 | + 'subject:id,name,course,slug,group', |
| 40 | + 'node:id,name,slug,subject_id', |
| 41 | + ]); |
| 42 | + |
| 43 | + // Filter by course and group (unless searching specific @username without explicit track) |
| 44 | + $isAuthorSearch = str_starts_with($search, '@'); |
| 45 | + $hasExplicitTrack = $request->filled('curriculum') || $request->filled('course') || $request->filled('group'); |
| 46 | + |
| 47 | + if (! $isAuthorSearch || $hasExplicitTrack) { |
| 48 | + $query->where(function ($q) use ($course, $group) { |
| 49 | + $q->whereHas('subject', function ($s) use ($course, $group) { |
| 50 | + $s->where('course', $course) |
| 51 | + ->whereIn('group', [$group, 'common']); |
| 52 | + })->orWhereHas('node.subject', function ($s) use ($course, $group) { |
| 53 | + $s->where('course', $course) |
| 54 | + ->whereIn('group', [$group, 'common']); |
| 55 | + }); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + if ($subjectId) { |
| 60 | + $query->where(function ($q) use ($subjectId) { |
| 61 | + $q->where('subject_id', $subjectId) |
| 62 | + ->orWhereHas('node', fn ($n) => $n->where('subject_id', $subjectId)); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + if ($nodeId === 'whole' || $nodeId === 'general') { |
| 67 | + $query->whereNull('node_id'); |
| 68 | + } elseif (! empty($nodeId) && is_numeric($nodeId)) { |
| 69 | + $query->where('node_id', (int) $nodeId); |
| 70 | + } |
| 71 | + |
| 72 | + if (in_array($type, ['note', 'video', 'practical', 'other'], true)) { |
| 73 | + $query->where('resource_type', $type); |
| 74 | + } |
| 75 | + |
| 76 | + if (! empty($search)) { |
| 77 | + if ($isAuthorSearch) { |
| 78 | + $username = ltrim($search, '@'); |
| 79 | + $query->whereHas('user', fn ($u) => $u->where('username', $username)); |
| 80 | + } else { |
| 81 | + $query->where(function ($q) use ($search) { |
| 82 | + $q->where('title', 'like', "%{$search}%") |
| 83 | + ->orWhere('description', 'like', "%{$search}%"); |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if ($sort === 'latest') { |
| 89 | + $query->latest('id'); |
| 90 | + } else { |
| 91 | + $query->orderByDesc('vote_score')->latest('id'); |
| 92 | + } |
| 93 | + |
| 94 | + $resources = $query->simplePaginate(16)->withQueryString(); |
| 95 | + |
| 96 | + // Query available subjects for filter dropdown (simple, no caching) |
| 97 | + $subjects = Subject::where('is_disabled', false) |
| 98 | + ->where('course', $course) |
| 99 | + ->whereIn('group', [$group, 'common']) |
| 100 | + ->orderBy('sort_order') |
| 101 | + ->with(['nodes' => function ($q) { |
| 102 | + $q->whereNull('parent_id') |
| 103 | + ->select('id', 'subject_id', 'parent_id', 'name', 'slug') |
| 104 | + ->orderBy('sort_order'); |
| 105 | + }]) |
| 106 | + ->get(['id', 'name', 'slug', 'course', 'group']) |
| 107 | + ->toArray(); |
| 108 | + |
| 109 | + return Inertia::render('Notes/Index', [ |
| 110 | + 'resources' => $resources, |
| 111 | + 'subjects' => $subjects, |
| 112 | + 'filters' => [ |
| 113 | + 'curriculum' => $course, |
| 114 | + 'course' => $course, |
| 115 | + 'group' => $group, |
| 116 | + 'subject_id' => $subjectId ? (int) $subjectId : null, |
| 117 | + 'node_id' => $nodeId, |
| 118 | + 'type' => $type, |
| 119 | + 'sort' => $sort, |
| 120 | + 'search' => $search, |
| 121 | + ], |
| 122 | + ]); |
| 123 | + } |
| 124 | +} |
0 commit comments