3 namespace Oxbow\Http\Controllers;
5 use Illuminate\Http\Request;
7 use Illuminate\Support\Facades\Auth;
8 use Illuminate\Support\Str;
9 use Oxbow\Http\Requests;
10 use Oxbow\Repos\BookRepo;
11 use Oxbow\Repos\PageRepo;
13 class BookController extends Controller
20 * BookController constructor.
21 * @param BookRepo $bookRepo
22 * @param PageRepo $pageRepo
24 public function __construct(BookRepo $bookRepo, PageRepo $pageRepo)
26 $this->bookRepo = $bookRepo;
27 $this->pageRepo = $pageRepo;
31 * Display a listing of the book.
35 public function index()
37 $books = $this->bookRepo->getAll();
38 return view('books/index', ['books' => $books]);
42 * Show the form for creating a new book.
46 public function create()
48 return view('books/create');
52 * Store a newly created book in storage.
54 * @param Request $request
57 public function store(Request $request)
59 $this->validate($request, [
60 'name' => 'required|string|max:255',
61 'description' => 'string|max:1000'
63 $book = $this->bookRepo->newFromInput($request->all());
64 $slug = Str::slug($book->name);
65 while($this->bookRepo->countBySlug($slug) > 0) {
69 $book->created_by = Auth::user()->id;
70 $book->updated_by = Auth::user()->id;
72 return redirect('/books');
76 * Display the specified book.
81 public function show($slug)
83 $book = $this->bookRepo->getBySlug($slug);
84 return view('books/show', ['book' => $book]);
88 * Show the form for editing the specified book.
93 public function edit($slug)
95 $book = $this->bookRepo->getBySlug($slug);
96 return view('books/edit', ['book' => $book]);
100 * Update the specified book in storage.
102 * @param Request $request
106 public function update(Request $request, $slug)
108 $book = $this->bookRepo->getBySlug($slug);
109 $this->validate($request, [
110 'name' => 'required|string|max:255',
111 'description' => 'string|max:1000'
113 $book->fill($request->all());
114 $slug = Str::slug($book->name);
115 while($this->bookRepo->countBySlug($slug) > 0 && $book->slug != $slug) {
119 $book->updated_by = Auth::user()->id;
121 return redirect($book->getUrl());
125 * Shows the page to confirm deletion
127 * @return \Illuminate\View\View
129 public function showDelete($bookSlug)
131 $book = $this->bookRepo->getBySlug($bookSlug);
132 return view('books/delete', ['book' => $book]);
136 * Remove the specified book from storage.
141 public function destroy($bookSlug)
143 $this->bookRepo->destroyBySlug($bookSlug);
144 return redirect('/books');