Skip to content

Commit 743b5d9

Browse files
Fix remove image
1 parent da7740f commit 743b5d9

File tree

3 files changed

+143
-106
lines changed

3 files changed

+143
-106
lines changed

app/Http/Controllers/Api/Home/CommentController.php

+95-49
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Http\Request;
77
use App\Models\Comment;
88
use App\Models\Post;
9-
use App\Models\User;
109
use Illuminate\Support\Facades\Auth;
1110
use App\Http\Requests\CommentRequest;
1211
use 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
}

app/Http/Controllers/Api/Home/LikesController.php

+15-17
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,19 @@ public function likePost(Post $post)
4141
}
4242

4343
public function getLikesForPost(Post $post)
44-
{
45-
// Get all likes for the post
46-
$likes = Like::where('post_id', $post->id)->get();
47-
48-
// Transform the likes data to include user_name and user_imageUrl
49-
$likeData = $likes->map(function ($like) {
50-
$data = $like->toArray();
51-
$data['user_name'] = $like->user->name; // Change 'name' to the actual column name in your users table
52-
$data['user_imageUrl'] = $like->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table
53-
unset($data['user']); // Remove the user relationship to avoid redundancy
54-
return $data;
55-
});
56-
57-
return $this->data(compact('likeData'));
44+
{
45+
// Get all likes for the post
46+
$likes = Like::where('post_id', $post->id)->get();
47+
48+
// Transform the likes data to include user_name and user_imageUrl
49+
$likeData = $likes->map(function ($like) {
50+
$data = $like->toArray();
51+
$data['user_name'] = $like->user->name;
52+
$data['user_imageUrl'] = $like->user->imageUrl;
53+
unset($data['user']);
54+
return $data;
55+
});
56+
57+
return $this->data(compact('likeData'));
58+
}
5859
}
59-
60-
61-
}

app/Http/Controllers/Api/Home/PostController.php

+33-40
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ class PostController extends Controller
1818

1919
public function getPosts()
2020
{
21-
// Retrieve all posts with the user relationship and order them by 'created_at' in descending order
2221
$posts = Post::with('user')->orderBy('created_at', 'desc')->get();
2322

2423
// Transform the posts data to include user_name and user_imageUrl
2524
$postData = $posts->map(function ($post) {
2625
$data = $post->toArray();
27-
$data['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table
28-
$data['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table
29-
unset($data['user']); // Remove the user relationship to avoid redundancy
26+
$data['user_name'] = $post->user->name;
27+
$data['user_imageUrl'] = $post->user->imageUrl;
28+
unset($data['user']);
3029
return $data;
3130
});
3231

@@ -42,14 +41,12 @@ public function showPost($id)
4241
return $this->errorMessage([], 'Post not found', 404);
4342
}
4443

45-
// Convert the Post model and user model to an array
4644
$postData = $post->toArray();
4745

4846
// 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
47+
$postData['user_name'] = $post->user->name;
48+
$postData['user_imageUrl'] = $post->user->imageUrl;
5149

52-
// Unset the user relationship to avoid redundancy
5350
unset($postData['user']);
5451

5552
return $this->data(['post' => $postData], 'Post retrieved successfully', 200);
@@ -58,7 +55,6 @@ public function showPost($id)
5855

5956
public function createPost(PostRequest $request)
6057
{
61-
// Check if either 'content' or 'file_path' is provided
6258
if (!$request->filled('content') && !$request->hasFile('file_path')) {
6359
return $this->errorMessage([], 'Either content or an image must be provided', 422);
6460
}
@@ -81,71 +77,69 @@ public function createPost(PostRequest $request)
8177

8278
$post->save();
8379

84-
// Fetch the post again to include user_name, user_imageUrl, and other data
8580
$post = Post::with('user')->find($post->id);
8681

8782
$postData = $post->toArray();
8883
// Replace the user_id with user's name and imageUrl
89-
$postData['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table
90-
$postData['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table
91-
unset($postData['user']); // Remove the user relationship to avoid redundancy
84+
$postData['user_name'] = $post->user->name;
85+
$postData['user_imageUrl'] = $post->user->imageUrl;
86+
unset($postData['user']);
9287

9388
return $this->data(['post' => $postData], 'Post created successfully', 201);
9489
}
90+
9591

9692
public function editPost(PostRequest $request, $id)
9793
{
9894
$post = Post::find($id);
95+
9996
if (!$post) {
10097
return $this->errorMessage([], 'Post not found', 404);
10198
}
102-
99+
103100
if ($post->user_id !== Auth::id()) {
104101
return $this->errorMessage([], 'You are not authorized to edit this post', 403);
105102
}
106-
103+
107104
// Check if there are any changes to update
108-
if (!$request->filled('content') && !$request->hasFile('file_path')) {
105+
if (!$request->filled('content') && !$request->hasFile('file_path') && !$request->filled('remove_image')) {
109106
return $this->errorMessage([], 'No changes to update', 422);
110107
}
111-
108+
112109
// Update the post content if provided
113110
if ($request->filled('content')) {
114111
$post->content = $request->input('content');
115112
}
116-
117-
// Handle image upload, if provided
113+
114+
// Handle image upload or removal, if provided
118115
if ($request->hasFile('file_path')) {
119116
$image = $request->file('file_path');
120117
$imagePath = $this->upload($image, 'posts');
121-
$post->image_path = "images/posts/$imagePath";
122-
} elseif (empty($request->input('file_path'))) {
123-
// No new image provided, check if 'file_path' input is empty
124-
// If it's empty, delete the existing image
118+
$post->image_path = "images/posts/$imagePath";
119+
} elseif ($request->filled('remove_image')) {
120+
// Check if the 'remove_image' input is provided and set to true
121+
// If 'remove_image' is true, delete the existing image and set image_path to null
125122
$this->delete($post->image_path);
126-
$post->image_path = null; // Set image_path to null in the database
123+
$post->image_path = null;
127124
}
128-
125+
129126
$post->save();
130-
131-
// Fetch the post again to include user_name, user_imageUrl, and other data
127+
132128
$post = Post::with('user')->find($post->id);
133-
134129
$postData = $post->toArray();
130+
135131
// Replace the user_id with user's name and imageUrl
136-
$postData['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table
137-
$postData['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table
138-
unset($postData['user']); // Remove the user relationship to avoid redundancy
139-
140-
if ($request->filled('content') || $request->hasFile('file_path')) {
132+
$postData['user_name'] = $post->user->name;
133+
$postData['user_imageUrl'] = $post->user->imageUrl;
134+
unset($postData['user']);
135+
136+
if ($request->filled('content') || $request->hasFile('file_path') || $request->filled('remove_image')) {
141137
return $this->data(['post' => $postData], 'Post updated successfully', 200);
142138
} else {
143139
return $this->errorMessage([], 'No changes to update', 422);
144140
}
145141
}
146142

147-
148-
149143

150144
public function deletePost($id)
151145
{
@@ -167,9 +161,9 @@ public function deletePost($id)
167161
return $this->successMessage('Post deleted successfully', 200);
168162
}
169163

164+
170165
public function getUserPosts($userId)
171166
{
172-
// Find the user by their ID
173167
$user = User::find($userId);
174168

175169
if (!$user) {
@@ -182,15 +176,14 @@ public function getUserPosts($userId)
182176
// Transform the posts data to include user_name and user_imageUrl
183177
$postData = $posts->map(function ($post) {
184178
$data = $post->toArray();
185-
$data['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table
186-
$data['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table
187-
unset($data['user']); // Remove the user relationship to avoid redundancy
179+
$data['user_name'] = $post->user->name;
180+
$data['user_imageUrl'] = $post->user->imageUrl;
181+
unset($data['user']);
188182
return $data;
189183
});
190184

191185
return $this->data(compact('postData'));
192186
}
193187

194188

195-
196189
}

0 commit comments

Comments
 (0)