14
14
15
15
class PostController extends Controller
16
16
{
17
- use ApiTrait, Media;
17
+ use ApiTrait, Media;
18
18
19
19
public function getPosts ()
20
20
{
21
21
// Retrieve all posts with the user relationship and order them by 'created_at' in descending order
22
22
$ posts = Post::with ('user ' )->orderBy ('created_at ' , 'desc ' )->get ();
23
-
23
+
24
24
// Transform the posts data to include user_name and user_imageUrl
25
25
$ postData = $ posts ->map (function ($ post ) {
26
26
$ data = $ post ->toArray ();
@@ -29,99 +29,99 @@ public function getPosts()
29
29
unset($ data ['user ' ]); // Remove the user relationship to avoid redundancy
30
30
return $ data ;
31
31
});
32
-
32
+
33
33
return $ this ->data (compact ('postData ' ));
34
34
}
35
-
36
-
37
- public function showPost ($ id )
38
- {
39
- $ post = Post::with ('user ' )->find ($ id );
40
-
41
- if (!$ post ) {
42
- return $ this ->errorMessage ([], 'Post not found ' , 404 );
43
- }
44
-
45
- // Convert the Post model and user model to an array
46
- $ postData = $ post ->toArray ();
47
-
48
- // Replace the user_id with user's name and imageUrl
49
- $ postData ['user_name ' ] = $ post ->user ->name ; // Change 'name' to the actual column name in your users table
50
- $ postData ['user_imageUrl ' ] = $ post ->user ->imageUrl ; // Change 'imageUrl' to the actual column name in your users table
51
-
52
- // Unset the user relationship to avoid redundancy
53
- unset($ postData ['user ' ]);
54
-
55
- return $ this ->data ($ postData , 'Post retrieved successfully ' , 200 );
56
- }
57
-
35
+
36
+
37
+ public function showPost ($ id )
38
+ {
39
+ $ post = Post::with ('user ' )->find ($ id );
40
+
41
+ if (!$ post ) {
42
+ return $ this ->errorMessage ([], 'Post not found ' , 404 );
43
+ }
44
+
45
+ // Convert the Post model and user model to an array
46
+ $ postData = $ post ->toArray ();
47
+
48
+ // Replace the user_id with user's name and imageUrl
49
+ $ postData ['user_name ' ] = $ post ->user ->name ; // Change 'name' to the actual column name in your users table
50
+ $ postData ['user_imageUrl ' ] = $ post ->user ->imageUrl ; // Change 'imageUrl' to the actual column name in your users table
51
+
52
+ // Unset the user relationship to avoid redundancy
53
+ unset($ postData ['user ' ]);
54
+
55
+ return $ this ->data ($ postData , 'Post retrieved successfully ' , 200 );
56
+ }
57
+
58
58
59
59
public function createPost (PostRequest $ request )
60
- {
60
+ {
61
61
// Check if either 'content' or 'file_path' is provided
62
62
if (!$ request ->filled ('content ' ) && !$ request ->hasFile ('file_path ' )) {
63
63
return $ this ->errorMessage ([], 'Either content or an image must be provided ' , 422 );
64
64
}
65
-
65
+
66
66
// Create a new post
67
67
$ post = new Post ;
68
- $ post ->user_id = Auth::id ();
69
-
68
+ $ post ->user_id = Auth::id ();
69
+
70
70
// Set content if provided
71
71
if ($ request ->filled ('content ' )) {
72
72
$ post ->content = $ request ->input ('content ' );
73
73
}
74
-
75
- // Handle image upload, if provided
76
- if ($ request ->hasFile ('file_path ' )) {
77
- $ image = $ request ->file ('file_path ' );
78
- $ imagePath = $ this ->upload ($ image , 'posts ' );
79
- $ post ->image_path = "images/posts/ $ imagePath " ;
80
- }
81
-
82
-
74
+
75
+ // Handle image upload, if provided
76
+ if ($ request ->hasFile ('file_path ' )) {
77
+ $ image = $ request ->file ('file_path ' );
78
+ $ imagePath = $ this ->upload ($ image , 'posts ' );
79
+ $ post ->image_path = "images/posts/ $ imagePath " ;
80
+ }
81
+
82
+
83
83
$ post ->save ();
84
84
return $ this ->successMessage ('Post created successfully ' , 201 );
85
85
}
86
-
86
+
87
87
public function editPost (PostRequest $ request , $ id )
88
- {
88
+ {
89
89
$ post = Post::find ($ id );
90
90
if (!$ post ) {
91
91
return $ this ->errorMessage ([], 'Post not found ' , 404 );
92
92
}
93
-
93
+
94
94
if ($ post ->user_id !== Auth::id ()) {
95
95
return $ this ->errorMessage ([], 'You are not authorized to edit this post ' , 403 );
96
96
}
97
-
97
+
98
98
// Check if there are any changes to update
99
99
if (!$ request ->filled ('content ' ) && !$ request ->hasFile ('file_path ' )) {
100
100
return $ this ->errorMessage ([], 'No changes to update ' , 422 );
101
101
}
102
-
102
+
103
103
// Update the post content if provided
104
104
if ($ request ->filled ('content ' )) {
105
105
$ post ->content = $ request ->input ('content ' );
106
106
}
107
-
107
+
108
108
// Handle image upload, if provided
109
- if ($ request ->hasFile ('file_path ' )) {
110
- $ image = $ request ->file ('file_path ' );
111
- $ imagePath = $ this ->upload ($ image , 'posts ' );
112
- $ post ->image_path = "images/posts/ $ imagePath " ;
113
- }
109
+ if ($ request ->hasFile ('file_path ' )) {
110
+ $ image = $ request ->file ('file_path ' );
111
+ $ imagePath = $ this ->upload ($ image , 'posts ' );
112
+ $ post ->image_path = "images/posts/ $ imagePath " ;
113
+ }
114
114
115
115
$ post ->save ();
116
-
116
+
117
117
if ($ request ->filled ('content ' ) || $ request ->hasFile ('file_path ' )) {
118
118
return $ this ->successMessage ('Post updated successfully ' , 200 );
119
119
} else {
120
120
return $ this ->errorMessage ([], 'No changes to update ' , 422 );
121
121
}
122
122
}
123
123
124
-
124
+
125
125
public function deletePost ($ id )
126
126
{
127
127
$ post = Post::find ($ id );
@@ -137,7 +137,7 @@ public function deletePost($id)
137
137
if ($ post ->image_path ) {
138
138
$ this ->delete ($ post ->image_path );
139
139
}
140
-
140
+
141
141
$ post ->delete ();
142
142
return $ this ->successMessage ('Post deleted successfully ' , 200 );
143
143
}
@@ -146,14 +146,14 @@ public function getUserPosts($userId)
146
146
{
147
147
// Find the user by their ID
148
148
$ user = User::find ($ userId );
149
-
149
+
150
150
if (!$ user ) {
151
151
return $ this ->errorMessage ([], 'User not found ' , 404 );
152
152
}
153
-
153
+
154
154
// Retrieve all posts for the user with the user relationship
155
155
$ posts = $ user ->posts ()->orderBy ('created_at ' , 'desc ' )->get ();
156
-
156
+
157
157
// Transform the posts data to include user_name and user_imageUrl
158
158
$ postData = $ posts ->map (function ($ post ) {
159
159
$ data = $ post ->toArray ();
@@ -162,10 +162,7 @@ public function getUserPosts($userId)
162
162
unset($ data ['user ' ]); // Remove the user relationship to avoid redundancy
163
163
return $ data ;
164
164
});
165
-
165
+
166
166
return $ this ->data (compact ('postData ' ));
167
167
}
168
-
169
-
170
-
171
168
}
0 commit comments