Skip to content

Commit

Permalink
Add order by param to request, update comment service
Browse files Browse the repository at this point in the history
  • Loading branch information
iooe committed Dec 10, 2018
1 parent f995b7e commit 7f94852
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
18 changes: 10 additions & 8 deletions src/Http/Controllers/CommentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ class CommentsController extends Controller

/**
* CommentsController constructor.
* @param CommentService $commentService
* @param VoteService $voteService
*/
public function __construct(CommentService $commentService, VoteService $voteService)
public function __construct(VoteService $voteService)
{
$this->middleware(['web', 'auth'], ['except' => ['get']]);
$this->policyPrefix = config('comments.policy_prefix');
$this->commentService = $commentService;
$this->voteService = $voteService;
}

Expand All @@ -59,7 +57,7 @@ public function store(SaveRequest $request)
}

$model = $model::findOrFail($request->commentable_id);
$comment = $this->commentService->createComment(Auth::user(), $model, $message);
$comment = CommentService::createComment(Auth::user(), $model, $message);

$resource = new CommentResource($comment);

Expand All @@ -74,6 +72,7 @@ public function get(GetRequest $request): array
{
$modelPath = $request->commentable_type;
$modelId = $request->commentable_id;
$orderBy = CommentService::orderByRequestAdapter($request);

if (!class_exists($modelPath)) {
throw new \DomainException('Model don\'t exists');
Expand All @@ -88,7 +87,10 @@ public function get(GetRequest $request): array
$model = $modelPath::where(['id' => $modelId])->first();

$count = $model->comments()->count();
$comments = $model->comments()->parentless()->get();
$comments = $model->comments()
->parentless()
->orderBy($orderBy['column'], $orderBy['direction'])
->get();

$resource = CommentResource::collection($comments);

Expand All @@ -109,7 +111,7 @@ public function update(EditRequest $request, Comment $comment)

$message = CommentService::htmlFilter($request->message);

$this->commentService->updateComment($comment, $message);
CommentService::updateComment($comment, $message);

$resource = new CommentResource($comment);

Expand All @@ -131,7 +133,7 @@ public function destroy(Request $request, Comment $comment)
$this->authorize($this->policyPrefix . '.delete', $comment);

try {
$this->commentService->deleteComment($comment);
CommentService::deleteComment($comment);
$response = ['success' => true];
} catch (\DomainException $e) {
$response = ['success' => false, 'message' => $e->getMessage()];
Expand All @@ -154,7 +156,7 @@ public function reply(ReplyRequest $request, Comment $comment)
$this->authorize($this->policyPrefix . '.reply', $comment);
$message = CommentService::htmlFilter($request->message);

$reply = $this->commentService->createComment(Auth::user(), $comment->commentable, $message, $comment);
$reply = CommentService::createComment(Auth::user(), $comment->commentable, $message, $comment);
$resource = new CommentResource($reply);

return $request->ajax() ? ['success' => true, 'comment' => $resource] : redirect()->to(url()->previous() . '#comment-' . $reply->id);
Expand Down
6 changes: 2 additions & 4 deletions src/Http/Controllers/VoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ class VoteController extends Controller

/**
* CommentsController constructor.
* @param CommentService $commentService
* @param VoteService $voteService
*/
public function __construct(CommentService $commentService, VoteService $voteService)
public function __construct(VoteService $voteService)
{
$this->middleware(['web', 'auth']);
$this->policyPrefix = config('comments.policy_prefix');
$this->commentService = $commentService;
$this->voteService = $voteService;
}

Expand All @@ -38,7 +36,7 @@ public function vote(VoteRequest $request, Comment $comment)
$this->authorize($this->policyPrefix . '.vote', $comment);

$this->voteService->make(Auth::user(), $comment, $request->vote);
$rating = $this->commentService->ratingRecalculation($comment);
$rating = CommentService::ratingRecalculation($comment);
$votesCount = $comment->votesCount();

return $request->ajax() ? ['success' => true, 'count' => $votesCount, 'rating' => $rating] : redirect()->to(url()->previous() . '#comment-' . $comment->id);
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Requests/GetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public function rules(): array
{
return [
'commentable_type' => 'required|string',
'commentable_id' => 'required|integer|min:1'
'commentable_id' => 'required|integer|min:1',
'order_by' => 'string|min:1|max:11'
];
}
}
22 changes: 18 additions & 4 deletions src/UseCases/CommentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use tizis\laraComments\Contracts\ICommentable;
use tizis\laraComments\Entity\Comment;
use tizis\laraComments\Http\Requests\GetRequest;

class CommentService
{
Expand All @@ -30,11 +31,24 @@ public static function isCommentable($model): bool
return $model instanceof ICommentable;
}

/**
* @param GetRequest $request
* @return array
*/
public static function orderByRequestAdapter(GetRequest $request): array
{
if ($request->order_by === 'rating') {
return ['column' => 'rating', 'direction' => 'desc'];
}
return ['column' => 'id', 'direction' => 'asc'];
}


/**
* @param string $message
* @return Comment
*/
public function updateComment(Comment $comment, string $message): Comment
public static function updateComment(Comment $comment, string $message): Comment
{
$comment->update([
'comment' => $message
Expand All @@ -50,7 +64,7 @@ public function updateComment(Comment $comment, string $message): Comment
* @param null $parent
* @return Comment
*/
public function createComment($user, ICommentable $model, string $message, $parent = null): Comment
public static function createComment($user, ICommentable $model, string $message, $parent = null): Comment
{

$comment = new Comment();
Expand All @@ -72,7 +86,7 @@ public function createComment($user, ICommentable $model, string $message, $pare
* @param Comment $comment
* @throws \Exception
*/
public function deleteComment(Comment $comment): void
public static function deleteComment(Comment $comment): void
{
if (!$comment->children()->exists()) {
$comment->delete();
Expand All @@ -85,7 +99,7 @@ public function deleteComment(Comment $comment): void
* @param Comment $comment
* @return int
*/
public function ratingRecalculation(Comment $comment): int
public static function ratingRecalculation(Comment $comment): int
{
$rating = 0;
foreach ($comment->votes as $vote) {
Expand Down

0 comments on commit 7f94852

Please sign in to comment.