]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/CommentController.php
Added status endpoint
[bookstack] / app / Http / Controllers / CommentController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Actions\ActivityType;
5 use BookStack\Actions\CommentRepo;
6 use BookStack\Entities\Models\Page;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
9
10 class CommentController extends Controller
11 {
12     protected $commentRepo;
13
14     public function __construct(CommentRepo $commentRepo)
15     {
16         $this->commentRepo = $commentRepo;
17     }
18
19     /**
20      * Save a new comment for a Page
21      * @throws ValidationException
22      */
23     public function savePageComment(Request $request, int $pageId)
24     {
25         $this->validate($request, [
26             'text' => 'required|string',
27             'parent_id' => 'nullable|integer',
28         ]);
29
30         $page = Page::visible()->find($pageId);
31         if ($page === null) {
32             return response('Not found', 404);
33         }
34
35         // Prevent adding comments to draft pages
36         if ($page->draft) {
37             return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
38         }
39
40         // Create a new comment.
41         $this->checkPermission('comment-create-all');
42         $comment = $this->commentRepo->create($page, $request->get('text'), $request->get('parent_id'));
43         return view('comments.comment', ['comment' => $comment]);
44     }
45
46     /**
47      * Update an existing comment.
48      * @throws ValidationException
49      */
50     public function update(Request $request, int $commentId)
51     {
52         $this->validate($request, [
53             'text' => 'required|string',
54         ]);
55
56         $comment = $this->commentRepo->getById($commentId);
57         $this->checkOwnablePermission('page-view', $comment->entity);
58         $this->checkOwnablePermission('comment-update', $comment);
59
60         $comment = $this->commentRepo->update($comment, $request->get('text'));
61         return view('comments.comment', ['comment' => $comment]);
62     }
63
64     /**
65      * Delete a comment from the system.
66      */
67     public function destroy(int $id)
68     {
69         $comment = $this->commentRepo->getById($id);
70         $this->checkOwnablePermission('comment-delete', $comment);
71
72         $this->commentRepo->delete($comment);
73         return response()->json(['message' => trans('entities.comment_deleted')]);
74     }
75 }