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;
11 class BookController extends Controller
17 * BookController constructor.
18 * @param BookRepo $bookRepo
20 public function __construct(BookRepo $bookRepo)
22 $this->bookRepo = $bookRepo;
26 * Display a listing of the book.
30 public function index()
32 $books = $this->bookRepo->getAll();
33 return view('books/index', ['books' => $books]);
37 * Show the form for creating a new book.
41 public function create()
43 return view('books/create');
47 * Store a newly created book in storage.
49 * @param Request $request
52 public function store(Request $request)
54 $this->validate($request, [
55 'name' => 'required|string|max:255',
56 'description' => 'string|max:1000'
58 $book = $this->bookRepo->newFromInput($request->all());
59 $slug = Str::slug($book->name);
60 while($this->bookRepo->countBySlug($slug) > 0) {
65 return redirect('/books');
69 * Display the specified book.
74 public function show($slug)
76 $book = $this->bookRepo->getBySlug($slug);
77 return view('books/show', ['book' => $book]);
81 * Show the form for editing the specified book.
86 public function edit($slug)
88 $book = $this->bookRepo->getBySlug($slug);
89 return view('books/edit', ['book' => $book]);
93 * Update the specified book in storage.
95 * @param Request $request
99 public function update(Request $request, $slug)
101 $book = $this->bookRepo->getBySlug($slug);
102 $this->validate($request, [
103 'name' => 'required|string|max:255',
104 'description' => 'string|max:1000'
106 $slug = Str::slug($book->name);
107 while($this->bookRepo->countBySlug($slug) > 0 && $book->slug != $slug) {
112 return redirect('/books');
116 * Remove the specified book from storage.
121 public function destroy($id)
123 $this->bookRepo->destroyById($id);
124 return redirect('/books');