]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Added page editing
[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\Str;
8 use Oxbow\Http\Requests;
9 use Oxbow\Repos\BookRepo;
10 use Oxbow\Repos\PageRepo;
11
12 class BookController extends Controller
13 {
14
15     protected $bookRepo;
16     protected $pageRepo;
17
18     /**
19      * BookController constructor.
20      * @param BookRepo $bookRepo
21      * @param PageRepo $pageRepo
22      */
23     public function __construct(BookRepo $bookRepo, PageRepo $pageRepo)
24     {
25         $this->bookRepo = $bookRepo;
26         $this->pageRepo = $pageRepo;
27     }
28
29     /**
30      * Display a listing of the book.
31      *
32      * @return Response
33      */
34     public function index()
35     {
36         $books = $this->bookRepo->getAll();
37         return view('books/index', ['books' => $books]);
38     }
39
40     /**
41      * Show the form for creating a new book.
42      *
43      * @return Response
44      */
45     public function create()
46     {
47         return view('books/create');
48     }
49
50     /**
51      * Store a newly created book in storage.
52      *
53      * @param  Request  $request
54      * @return Response
55      */
56     public function store(Request $request)
57     {
58         $this->validate($request, [
59             'name' => 'required|string|max:255',
60             'description' => 'string|max:1000'
61         ]);
62         $book = $this->bookRepo->newFromInput($request->all());
63         $slug = Str::slug($book->name);
64         while($this->bookRepo->countBySlug($slug) > 0) {
65             $slug .= '1';
66         }
67         $book->slug = $slug;
68         $book->save();
69         return redirect('/books');
70     }
71
72     /**
73      * Display the specified book.
74      *
75      * @param $slug
76      * @return Response
77      */
78     public function show($slug)
79     {
80         $book = $this->bookRepo->getBySlug($slug);
81         return view('books/show', ['book' => $book]);
82     }
83
84     /**
85      * Show the form for editing the specified book.
86      *
87      * @param $slug
88      * @return Response
89      */
90     public function edit($slug)
91     {
92         $book = $this->bookRepo->getBySlug($slug);
93         return view('books/edit', ['book' => $book]);
94     }
95
96     /**
97      * Update the specified book in storage.
98      *
99      * @param  Request $request
100      * @param $slug
101      * @return Response
102      */
103     public function update(Request $request, $slug)
104     {
105         $book = $this->bookRepo->getBySlug($slug);
106         $this->validate($request, [
107             'name' => 'required|string|max:255',
108             'description' => 'string|max:1000'
109         ]);
110         $slug = Str::slug($book->name);
111         while($this->bookRepo->countBySlug($slug) > 0 && $book->slug != $slug) {
112             $slug += '1';
113         }
114         $book->slug = $slug;
115         $book->save();
116         return redirect('/books');
117     }
118
119     /**
120      * Remove the specified book from storage.
121      *
122      * @param  int  $id
123      * @return Response
124      */
125     public function destroy($id)
126     {
127         $this->bookRepo->destroyById($id);
128         return redirect('/books');
129     }
130 }