6
6
use Illuminate \Http \Request ;
7
7
use App \Models \Comment ;
8
8
use App \Models \Post ;
9
- use App \Models \User ;
10
9
use Illuminate \Support \Facades \Auth ;
11
10
use App \Http \Requests \CommentRequest ;
12
11
use App \Traits \ApiTrait ;
@@ -16,68 +15,115 @@ class CommentController extends Controller
16
15
use ApiTrait;
17
16
18
17
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
+ }
38
40
39
41
public function createComment (CommentRequest $ request , Post $ post )
40
42
{
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
+ }
52
74
}
53
75
54
76
public function editComment (CommentRequest $ request , Post $ post , Comment $ comment )
55
77
{
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 );
59
107
}
60
-
61
- // Update the comment content
62
- $ comment ->content = $ request ->input ('content ' );
63
- $ comment ->save ();
64
-
65
- return $ this ->successMessage ('Comment updated successfully ' , 200 );
66
108
}
67
109
68
110
public function deleteComment (Post $ post , Comment $ comment )
69
111
{
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
+ }
74
117
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 ' );
77
120
78
- // Delete the comment
79
- $ comment ->delete ();
121
+ // Delete the comment
122
+ $ comment ->delete ();
80
123
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
+ }
82
128
}
83
129
}
0 commit comments