diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index bf0250ad..bb2cd111 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -39,11 +39,11 @@ public function store(StoreCommentRequest $request) $comment->content = $validatedData['content']; $comment->user_id = Auth::id(); - $commentableType = $this->modelResolver->getModelClass($validatedData['commentable_type']); + $commentableType = $this->modelResolver->getModelClass($validatedData['commentable_key']); $commentableId = $validatedData['commentable_id']; // Ensure that the model exists - $model = $this->modelResolver->resolve($validatedData['commentable_type'], $commentableId); + $model = $this->modelResolver->resolve($validatedData['commentable_key'], $commentableId); if (!$model) { return response()->json(['message' => 'Model not found'], 404); } @@ -140,7 +140,7 @@ public function store(StoreCommentRequest $request) /** * Display the specified comment, with pagination. */ - public function show(Request $request, string $commentableType, int $commentableId, int $index, int $paginationLimit = -1) + public function show(Request $request, string $commentableKey, int $commentableId, int $index, int $paginationLimit = -1) { if ($paginationLimit == -1) { @@ -149,8 +149,8 @@ public function show(Request $request, string $commentableType, int $commentable $sortBy = $request->query('sort_by', 'top'); - Log::debug("Request is, commentable_type: " . $commentableType . ". id: " . $commentableId . ". index: " . $index . ". Sorting: " . $sortBy); - $paginatedResults = $this->commentService->getPaginatedComments($commentableType, $commentableId, $index, $paginationLimit, $sortBy); + Log::debug("Request is, commentable_type: " . $commentableKey . ". id: " . $commentableId . ". index: " . $index . ". Sorting: " . $sortBy); + $paginatedResults = $this->commentService->getPaginatedComments($commentableKey, $commentableId, $index, $paginationLimit, $sortBy); return $paginatedResults; } diff --git a/app/Http/Controllers/UpvoteController.php b/app/Http/Controllers/UpvoteController.php index e2961ab2..2dc0c9f3 100644 --- a/app/Http/Controllers/UpvoteController.php +++ b/app/Http/Controllers/UpvoteController.php @@ -19,20 +19,20 @@ function __construct(ModelResolverService $modelResolver) } /** - * Upvote a Model (type, id) + * Upvote a Model */ - public function upvote($type, $id) + public function upvote($typeKey, $id) { validator( [ - 'type' => $type, + 'type_key' => $typeKey, ], [ - 'type' => ['required', Rule::in(config('upvotes.upvotable_types'))] + 'type_key' => ['required', Rule::in(config('upvotes.upvotable_keys'))] ] )->validate(); - $model = $this->modelResolver->resolve($type, $id); + $model = $this->modelResolver->resolve($typeKey, $id); if (!$model) { return response()->json(['message' => 'Model not found'], 404); @@ -50,18 +50,18 @@ public function upvote($type, $id) /** * Downvote a Model (type, id) */ - public function downvote($type, $id) + public function downvote($typeKey, $id) { validator( [ - 'type' => $type, + 'type_key' => $typeKey, ], [ - 'type' => ['required', Rule::in(config('upvotes.upvotable_types'))] + 'type_key' => ['required', Rule::in(config('upvotes.upvotable_keys'))] ] )->validate(); - $model = $this->modelResolver->resolve($type, $id); + $model = $this->modelResolver->resolve($typeKey, $id); if (!$model) { return response()->json(['message' => 'Model not found'], 404); diff --git a/app/Http/Requests/Comment/StoreCommentRequest.php b/app/Http/Requests/Comment/StoreCommentRequest.php index c151e359..e27e4502 100644 --- a/app/Http/Requests/Comment/StoreCommentRequest.php +++ b/app/Http/Requests/Comment/StoreCommentRequest.php @@ -34,10 +34,10 @@ public function rules(): array { return [ "commentable_id" => ['required', 'integer'], - "commentable_type" => [ + "commentable_key" => [ 'required', 'string', - Rule::in(config('comment.commentable_types_shorthand')), + Rule::in(config('comment.commentable_keys')), ], "content" => ["required", "string", "max:4000"], "parent_comment_id" => ["nullable", "exists:App\Models\Comment,id"] diff --git a/app/Services/CommentService.php b/app/Services/CommentService.php index 5c267ec8..7910a050 100644 --- a/app/Services/CommentService.php +++ b/app/Services/CommentService.php @@ -28,7 +28,7 @@ function __construct(ModelResolverService $resolver) * @return array */ // TODO: Refactor commentable types, and commentable types short for all other objects - public function getPaginatedComments(string $commentableTypeShorthand, int $commentableId, int $index, int $paginationLimit = -1, string $sortBy = 'top'): array + public function getPaginatedComments(string $commentableKey, int $commentableId, int $index, int $paginationLimit = -1, string $sortBy = 'top'): array { if ($paginationLimit == -1) { @@ -37,15 +37,15 @@ public function getPaginatedComments(string $commentableTypeShorthand, int $comm Validator::make([ 'index' => $index, - 'commentable_type_short' => $commentableTypeShorthand, + 'commentable_key' => $commentableKey, 'pagination_limit' => $paginationLimit, ], [ 'index' => ['required', 'integer', 'min:0'], - 'commentable_type_short' => ['required', Rule::in(config('comment.commentable_types_shorthand'))], + 'commentable_key' => ['required', Rule::in(config('comment.commentable_keys'))], 'pagination_limit' => ['required', 'integer', 'max:' . config('comment.pagination_limit')], ])->validate(); - $commentableType = $this->modelResolver->getModelClass($commentableTypeShorthand); + $commentableType = $this->modelResolver->getModelClass($commentableKey); Log::debug("Request is, commentable_type: {$commentableType}. id: {$commentableId}. index: {$index}"); // Get the root comments: diff --git a/config/comment.php b/config/comment.php index 20f863db..293683a3 100644 --- a/config/comment.php +++ b/config/comment.php @@ -5,6 +5,6 @@ 'max_depth' => 7, // 1-indexed, 1 is the start 'pagination_limit' => 150, 'default_pagination_limit' => 5, - 'commentable_types_shorthand' => ['review', 'comment', 'edit', 'resource'], + 'commentable_keys' => ['review', 'comment', 'edit', 'resource'], 'sortable_options' => ['latest', 'top', 'bottom', 'controversial', 'mine'] ]; \ No newline at end of file diff --git a/config/upvotes.php b/config/upvotes.php index 2c8edb6e..8c0b1f57 100644 --- a/config/upvotes.php +++ b/config/upvotes.php @@ -1,5 +1,5 @@ ['review', 'comment', 'edit', 'resource'] + 'upvotable_keys' => ['review', 'comment', 'edit', 'resource'] ]; \ No newline at end of file diff --git a/database/factories/CommentFactory.php b/database/factories/CommentFactory.php index 4bcb9d82..4c6c22a9 100644 --- a/database/factories/CommentFactory.php +++ b/database/factories/CommentFactory.php @@ -23,9 +23,9 @@ class CommentFactory extends Factory public function definition(): array { // Pick a random commentable type from config. - $commentableName = $this->faker->randomElement(['comment', 'resource']); + $commentableKey = $this->faker->randomElement(['comment', 'resource']); $modelResolver = app(ModelResolverService::class); - $modelClass = $modelResolver->getModelClass($commentableName); + $modelClass = $modelResolver->getModelClass($commentableKey); // Use an existing user or create one. $user = User::inRandomOrder()->first() ?? User::factory()->create(); diff --git a/resources/js/Components/Comments/CommentActionsForm.vue b/resources/js/Components/Comments/CommentActionsForm.vue index 16698661..739c9522 100644 --- a/resources/js/Components/Comments/CommentActionsForm.vue +++ b/resources/js/Components/Comments/CommentActionsForm.vue @@ -18,7 +18,7 @@ const props = defineProps({ const isOpen = ref(false); -const commentableType = inject("commentableType"); +const commentableKey = inject("commentableKey"); const commentableId = inject("commentableId"); const createdNewCommentCallback = inject("createdNewCommentCallback"); @@ -29,7 +29,7 @@ const toggleOpen = () => { const form = reactive({ content: "", commentable_id: commentableId, - commentable_type: commentableType, + commentable_key: commentableKey, parent_comment_id: props.parentCommentId ?? null, errors: {}, processing: false, diff --git a/resources/js/Components/Comments/Commentable.vue b/resources/js/Components/Comments/Commentable.vue index a1f67801..5ffd217e 100644 --- a/resources/js/Components/Comments/Commentable.vue +++ b/resources/js/Components/Comments/Commentable.vue @@ -1,5 +1,5 @@