3 namespace Oxbow\Http\Controllers;
6 use Illuminate\Http\Request;
8 use Illuminate\Support\Facades\Auth;
9 use Illuminate\Support\Str;
10 use Oxbow\Http\Requests;
11 use Oxbow\Repos\BookRepo;
12 use Oxbow\Repos\PageRepo;
14 class BookController extends Controller
21 * BookController constructor.
22 * @param BookRepo $bookRepo
23 * @param PageRepo $pageRepo
25 public function __construct(BookRepo $bookRepo, PageRepo $pageRepo)
27 $this->bookRepo = $bookRepo;
28 $this->pageRepo = $pageRepo;
29 parent::__construct();
33 * Display a listing of the book.
37 public function index()
39 $books = $this->bookRepo->getAll();
40 return view('books/index', ['books' => $books]);
44 * Show the form for creating a new book.
48 public function create()
50 $this->checkPermission('book-create');
51 return view('books/create');
55 * Store a newly created book in storage.
57 * @param Request $request
60 public function store(Request $request)
62 $this->checkPermission('book-create');
63 $this->validate($request, [
64 'name' => 'required|string|max:255',
65 'description' => 'string|max:1000'
67 $book = $this->bookRepo->newFromInput($request->all());
68 $book->slug = $this->bookRepo->findSuitableSlug($book->name);
69 $book->created_by = Auth::user()->id;
70 $book->updated_by = Auth::user()->id;
72 Activity::add($book, 'book_create', $book->id);
73 return redirect($book->getUrl());
77 * Display the specified book.
82 public function show($slug)
84 $book = $this->bookRepo->getBySlug($slug);
85 return view('books/show', ['book' => $book, 'current' => $book]);
89 * Show the form for editing the specified book.
94 public function edit($slug)
96 $this->checkPermission('book-update');
97 $book = $this->bookRepo->getBySlug($slug);
98 return view('books/edit', ['book' => $book, 'current' => $book]);
102 * Update the specified book in storage.
104 * @param Request $request
108 public function update(Request $request, $slug)
110 $this->checkPermission('book-update');
111 $book = $this->bookRepo->getBySlug($slug);
112 $this->validate($request, [
113 'name' => 'required|string|max:255',
114 'description' => 'string|max:1000'
116 $book->fill($request->all());
117 $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
118 $book->updated_by = Auth::user()->id;
120 Activity::add($book, 'book_update', $book->id);
121 return redirect($book->getUrl());
125 * Shows the page to confirm deletion
127 * @return \Illuminate\View\View
129 public function showDelete($bookSlug)
131 $this->checkPermission('book-delete');
132 $book = $this->bookRepo->getBySlug($bookSlug);
133 return view('books/delete', ['book' => $book, 'current' => $book]);
137 * Remove the specified book from storage.
142 public function destroy($bookSlug)
144 $this->checkPermission('book-delete');
145 $book = $this->bookRepo->getBySlug($bookSlug);
146 Activity::addMessage('book_delete', 0, $book->name);
147 $this->bookRepo->destroyBySlug($bookSlug);
148 return redirect('/books');