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