66use Illuminate \Http \Request ;
77use App \Models \Comment ;
88use App \Models \Post ;
9- use App \Models \User ;
109use Illuminate \Support \Facades \Auth ;
1110use App \Http \Requests \CommentRequest ;
1211use App \Traits \ApiTrait ;
@@ -16,68 +15,115 @@ class CommentController extends Controller
1615 use ApiTrait;
1716
1817 public function showAllComments (Post $ post )
19- {
20- $ comments = $ post ->comments ->map (function ($ comment ) {
21- $ user = $ comment ->user ;
22- return [
23- 'id ' => $ comment ->id ,
24- 'content ' => $ comment ->content ,
25- 'post_id ' => $ comment ->post_id ,
26- 'user_id ' => $ comment ->user_id ,
27- 'user_name ' => $ user ->name ,
28- 'user_imageUrl ' => $ user ->imageUrl ,
29- 'deleted_at ' => $ comment ->deleted_at ,
30- 'created_at ' => $ comment ->created_at ,
31- 'updated_at ' => $ comment ->updated_at ,
32- ];
33- });
34-
35- return $ this ->data (compact ('comments ' ));
36- }
37-
18+ {
19+ try {
20+ $ comments = $ post ->comments ->map (function ($ comment ) {
21+ $ user = $ comment ->user ;
22+ return [
23+ 'id ' => $ comment ->id ,
24+ 'content ' => $ comment ->content ,
25+ 'post_id ' => $ comment ->post_id ,
26+ 'user_id ' => $ comment ->user_id ,
27+ 'user_name ' => $ user ->name ,
28+ 'user_imageUrl ' => $ user ->imageUrl ,
29+ 'deleted_at ' => $ comment ->deleted_at ,
30+ 'created_at ' => $ comment ->created_at ,
31+ 'updated_at ' => $ comment ->updated_at ,
32+ ];
33+ });
34+
35+ return $ this ->data (compact ('comments ' ));
36+ } catch (\Exception $ e ) {
37+ return $ this ->errorMessage ([], 'An error occurred while fetching comments ' , 500 );
38+ }
39+ }
3840
3941 public function createComment (CommentRequest $ request , Post $ post )
4042 {
41- // Create a new comment
42- $ comment = new Comment ();
43- $ comment ->content = $ request ->input ('content ' );
44- $ comment ->user_id = Auth::id (); // Set the user ID from the authenticated user
45- $ comment ->post_id = $ post ->id ; // Set the post ID
46- $ comment ->save ();
47-
48- // Update the comments_count for the post
49- $ post ->increment ('comments_count ' );
50-
51- return $ this ->successMessage ('Comment created successfully ' , 201 );
43+ try {
44+ // Create a new comment
45+ $ comment = new Comment ();
46+ $ comment ->content = $ request ->input ('content ' );
47+ $ comment ->user_id = Auth::id (); // Set the user ID from the authenticated user
48+ $ comment ->post_id = $ post ->id ; // Set the post ID
49+ $ comment ->save ();
50+
51+ // Update the comments_count for the post
52+ $ post ->increment ('comments_count ' );
53+
54+ // Fetch all comments for the post
55+ $ comments = $ post ->comments ->map (function ($ comment ) {
56+ $ user = $ comment ->user ;
57+ return [
58+ 'id ' => $ comment ->id ,
59+ 'content ' => $ comment ->content ,
60+ 'post_id ' => $ comment ->post_id ,
61+ 'user_id ' => $ comment ->user_id ,
62+ 'user_name ' => $ user ->name ,
63+ 'user_imageUrl ' => $ user ->imageUrl ,
64+ 'deleted_at ' => $ comment ->deleted_at ,
65+ 'created_at ' => $ comment ->created_at ,
66+ 'updated_at ' => $ comment ->updated_at ,
67+ ];
68+ });
69+
70+ return $ this ->data (compact ('comments ' ), 'Comment created successfully ' , 201 );
71+ } catch (\Exception $ e ) {
72+ return $ this ->errorMessage ([], 'An error occurred while creating the comment ' , 500 );
73+ }
5274 }
5375
5476 public function editComment (CommentRequest $ request , Post $ post , Comment $ comment )
5577 {
56- // Check if the comment belongs to the post
57- if ($ comment ->post_id !== $ post ->id ) {
58- return $ this ->errorMessage ([], 'Comment not found for the specified post ' , 404 );
78+ try {
79+ // Check if the comment belongs to the post
80+ if ($ comment ->post_id !== $ post ->id ) {
81+ return $ this ->errorMessage ([], 'Comment not found for the specified post ' , 404 );
82+ }
83+
84+ // Update the comment content
85+ $ comment ->content = $ request ->input ('content ' );
86+ $ comment ->save ();
87+
88+ // Fetch all comments for the post
89+ $ comments = $ post ->comments ->map (function ($ comment ) {
90+ $ user = $ comment ->user ;
91+ return [
92+ 'id ' => $ comment ->id ,
93+ 'content ' => $ comment ->content ,
94+ 'post_id ' => $ comment ->post_id ,
95+ 'user_id ' => $ comment ->user_id ,
96+ 'user_name ' => $ user ->name ,
97+ 'user_imageUrl ' => $ user ->imageUrl ,
98+ 'deleted_at ' => $ comment ->deleted_at ,
99+ 'created_at ' => $ comment ->created_at ,
100+ 'updated_at ' => $ comment ->updated_at ,
101+ ];
102+ });
103+
104+ return $ this ->data (compact ('comments ' ), 'Comment updated successfully ' , 200 );
105+ } catch (\Exception $ e ) {
106+ return $ this ->errorMessage ([], 'An error occurred while updating the comment ' , 500 );
59107 }
60-
61- // Update the comment content
62- $ comment ->content = $ request ->input ('content ' );
63- $ comment ->save ();
64-
65- return $ this ->successMessage ('Comment updated successfully ' , 200 );
66108 }
67109
68110 public function deleteComment (Post $ post , Comment $ comment )
69111 {
70- // Check if the comment belongs to the post
71- if ($ comment ->post_id !== $ post ->id ) {
72- return $ this ->errorMessage ([], 'Comment not found for the specified post ' , 404 );
73- }
112+ try {
113+ // Check if the comment belongs to the post
114+ if ($ comment ->post_id !== $ post ->id ) {
115+ return $ this ->errorMessage ([], 'Comment not found for the specified post ' , 404 );
116+ }
74117
75- // Decrement the comments_count for the post
76- $ post ->decrement ('comments_count ' );
118+ // Decrement the comments_count for the post
119+ $ post ->decrement ('comments_count ' );
77120
78- // Delete the comment
79- $ comment ->delete ();
121+ // Delete the comment
122+ $ comment ->delete ();
80123
81- return $ this ->successMessage ('Comment deleted successfully ' , 200 );
124+ return $ this ->successMessage ('Comment deleted successfully ' , 200 );
125+ } catch (\Exception $ e ) {
126+ return $ this ->errorMessage ([], 'An error occurred while deleting the comment ' , 500 );
127+ }
82128 }
83129}
0 commit comments