|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Http\Requests\Chapter\StoreChapterRequest; |
| 7 | +use App\Http\Requests\Chapter\UpdateChapterRequest; |
| 8 | +use App\Http\Requests\Subject\StoreSubjectRequest; |
| 9 | +use App\Http\Requests\Subject\UpdateSubjectRequest; |
| 10 | +use App\Models\Chapter; |
| 11 | +use App\Models\Subject; |
| 12 | +use Illuminate\Http\Request; |
| 13 | +use Illuminate\Support\Facades\DB; |
| 14 | +use Inertia\Inertia; |
| 15 | + |
| 16 | +class CurriculumController extends Controller |
| 17 | +{ |
| 18 | + public function subjectIndex(Request $request) |
| 19 | + { |
| 20 | + $course = $request->query('course', 'hsc'); |
| 21 | + if (! in_array($course, ['hsc', 'ssc'], true)) { |
| 22 | + $course = 'hsc'; |
| 23 | + } |
| 24 | + |
| 25 | + $group = $request->query('group', 'science'); |
| 26 | + if (! in_array($group, ['science', 'humanities', 'commerce'], true)) { |
| 27 | + $group = 'science'; |
| 28 | + } |
| 29 | + |
| 30 | + $subjects = Subject::where('course', $course) |
| 31 | + ->whereIn('group', [$group, 'common']) |
| 32 | + ->orderBy('sort_order', 'asc') |
| 33 | + ->withCount('chapters') |
| 34 | + ->get(); |
| 35 | + |
| 36 | + return Inertia::render('admin/Index', [ |
| 37 | + 'subjects' => $subjects, |
| 38 | + 'current_course' => $course, |
| 39 | + 'current_group' => $group, |
| 40 | + ]); |
| 41 | + } |
| 42 | + |
| 43 | + public function storeSubject(StoreSubjectRequest $request) |
| 44 | + { |
| 45 | + Subject::create($request->validated()); |
| 46 | + |
| 47 | + return back()->with('success', 'Subject created successfully.'); |
| 48 | + } |
| 49 | + |
| 50 | + public function updateSubject(UpdateSubjectRequest $request, Subject $subject) |
| 51 | + { |
| 52 | + $subject->update($request->validated()); |
| 53 | + |
| 54 | + return back()->with('success', 'Subject updated successfully.'); |
| 55 | + } |
| 56 | + |
| 57 | + public function destroySubject(Subject $subject) |
| 58 | + { |
| 59 | + $subject->delete(); |
| 60 | + |
| 61 | + return redirect()->back()->with('success', 'Subject deleted successfully.'); |
| 62 | + } |
| 63 | + |
| 64 | + public function chapterIndex(Subject $subject) |
| 65 | + { |
| 66 | + $chapters = Chapter::where('subject_id', $subject->id) |
| 67 | + ->orderBy('sort_order') |
| 68 | + ->get(); |
| 69 | + |
| 70 | + return Inertia::render('admin/Chapter', [ |
| 71 | + 'subject' => $subject, |
| 72 | + 'chapters' => $chapters, |
| 73 | + 'resources' => [], |
| 74 | + 'breadcrumb' => [], |
| 75 | + ]); |
| 76 | + } |
| 77 | + |
| 78 | + public function storeChapter(StoreChapterRequest $request, Subject $subject) |
| 79 | + { |
| 80 | + $validated = $request->validated(); |
| 81 | + |
| 82 | + Chapter::create([ |
| 83 | + 'user_id' => auth()->id(), |
| 84 | + 'subject_id' => $subject->id, |
| 85 | + 'name' => $validated['name'], |
| 86 | + 'sort_order' => $validated['sort_order'] ?? 0, |
| 87 | + 'weight' => (int) ($validated['weight'] ?? 2), |
| 88 | + ]); |
| 89 | + |
| 90 | + return back()->with('success', 'Chapter created successfully.'); |
| 91 | + } |
| 92 | + |
| 93 | + public function updateChapter(UpdateChapterRequest $request, Subject $subject, Chapter $chapter) |
| 94 | + { |
| 95 | + $validated = $request->validated(); |
| 96 | + |
| 97 | + if (array_key_exists('name', $validated)) { |
| 98 | + $chapter->name = $validated['name']; |
| 99 | + } |
| 100 | + |
| 101 | + if (array_key_exists('sort_order', $validated)) { |
| 102 | + $chapter->sort_order = $validated['sort_order'] ?? 0; |
| 103 | + } |
| 104 | + |
| 105 | + if (array_key_exists('weight', $validated)) { |
| 106 | + $chapter->weight = (int) $validated['weight']; |
| 107 | + } |
| 108 | + |
| 109 | + $chapter->save(); |
| 110 | + |
| 111 | + return back()->with('success', 'Chapter updated successfully.'); |
| 112 | + } |
| 113 | + |
| 114 | + public function batchStoreChapters(Request $request, Subject $subject) |
| 115 | + { |
| 116 | + $validated = $request->validate([ |
| 117 | + 'chapters' => 'nullable|array', |
| 118 | + 'nodes' => 'nullable|array', |
| 119 | + ]); |
| 120 | + |
| 121 | + $inputChapters = $validated['chapters'] ?? $validated['nodes'] ?? []; |
| 122 | + |
| 123 | + $baseSortOrder = Chapter::where('subject_id', $subject->id) |
| 124 | + ->max('sort_order') ?? 0; |
| 125 | + |
| 126 | + $createdCount = 0; |
| 127 | + |
| 128 | + DB::transaction(function () use ($inputChapters, $subject, $baseSortOrder, &$createdCount) { |
| 129 | + foreach ($inputChapters as $index => $chapterData) { |
| 130 | + $chapterName = trim($chapterData['name'] ?? ''); |
| 131 | + if (empty($chapterName)) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + Chapter::create([ |
| 136 | + 'user_id' => auth()->id(), |
| 137 | + 'subject_id' => $subject->id, |
| 138 | + 'name' => $chapterName, |
| 139 | + 'sort_order' => $baseSortOrder + $index + 1, |
| 140 | + 'weight' => (int) ($chapterData['weight'] ?? 2), |
| 141 | + ]); |
| 142 | + |
| 143 | + $createdCount++; |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + return back()->with('success', "Successfully generated {$createdCount} chapters!"); |
| 148 | + } |
| 149 | + |
| 150 | + public function destroyChapter(Chapter $chapter) |
| 151 | + { |
| 152 | + $chapter->delete(); |
| 153 | + |
| 154 | + return redirect()->back()->with('success', 'Chapter deleted successfully.'); |
| 155 | + } |
| 156 | +} |
0 commit comments