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;
32 * Display a listing of the book.
36 public function index()
38 $books = $this->bookRepo->getAll();
39 return view('books/index', ['books' => $books]);
43 * Show the form for creating a new book.
47 public function create()
49 return view('books/create');
53 * Store a newly created book in storage.
55 * @param Request $request
58 public function store(Request $request)
60 $this->validate($request, [
61 'name' => 'required|string|max:255',
62 'description' => 'string|max:1000'
64 $book = $this->bookRepo->newFromInput($request->all());
65 $book->slug = $this->bookRepo->findSuitableSlug($book->name);
66 $book->created_by = Auth::user()->id;
67 $book->updated_by = Auth::user()->id;
69 Activity::add($book, 'book_create', $book->id);
70 return redirect('/books');
74 * Display the specified book.
79 public function show($slug)
81 $book = $this->bookRepo->getBySlug($slug);
82 return view('books/show', ['book' => $book, 'current' => $book]);
86 * Show the form for editing the specified book.
91 public function edit($slug)
93 $book = $this->bookRepo->getBySlug($slug);
94 return view('books/edit', ['book' => $book, 'current' => $book]);
98 * Update the specified book in storage.
100 * @param Request $request
104 public function update(Request $request, $slug)
106 $book = $this->bookRepo->getBySlug($slug);
107 $this->validate($request, [
108 'name' => 'required|string|max:255',
109 'description' => 'string|max:1000'
111 $book->fill($request->all());
112 $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
113 $book->updated_by = Auth::user()->id;
115 Activity::add($book, 'book_update', $book->id);
116 return redirect($book->getUrl());
120 * Shows the page to confirm deletion
122 * @return \Illuminate\View\View
124 public function showDelete($bookSlug)
126 $book = $this->bookRepo->getBySlug($bookSlug);
127 return view('books/delete', ['book' => $book, 'current' => $book]);
131 * Remove the specified book from storage.
136 public function destroy($bookSlug)
138 $book = $this->bookRepo->getBySlug($bookSlug);
139 Activity::addMessage('book_delete', 0, $book->name);
140 $this->bookRepo->destroyBySlug($bookSlug);
141 return redirect('/books');