3 namespace Oxbow\Http\Controllers;
5 use Illuminate\Http\Request;
7 use Illuminate\Support\Str;
8 use Oxbow\Http\Requests;
9 use Oxbow\Repos\BookRepo;
10 use Oxbow\Repos\PageRepo;
12 class BookController extends Controller
19 * BookController constructor.
20 * @param BookRepo $bookRepo
21 * @param PageRepo $pageRepo
23 public function __construct(BookRepo $bookRepo, PageRepo $pageRepo)
25 $this->bookRepo = $bookRepo;
26 $this->pageRepo = $pageRepo;
30 * Display a listing of the book.
34 public function index()
36 $books = $this->bookRepo->getAll();
37 return view('books/index', ['books' => $books]);
41 * Show the form for creating a new book.
45 public function create()
47 return view('books/create');
51 * Store a newly created book in storage.
53 * @param Request $request
56 public function store(Request $request)
58 $this->validate($request, [
59 'name' => 'required|string|max:255',
60 'description' => 'string|max:1000'
62 $book = $this->bookRepo->newFromInput($request->all());
63 $slug = Str::slug($book->name);
64 while($this->bookRepo->countBySlug($slug) > 0) {
69 return redirect('/books');
73 * Display the specified book.
78 public function show($slug)
80 $book = $this->bookRepo->getBySlug($slug);
81 return view('books/show', ['book' => $book]);
85 * Show the form for editing the specified book.
90 public function edit($slug)
92 $book = $this->bookRepo->getBySlug($slug);
93 return view('books/edit', ['book' => $book]);
97 * Update the specified book in storage.
99 * @param Request $request
103 public function update(Request $request, $slug)
105 $book = $this->bookRepo->getBySlug($slug);
106 $this->validate($request, [
107 'name' => 'required|string|max:255',
108 'description' => 'string|max:1000'
110 $slug = Str::slug($book->name);
111 while($this->bookRepo->countBySlug($slug) > 0 && $book->slug != $slug) {
116 return redirect('/books');
120 * Remove the specified book from storage.
125 public function destroy($id)
127 $this->bookRepo->destroyById($id);
128 return redirect('/books');