]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Added created_by and updated_by to entities. Fixes #3
[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         $slug = Str::slug($book->name);
65         while($this->bookRepo->countBySlug($slug) > 0) {
66             $slug .= '1';
67         }
68         $book->slug = $slug;
69         $book->created_by = Auth::user()->id;
70         $book->updated_by = Auth::user()->id;
71         $book->save();
72         return redirect('/books');
73     }
74
75     /**
76      * Display the specified book.
77      *
78      * @param $slug
79      * @return Response
80      */
81     public function show($slug)
82     {
83         $book = $this->bookRepo->getBySlug($slug);
84         return view('books/show', ['book' => $book]);
85     }
86
87     /**
88      * Show the form for editing the specified book.
89      *
90      * @param $slug
91      * @return Response
92      */
93     public function edit($slug)
94     {
95         $book = $this->bookRepo->getBySlug($slug);
96         return view('books/edit', ['book' => $book]);
97     }
98
99     /**
100      * Update the specified book in storage.
101      *
102      * @param  Request $request
103      * @param $slug
104      * @return Response
105      */
106     public function update(Request $request, $slug)
107     {
108         $book = $this->bookRepo->getBySlug($slug);
109         $this->validate($request, [
110             'name' => 'required|string|max:255',
111             'description' => 'string|max:1000'
112         ]);
113         $book->fill($request->all());
114         $slug = Str::slug($book->name);
115         while($this->bookRepo->countBySlug($slug) > 0 && $book->slug != $slug) {
116             $slug += '1';
117         }
118         $book->slug = $slug;
119         $book->updated_by = Auth::user()->id;
120         $book->save();
121         return redirect($book->getUrl());
122     }
123
124     /**
125      * Shows the page to confirm deletion
126      * @param $bookSlug
127      * @return \Illuminate\View\View
128      */
129     public function showDelete($bookSlug)
130     {
131         $book = $this->bookRepo->getBySlug($bookSlug);
132         return view('books/delete', ['book' => $book]);
133     }
134
135     /**
136      * Remove the specified book from storage.
137      *
138      * @param $bookSlug
139      * @return Response
140      */
141     public function destroy($bookSlug)
142     {
143         $this->bookRepo->destroyBySlug($bookSlug);
144         return redirect('/books');
145     }
146 }