3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Repos\PageRepo;
9 use BookStack\Exceptions\PermissionsException;
11 use Illuminate\Http\Request;
13 class PageApiController extends ApiController
19 'book_id' => 'required_unless:chapter_id|integer',
20 'chapter_id' => 'required_unless:book_id|integer',
21 'name' => 'required|string|max:255',
22 'html' => 'required_without:markdown|string',
23 'markdown' => 'required_without:html|string',
27 'book_id' => 'required|integer',
28 'chapter_id' => 'required|integer',
29 'name' => 'string|min:1|max:255',
31 'markdown' => 'string',
36 public function __construct(PageRepo $pageRepo)
38 $this->pageRepo = $pageRepo;
42 * Get a listing of pages visible to the user.
44 public function list()
46 $pages = Page::visible();
47 return $this->apiListingResponse($pages, [
48 'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
50 'created_at', 'updated_at', 'created_by', 'updated_by',
55 * Create a new page in the system.
57 public function create(Request $request)
59 $this->validate($request, $this->rules['create']);
61 if ($request->has('chapter_id')) {
62 $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
64 $parent = Book::visible()->findOrFail($request->get('book_id'));
66 $this->checkOwnablePermission('page-create', $parent);
68 $draft = $this->pageRepo->getNewDraftPage($parent);
69 $this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
71 return response()->json($draft->load(['tags']));
75 * View the details of a single page.
77 public function read(string $id)
79 $page = $this->pageRepo->getById($id, ['tags', 'createdBy', 'updatedBy']);
80 return response()->json($page);
84 * Update the details of a single page.
86 public function update(Request $request, string $id)
88 $page = $this->pageRepo->getById($id, []);
89 $this->checkOwnablePermission('page-update', $page);
92 if ($request->has('chapter_id')) {
93 $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
94 } else if ($request->has('book_id')) {
95 $parent = Book::visible()->findOrFail($request->get('book_id'));
98 if ($parent && !$parent->matches($page->getParent())) {
99 $this->checkOwnablePermission('page-delete', $page);
101 $this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
102 } catch (Exception $exception) {
103 if ($exception instanceof PermissionsException) {
104 $this->showPermissionError();
107 return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
111 $updatedPage = $this->pageRepo->update($page, $request->all());
112 return response()->json($updatedPage->load(['tags']));
116 * Delete a page from the system.
118 public function delete(string $id)
120 $page = $this->pageRepo->getById($id, []);
121 $this->checkOwnablePermission('page-delete', $page);
123 $this->pageRepo->destroy($page);
124 return response('', 204);