]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/PageApiController.php
Started pages API
[bookstack] / app / Http / Controllers / Api / PageApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
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;
10 use Exception;
11 use Illuminate\Http\Request;
12
13 class PageApiController extends ApiController
14 {
15     protected $pageRepo;
16
17     protected $rules = [
18         'create' => [
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',
24             'tags' => 'array',
25         ],
26         'update' => [
27             'book_id' => 'required|integer',
28             'chapter_id' => 'required|integer',
29             'name' => 'string|min:1|max:255',
30             'html' => 'string',
31             'markdown' => 'string',
32             'tags' => 'array',
33         ],
34     ];
35
36     public function __construct(PageRepo $pageRepo)
37     {
38         $this->pageRepo = $pageRepo;
39     }
40
41     /**
42      * Get a listing of pages visible to the user.
43      */
44     public function list()
45     {
46         $pages = Page::visible();
47         return $this->apiListingResponse($pages, [
48             'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
49             'draft', 'template',
50             'created_at', 'updated_at', 'created_by', 'updated_by',
51         ]);
52     }
53
54     /**
55      * Create a new page in the system.
56      */
57     public function create(Request $request)
58     {
59         $this->validate($request, $this->rules['create']);
60
61         if ($request->has('chapter_id')) {
62             $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
63         } else {
64             $parent = Book::visible()->findOrFail($request->get('book_id'));
65         }
66         $this->checkOwnablePermission('page-create', $parent);
67
68         $draft = $this->pageRepo->getNewDraftPage($parent);
69         $this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
70
71         return response()->json($draft->load(['tags']));
72     }
73
74     /**
75      * View the details of a single page.
76      */
77     public function read(string $id)
78     {
79         $page = $this->pageRepo->getById($id, ['tags', 'createdBy', 'updatedBy']);
80         return response()->json($page);
81     }
82
83     /**
84      * Update the details of a single page.
85      */
86     public function update(Request $request, string $id)
87     {
88         $page = $this->pageRepo->getById($id, []);
89         $this->checkOwnablePermission('page-update', $page);
90
91         $parent = null;
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'));
96         }
97
98         if ($parent && !$parent->matches($page->getParent())) {
99             $this->checkOwnablePermission('page-delete', $page);
100             try {
101                 $this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
102             } catch (Exception $exception) {
103                 if ($exception instanceof  PermissionsException) {
104                     $this->showPermissionError();
105                 }
106
107                 return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
108             }
109         }
110
111         $updatedPage = $this->pageRepo->update($page, $request->all());
112         return response()->json($updatedPage->load(['tags']));
113     }
114
115     /**
116      * Delete a page from the system.
117      */
118     public function delete(string $id)
119     {
120         $page = $this->pageRepo->getById($id, []);
121         $this->checkOwnablePermission('page-delete', $page);
122
123         $this->pageRepo->destroy($page);
124         return response('', 204);
125     }
126 }