Skip to content

Commit 03c6545

Browse files
committed
user update
2 parents cbafdac + 1c63762 commit 03c6545

13 files changed

+2161
-1640
lines changed

Codelink.postman_collection.json

+1,699-1,515
Large diffs are not rendered by default.

app/Http/Controllers/Api/CommentController.php

-83
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Home;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
use App\Models\Comment;
8+
use App\Models\Post;
9+
use Illuminate\Support\Facades\Auth;
10+
use App\Http\Requests\CommentRequest;
11+
use App\Traits\ApiTrait;
12+
13+
class CommentController extends Controller
14+
{
15+
use ApiTrait;
16+
17+
public function showAllComments(Post $post)
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+
}
40+
41+
public function createComment(CommentRequest $request, Post $post)
42+
{
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+
}
74+
}
75+
76+
public function editComment(CommentRequest $request, Post $post, Comment $comment)
77+
{
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);
107+
}
108+
}
109+
110+
public function deleteComment(Post $post, Comment $comment)
111+
{
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+
}
117+
118+
// Decrement the comments_count for the post
119+
$post->decrement('comments_count');
120+
121+
// Delete the comment
122+
$comment->delete();
123+
124+
return $this->data(['post_id' => $post->id],'Comment deleted successfully', 200);
125+
} catch (\Exception $e) {
126+
return $this->errorMessage([], 'An error occurred while deleting the comment', 500);
127+
}
128+
}
129+
}

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

+18-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Http\Controllers\Api;
3+
namespace App\Http\Controllers\Api\Home;
44

55
use App\Http\Controllers\Controller;
66
use App\Models\Like;
@@ -25,7 +25,7 @@ public function likePost(Post $post)
2525
// Update the likes_count in the posts table
2626
$post->decrement('likes_count');
2727

28-
return $this->successMessage('Post unliked successfully', 200);
28+
return $this->data(['post_id' => $post->id],'Post unliked successfully', 200);
2929
}
3030

3131
// Create a new like record for the authenticated user and the post
@@ -37,25 +37,23 @@ public function likePost(Post $post)
3737
// Update the likes_count in the posts table
3838
$post->increment('likes_count');
3939

40-
return $this->successMessage('Post liked successfully', 200);
40+
return $this->data(['post_id' => $post->id],'Post liked successfully', 200);
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-
}

0 commit comments

Comments
 (0)