-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentController.php
More file actions
97 lines (83 loc) · 3.27 KB
/
CommentController.php
File metadata and controls
97 lines (83 loc) · 3.27 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
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreCommentRequest;
use App\Http\Resources\CommentResource;
use App\Http\Resources\UserResource;
use App\Services\CommentService;
use App\Services\ModelResolverService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
class CommentController extends Controller
{
public function __construct(protected ModelResolverService $modelResolver,
protected 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]);
try {
DB::beginTransaction();
$comment = $this->commentService->createComment($validatedData);
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,
]);
DB::commit();
return response()->json([
'new_comment' => new CommentResource($comment),
'user' => new UserResource(Auth::user()),
]);
} catch (ValidationException $e) {
DB::rollBack();
throw $e; // Let Laravel handle validation errors (422)
} catch (NotFoundHttpException $e) {
DB::rollBack();
Log::warning('Comment target not found', [
'error' => $e->getMessage(),
'validated_data' => $validatedData,
'user_id' => Auth::id(),
]);
throw $e; // Let Laravel handle 404 errors
} catch (Throwable $e) {
DB::rollBack();
Log::critical('Failed to save comment', [
'validated_data' => $validatedData,
'user_id' => Auth::id(),
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return response()->json(['message' => 'Failed to save comment'], 500);
}
}
/**
* 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;
}
}