]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Added sidebar highlighting and fixed code elements. Fixes #18
[bookstack] / app / Http / Controllers / BookController.php
1 <?php
2
3 namespace Oxbow\Http\Controllers;
4
5 use Illuminate\Http\Request;
6
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;
12
13 class BookController extends Controller
14 {
15
16     protected $bookRepo;
17     protected $pageRepo;
18
19     /**
20      * BookController constructor.
21      * @param BookRepo $bookRepo
22      * @param PageRepo $pageRepo
23      */
24     public function __construct(BookRepo $bookRepo, PageRepo $pageRepo)
25     {
26         $this->bookRepo = $bookRepo;
27         $this->pageRepo = $pageRepo;
28     }
29
30     /**
31      * Display a listing of the book.
32      *
33      * @return Response
34      */
35     public function index()
36     {
37         $books = $this->bookRepo->getAll();
38         return view('books/index', ['books' => $books]);
39     }
40
41     /**
42      * Show the form for creating a new book.
43      *
44      * @return Response
45      */
46     public function create()
47     {
48         return view('books/create');
49     }
50
51     /**
52      * Store a newly created book in storage.
53      *
54      * @param  Request  $request
55      * @return Response
56      */
57     public function store(Request $request)
58     {
59         $this->validate($request, [
60             'name' => 'required|string|max:255',
61             'description' => 'string|max:1000'
62         ]);
63         $book = $this->bookRepo->newFromInput($request->all());
64         $book->slug = $this->bookRepo->findSuitableSlug($book->name);
65         $book->created_by = Auth::user()->id;
66         $book->updated_by = Auth::user()->id;
67         $book->save();
68         return redirect('/books');
69     }
70
71     /**
72      * Display the specified book.
73      *
74      * @param $slug
75      * @return Response
76      */
77     public function show($slug)
78     {
79         $book = $this->bookRepo->getBySlug($slug);
80         return view('books/show', ['book' => $book, 'current' => $book]);
81     }
82
83     /**
84      * Show the form for editing the specified book.
85      *
86      * @param $slug
87      * @return Response
88      */
89     public function edit($slug)
90     {
91         $book = $this->bookRepo->getBySlug($slug);
92         return view('books/edit', ['book' => $book, 'current' => $book]);
93     }
94
95     /**
96      * Update the specified book in storage.
97      *
98      * @param  Request $request
99      * @param $slug
100      * @return Response
101      */
102     public function update(Request $request, $slug)
103     {
104         $book = $this->bookRepo->getBySlug($slug);
105         $this->validate($request, [
106             'name' => 'required|string|max:255',
107             'description' => 'string|max:1000'
108         ]);
109         $book->fill($request->all());
110         $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
111         $book->updated_by = Auth::user()->id;
112         $book->save();
113         return redirect($book->getUrl());
114     }
115
116     /**
117      * Shows the page to confirm deletion
118      * @param $bookSlug
119      * @return \Illuminate\View\View
120      */
121     public function showDelete($bookSlug)
122     {
123         $book = $this->bookRepo->getBySlug($bookSlug);
124         return view('books/delete', ['book' => $book, 'current' => $book]);
125     }
126
127     /**
128      * Remove the specified book from storage.
129      *
130      * @param $bookSlug
131      * @return Response
132      */
133     public function destroy($bookSlug)
134     {
135         $this->bookRepo->destroyBySlug($bookSlug);
136         return redirect('/books');
137     }
138 }