X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/4f231d1bf088b75dca874472962a61da720d0e67..refs/pull/918/head:/app/Http/Controllers/CommentController.php diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index 8e7b1512a..7bf0a2aac 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -1,15 +1,21 @@ entityRepo = $entityRepo; @@ -17,7 +23,14 @@ class CommentController extends Controller parent::__construct(); } - public function save(Request $request, $pageId, $commentId = null) + /** + * Save a new comment for a Page + * @param Request $request + * @param integer $pageId + * @param null|integer $commentId + * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response + */ + public function savePageComment(Request $request, $pageId, $commentId = null) { $this->validate($request, [ 'text' => 'required|string', @@ -30,66 +43,51 @@ class CommentController extends Controller return response('Not found', 404); } - if($page->draft) { - // cannot add comments to drafts. - return response()->json([ - 'status' => 'error', - 'message' => trans('errors.cannot_add_comment_to_draft'), - ], 400); - } - $this->checkOwnablePermission('page-view', $page); - if (empty($commentId)) { - // create a new comment. - $this->checkPermission('comment-create-all'); - $comment = $this->commentRepo->create($page, $request->only(['text', 'html', 'parent_id'])); - $respMsg = trans('entities.comment_created'); - } else { - // update existing comment - // get comment by ID and check if this user has permission to update. - $comment = $this->comment->findOrFail($commentId); - $this->checkOwnablePermission('comment-update', $comment); - $this->commentRepo->update($comment, $request->all()); - $respMsg = trans('entities.comment_updated'); + + // Prevent adding comments to draft pages + if ($page->draft) { + return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400); } - return response()->json([ - 'status' => 'success', - 'message' => $respMsg + // Create a new comment. + $this->checkPermission('comment-create-all'); + $comment = $this->commentRepo->create($page, $request->only(['html', 'text', 'parent_id'])); + Activity::add($page, 'commented_on', $page->book->id); + return view('comments/comment', ['comment' => $comment]); + } + + /** + * Update an existing comment. + * @param Request $request + * @param integer $commentId + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + */ + public function update(Request $request, $commentId) + { + $this->validate($request, [ + 'text' => 'required|string', + 'html' => 'required|string', ]); - } - - public function destroy($id) { - $comment = $this->comment->findOrFail($id); - $this->checkOwnablePermission('comment-delete', $comment); + $comment = $this->commentRepo->getById($commentId); + $this->checkOwnablePermission('page-view', $comment->entity); + $this->checkOwnablePermission('comment-update', $comment); - // + $comment = $this->commentRepo->update($comment, $request->only(['html', 'text'])); + return view('comments/comment', ['comment' => $comment]); } - public function getComments($pageId, $commentId = null) { - try { - $page = $this->entityRepo->getById('page', $pageId, true); - } catch (ModelNotFoundException $e) { - return response('Not found', 404); - } - - if($page->draft) { - // cannot add comments to drafts. - return response()->json([ - 'status' => 'error', - 'message' => trans('errors.no_comments_for_draft'), - ], 400); - } - - $this->checkOwnablePermission('page-view', $page); - - $comments = $this->commentRepo->getCommentsForPage($pageId, $commentId); - if (empty($commentId)) { - // requesting for parent level comments, send the total count as well. - $totalComments = $this->commentRepo->getCommentCount($pageId); - return response()->json(array('success' => true, 'comments'=> $comments, 'total' => $totalComments)); - } - return response()->json(array('success' => true, 'comments'=> $comments)); + /** + * Delete a comment from the system. + * @param integer $id + * @return \Illuminate\Http\JsonResponse + */ + public function destroy($id) + { + $comment = $this->commentRepo->getById($id); + $this->checkOwnablePermission('comment-delete', $comment); + $this->commentRepo->delete($comment); + return response()->json(['message' => trans('entities.comment_deleted')]); } }