68 lines
1.7 KiB
PHP
68 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Course;
|
|
use App\Models\Chapter;
|
|
|
|
class ChapterController extends Controller
|
|
{
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'lesson_section_id' => 'required|exists:lesson_sections,id',
|
|
'title' => 'required|max:255',
|
|
]);
|
|
|
|
// Find the current max order for this section
|
|
$maxOrder = Chapter::where('lesson_section_id', $request->lesson_section_id)->max('order');
|
|
|
|
// If no chapters yet, maxOrder will be null → start at 1
|
|
$nextOrder = $maxOrder ? $maxOrder + 1 : 1;
|
|
|
|
Chapter::create([
|
|
'lesson_section_id' => $request->lesson_section_id,
|
|
'title' => $request->title,
|
|
'order' => $nextOrder,
|
|
]);
|
|
|
|
return back()->with('success', 'Chapter added.');
|
|
}
|
|
|
|
public function destroy(Chapter $chapter)
|
|
{
|
|
$chapter->delete();
|
|
return back()->with('success', 'Chapter removed.');
|
|
}
|
|
|
|
public function reorder(Request $request)
|
|
{
|
|
$request->validate([
|
|
'order' => 'required|string',
|
|
]);
|
|
|
|
$ids = json_decode($request->order, true);
|
|
|
|
if (is_array($ids)) {
|
|
foreach ($ids as $index => $id) {
|
|
Chapter::where('id', $id)->update(['order' => $index + 1]);
|
|
}
|
|
}
|
|
|
|
return back()->with('success', 'Chapter order updated.');
|
|
}
|
|
|
|
public function show(Course $course, Chapter $chapter)
|
|
{
|
|
// (Optional) safety check: make sure chapter belongs to this course
|
|
// via its module / section chain, if you want to enforce that later.
|
|
return view('admin.chapters.show', [
|
|
'course' => $course,
|
|
'chapter' => $chapter,
|
|
]);
|
|
}
|
|
|
|
|
|
} |