Skip to content

Commit b5ccdaa

Browse files
react Like - Unlike and get all likes for a post
1 parent fa5f891 commit b5ccdaa

File tree

3 files changed

+152
-4
lines changed

3 files changed

+152
-4
lines changed

Codelink.postman_collection.json

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@
317317
"bearer": [
318318
{
319319
"key": "token",
320-
"value": "1|DZu0Q0HEtqM18Sn1Xt7GYJPoNsKg50WPNQkUF0vb",
320+
"value": "1|2YArSOq0wRwfYxdSvlemFcX5PlewiRNkXlS8SQGz",
321321
"type": "string"
322322
}
323323
]
@@ -347,7 +347,7 @@
347347
"bearer": [
348348
{
349349
"key": "token",
350-
"value": "1|NpVE3izi4ZTMzabaHzUJHyDMGSm91Jnt3hSiRnw2",
350+
"value": "1|2YArSOq0wRwfYxdSvlemFcX5PlewiRNkXlS8SQGz",
351351
"type": "string"
352352
}
353353
]
@@ -379,7 +379,7 @@
379379
"bearer": [
380380
{
381381
"key": "token",
382-
"value": "1|DZu0Q0HEtqM18Sn1Xt7GYJPoNsKg50WPNQkUF0vb",
382+
"value": "1|2YArSOq0wRwfYxdSvlemFcX5PlewiRNkXlS8SQGz",
383383
"type": "string"
384384
}
385385
]
@@ -409,7 +409,8 @@
409409
{
410410
"key": "file_path",
411411
"type": "file",
412-
"src": []
412+
"src": [],
413+
"disabled": true
413414
}
414415
]
415416
},
@@ -784,6 +785,86 @@
784785
},
785786
"response": []
786787
},
788+
{
789+
"name": "likes",
790+
"request": {
791+
"auth": {
792+
"type": "bearer",
793+
"bearer": [
794+
{
795+
"key": "token",
796+
"value": "1|2YArSOq0wRwfYxdSvlemFcX5PlewiRNkXlS8SQGz",
797+
"type": "string"
798+
}
799+
]
800+
},
801+
"method": "GET",
802+
"header": [],
803+
"url": {
804+
"raw": "http://localhost:8000/api/posts/1/likes",
805+
"protocol": "http",
806+
"host": [
807+
"localhost"
808+
],
809+
"port": "8000",
810+
"path": [
811+
"api",
812+
"posts",
813+
"1",
814+
"likes"
815+
]
816+
}
817+
},
818+
"response": []
819+
},
820+
{
821+
"name": "likes/like",
822+
"request": {
823+
"auth": {
824+
"type": "bearer",
825+
"bearer": [
826+
{
827+
"key": "token",
828+
"value": "1|2YArSOq0wRwfYxdSvlemFcX5PlewiRNkXlS8SQGz",
829+
"type": "string"
830+
}
831+
]
832+
},
833+
"method": "POST",
834+
"header": [
835+
{
836+
"key": "Accept",
837+
"value": "application/json",
838+
"type": "text"
839+
},
840+
{
841+
"warning": "This is a duplicate header and will be overridden by the Authorization header generated by Postman.",
842+
"key": "Authorization",
843+
"value": "application/json",
844+
"type": "text"
845+
}
846+
],
847+
"body": {
848+
"mode": "formdata",
849+
"formdata": []
850+
},
851+
"url": {
852+
"raw": "http://localhost:8000/api/posts/1/like",
853+
"protocol": "http",
854+
"host": [
855+
"localhost"
856+
],
857+
"port": "8000",
858+
"path": [
859+
"api",
860+
"posts",
861+
"1",
862+
"like"
863+
]
864+
}
865+
},
866+
"response": []
867+
},
787868
{
788869
"name": "teams",
789870
"request": {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\Like;
7+
use App\Models\Post;
8+
use Illuminate\Http\Request;
9+
use Illuminate\Support\Facades\Auth;
10+
use App\Traits\ApiTrait;
11+
12+
class LikesController extends Controller
13+
{
14+
use ApiTrait;
15+
16+
public function likePost(Post $post)
17+
{
18+
// Check if the user has already liked this post
19+
$existingLike = Like::where('user_id', Auth::id())->where('post_id', $post->id)->first();
20+
21+
if ($existingLike) {
22+
// Remove the like record
23+
$existingLike->delete();
24+
25+
// Update the likes_count in the posts table
26+
$post->decrement('likes_count');
27+
28+
return $this->successMessage('Post unliked successfully', 200);
29+
}
30+
31+
// Create a new like record for the authenticated user and the post
32+
$like = new Like;
33+
$like->user_id = Auth::id();
34+
$like->post_id = $post->id;
35+
$like->save();
36+
37+
// Update the likes_count in the posts table
38+
$post->increment('likes_count');
39+
40+
return $this->successMessage('Post liked successfully', 200);
41+
}
42+
43+
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'));
58+
}
59+
60+
61+
}

routes/api.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use App\Http\Controllers\Api\NotificationController;
1919
use App\Http\Controllers\Api\DashboardController;
2020
use App\Http\Controllers\Api\CommentController;
21+
use App\Http\Controllers\Api\LikesController;
2122

2223

2324
/*
@@ -112,7 +113,12 @@
112113
Route::post('/edit/{comment}', 'editComment');
113114
Route::get('/delete/{comment}', 'deleteComment');
114115
});
116+
// --------------------------------- Likes Controller ------------------------------------------
115117

118+
Route::group(['prefix' => 'posts/{post}', 'middleware' => ['auth:sanctum'],'controller' => LikesController::class], function () {
119+
Route::get('/likes', 'getLikesForPost');
120+
Route::post('/like', 'likePost');
121+
});
116122
// --------------------------------- Track Controller ------------------------------------------
117123

118124
Route::group(['prefix' => 'tracks', 'middleware' => ['auth:sanctum'], 'controller' => TrackController::class], function () {

0 commit comments

Comments
 (0)