3 namespace Oxbow\Http\Controllers;
5 use Illuminate\Http\Request;
7 use Oxbow\Http\Requests;
8 use Oxbow\Http\Controllers\Controller;
9 use Oxbow\Repos\BookRepo;
10 use Oxbow\Repos\ChapterRepo;
12 class ChapterController extends Controller
16 protected $chapterRepo;
19 * ChapterController constructor.
23 public function __construct(BookRepo $bookRepo,ChapterRepo $chapterRepo)
25 $this->bookRepo = $bookRepo;
26 $this->chapterRepo = $chapterRepo;
31 * Show the form for creating a new chapter.
36 public function create($bookSlug)
38 $book = $this->bookRepo->getBySlug($bookSlug);
39 return view('chapters/create', ['book' => $book]);
43 * Store a newly created chapter in storage.
46 * @param Request $request
49 public function store($bookSlug, Request $request)
51 $this->validate($request, [
52 'name' => 'required|string|max:255'
55 $book = $this->bookRepo->getBySlug($bookSlug);
56 $chapter = $this->chapterRepo->newFromInput($request->all());
57 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id);
58 $chapter->priority = $this->bookRepo->getNewPriority($book);
59 $book->chapters()->save($chapter);
60 return redirect($book->getUrl());
64 * Display the specified chapter.
70 public function show($bookSlug, $chapterSlug)
72 $book = $this->bookRepo->getBySlug($bookSlug);
73 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
74 return view('chapters/show', ['book' => $book, 'chapter' => $chapter]);
78 * Show the form for editing the specified chapter.
84 public function edit($bookSlug, $chapterSlug)
86 $book = $this->bookRepo->getBySlug($bookSlug);
87 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
88 return view('chapters/edit', ['book' => $book, 'chapter' => $chapter]);
92 * Update the specified chapter in storage.
94 * @param Request $request
99 public function update(Request $request, $bookSlug, $chapterSlug)
101 $book = $this->bookRepo->getBySlug($bookSlug);
102 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
103 $chapter->fill($request->all());
104 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
106 return redirect($chapter->getUrl());
110 * Shows the page to confirm deletion of this chapter.
112 * @param $chapterSlug
113 * @return \Illuminate\View\View
115 public function showDelete($bookSlug, $chapterSlug)
117 $book = $this->bookRepo->getBySlug($bookSlug);
118 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
119 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter]);
123 * Remove the specified chapter from storage.
126 * @param $chapterSlug
129 public function destroy($bookSlug, $chapterSlug)
131 $book = $this->bookRepo->getBySlug($bookSlug);
132 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
133 if(count($chapter->pages) > 0) {
134 foreach($chapter->pages as $page) {
135 $page->chapter_id = 0;
140 return redirect($book->getUrl());