-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentController.php
More file actions
163 lines (138 loc) · 5.53 KB
/
CommentController.php
File metadata and controls
163 lines (138 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace App\Http\Controllers;
use App\Http\Requests\Comment\StoreCommentRequest;
use App\Http\Resources\CommentResource;
use App\Http\Resources\UserResource;
use App\Models\Comment;
use App\Services\CommentService;
use App\Services\ModelResolverService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Log;
class CommentController extends Controller
{
protected $modelResolver;
protected $commentService;
public function __construct(ModelResolverService $modelResolver, CommentService $commentService)
{
$this->modelResolver = $modelResolver;
$this->commentService = $commentService;
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreCommentRequest $request)
{
$validatedData = $request->validated();
Log::debug('Comment Controller Store', ['validated data' => $validatedData]);
$comment = new Comment;
$comment->content = $validatedData['content'];
$comment->user_id = Auth::id();
$commentableType = $this->modelResolver->getModelClass($validatedData['commentable_key']);
$commentableId = $validatedData['commentable_id'];
// Ensure that the model exists
$model = $this->modelResolver->resolve($validatedData['commentable_key'], $commentableId);
if (! $model) {
return response()->json(['message' => 'Model not found'], 404);
}
// Set the commentable type
$comment->commentable_type = $commentableType;
$comment->commentable_id = $commentableId;
// Top level comment
$parentCommentId = $validatedData['parent_comment_id'];
if (! $parentCommentId) {
$comment->parent_comment_id = null;
$comment->depth = 1;
$comment->children_count = 0;
}
// Is reply to a comment
else {
$parent = Comment::find($parentCommentId);
$new_comment_depth = $parent->depth + 1;
// Check if the parent is the root comment
if ($parent->depth == 1) {
$root_comment = $parent;
$root_comment_id = $parent->id;
} else {
// If not, fetch the root comment
$root_comment_id = $parent->root_comment_id;
$root_comment = Comment::find($root_comment_id);
}
$replies_count = $root_comment->children_count ?? 0;
// Ensure that they are commenting to the same root
// And the depth is not exceeded
Validator::validate(
[
'commentable_id' => $commentableId,
'commentable_type' => $commentableType,
'depth' => $new_comment_depth,
'replies_count' => $replies_count,
],
[
'commentable_id' => [
'required',
Rule::in([$parent->commentable_id]),
],
'commentable_type' => [
'required',
Rule::in([$parent->commentable_type]),
],
// Cannot exceed the max depth
'depth' => [
'required',
'integer',
'lte:'.(config('comment.max_depth')),
],
// Cannot exceed max replies
'replies_count' => [
'required',
'integer',
'lt:'.(config('comment.max_replies')),
],
]
);
// Set the parent id
$comment->parent_comment_id = $parentCommentId;
// Set the parent's root as this comment's root, unless it is the root itself.
$comment->root_comment_id = $root_comment_id;
// Set the new depth
$comment->depth = $new_comment_depth;
// Update the children count for root
$root_comment->children_count = $root_comment->children_count + 1;
$root_comment->save();
}
$comment->save();
Log::debug('New comment saved', [
'comment_id' => $comment->id,
'user_id' => $comment->user_id,
'commentable_type' => $comment->commentable_type,
'commentable_id' => $comment->commentable_id,
'depth' => $comment->depth,
]);
return response()->json([
'new_comment' => new CommentResource($comment),
'user' => new UserResource(Auth::user()),
]);
}
/**
* Display the specified comment, with pagination.
*/
public function show(Request $request, string $commentableKey, int $commentableId, int $index, int $paginationLimit = -1)
{
if ($paginationLimit == -1) {
$paginationLimit = config('comment.default_pagination_limit');
}
$sortBy = $request->query('sort_by', 'top');
Log::debug('Processing comment show request', [
'commentable_key' => $commentableKey,
'commentable_id' => $commentableId,
'index' => $index,
'sort_by' => $sortBy,
'pagination_limit' => $paginationLimit,
]);
$paginatedResults = $this->commentService->getPaginatedComments($commentableKey, $commentableId, $index, $paginationLimit, $sortBy);
return $paginatedResults;
}
}