3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\CommentRepo;
6 use BookStack\Entities\Models\Page;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
10 class CommentController extends Controller
12 protected $commentRepo;
14 public function __construct(CommentRepo $commentRepo)
16 $this->commentRepo = $commentRepo;
20 * Save a new comment for a Page.
22 * @throws ValidationException
24 public function savePageComment(Request $request, int $pageId)
26 $this->validate($request, [
27 'text' => 'required|string',
28 'parent_id' => 'nullable|integer',
31 $page = Page::visible()->find($pageId);
33 return response('Not found', 404);
36 // Prevent adding comments to draft pages
38 return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
41 // Create a new comment.
42 $this->checkPermission('comment-create-all');
43 $comment = $this->commentRepo->create($page, $request->get('text'), $request->get('parent_id'));
45 return view('comments.comment', ['comment' => $comment]);
49 * Update an existing comment.
51 * @throws ValidationException
53 public function update(Request $request, int $commentId)
55 $this->validate($request, [
56 'text' => 'required|string',
59 $comment = $this->commentRepo->getById($commentId);
60 $this->checkOwnablePermission('page-view', $comment->entity);
61 $this->checkOwnablePermission('comment-update', $comment);
63 $comment = $this->commentRepo->update($comment, $request->get('text'));
65 return view('comments.comment', ['comment' => $comment]);
69 * Delete a comment from the system.
71 public function destroy(int $id)
73 $comment = $this->commentRepo->getById($id);
74 $this->checkOwnablePermission('comment-delete', $comment);
76 $this->commentRepo->delete($comment);
78 return response()->json(['message' => trans('entities.comment_deleted')]);