File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Http \Controllers ;
4+
5+ use Illuminate \Http \Request ;
6+ use App \Models \Comment ;
7+ use App \Models \Post ;
8+ use Illuminate \Support \Facades \Auth ;
9+
10+ class CommentController extends Controller
11+ {
12+ // Ajouter un commentaire
13+ public function store (Request $ request , Post $ post )
14+ {
15+ $ request ->validate ([
16+ 'content ' => 'required|string|max:1000 ' ,
17+ ]);
18+
19+ $ comment = Comment::create ([
20+ 'content ' => $ request ->content ,
21+ 'user_id ' => Auth::id (),
22+ 'post_id ' => $ post ->id ,
23+ ]);
24+
25+ // Charger l'auteur du commentaire pour le renvoyer au frontend
26+ $ comment ->load ('user ' );
27+
28+ return response ()->json ($ comment , 201 );
29+ }
30+
31+ // Récupérer les commentaires d’un post
32+ public function index (Post $ post )
33+ {
34+ $ comments = $ post ->comments ()->with ('user ' )->orderBy ('created_at ' , 'asc ' )->get ();
35+ return response ()->json ($ comments );
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Http \Controllers ;
4+
5+ use Illuminate \Http \Request ;
6+ use App \Models \Like ;
7+ use App \Models \Post ;
8+ use Illuminate \Support \Facades \Auth ;
9+
10+ class LikeController extends Controller
11+ {
12+ // Like ou Unlike un post
13+ public function toggle (Post $ post )
14+ {
15+ $ user = Auth::user ();
16+
17+ $ like = Like::where ('user_id ' , $ user ->id )->where ('post_id ' , $ post ->id )->first ();
18+
19+ if ($ like ) {
20+ $ like ->delete ();
21+ return response ()->json (['liked ' => false , 'message ' => 'Like removed ' ]);
22+ } else {
23+ Like::create ([
24+ 'user_id ' => $ user ->id ,
25+ 'post_id ' => $ post ->id ,
26+ ]);
27+ return response ()->json (['liked ' => true , 'message ' => 'Post liked ' ]);
28+ }
29+ }
30+
31+ // Nombre de likes + si l'utilisateur a liké
32+ public function getLikes (Post $ post )
33+ {
34+ $ user = Auth::user ();
35+
36+ return response ()->json ([
37+ 'likes_count ' => $ post ->likes ()->count (),
38+ 'liked ' => $ post ->likes ()->where ('user_id ' , $ user ->id )->exists (),
39+ ]);
40+ }
41+ }
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ public static function middleware()
1919
2020 public function index ()
2121 {
22- return Post::with ('user ' )->latest ()->get ();
22+ return Post::with ([ 'user ' , ' comments.user ' , ' likes ' ] )->latest ()->get ();
2323 }
2424
2525 public function store (Request $ request )
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Models ;
4+
5+ use Illuminate \Database \Eloquent \Model ;
6+ use Illuminate \Database \Eloquent \Factories \HasFactory ;
7+
8+ class Comment extends Model
9+ {
10+ use HasFactory;
11+
12+ protected $ fillable = [
13+ 'content ' ,
14+ 'user_id ' ,
15+ 'post_id ' ,
16+ ];
17+
18+ // Relation vers l'utilisateur qui a commenté
19+ public function user ()
20+ {
21+ return $ this ->belongsTo (User::class);
22+ }
23+
24+ // Relation vers le post commenté
25+ public function post ()
26+ {
27+ return $ this ->belongsTo (Post::class);
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Models ;
4+
5+ use Illuminate \Database \Eloquent \Model ;
6+ use Illuminate \Database \Eloquent \Factories \HasFactory ;
7+
8+ class Like extends Model
9+ {
10+ use HasFactory;
11+
12+ protected $ fillable = [
13+ 'user_id ' ,
14+ 'post_id ' ,
15+ ];
16+
17+ // Relation vers l'utilisateur qui a liké
18+ public function user ()
19+ {
20+ return $ this ->belongsTo (User::class);
21+ }
22+
23+ // Relation vers le post liké
24+ public function post ()
25+ {
26+ return $ this ->belongsTo (Post::class);
27+ }
28+ }
Original file line number Diff line number Diff line change @@ -18,4 +18,11 @@ class Post extends Model
1818 public function user (){
1919 return $ this ->belongsTo (User::class);
2020 }
21+
22+ public function likes () {
23+ return $ this ->hasMany (Like::class);
24+ }
25+ public function comments () {
26+ return $ this ->hasMany (Comment::class);
27+ }
2128}
Original file line number Diff line number Diff line change @@ -33,4 +33,11 @@ protected function casts(): array
3333 'password ' => 'hashed ' ,
3434 ];
3535 }
36+
37+ public function likes () {
38+ return $ this ->hasMany (Like::class);
39+ }
40+ public function comments () {
41+ return $ this ->hasMany (Comment::class);
42+ }
3643}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ return [
4+
5+ 'paths ' => ['api/* ' , 'sanctum/csrf-cookie ' ],
6+
7+ 'allowed_methods ' => ['* ' ],
8+
9+ // 'allowed_origins' => [
10+ // 'https://vite-project-a3be7.web.app',
11+ // 'https://e3a44e1be803.ngrok-free.app',
12+ // ],
13+
14+ 'allowed_origins_patterns ' => [],
15+
16+ 'allowed_headers ' => ['* ' ],
17+
18+ 'exposed_headers ' => [],
19+
20+ 'max_age ' => 0 ,
21+
22+ 'supports_credentials ' => false ,
23+
24+ ];
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ use Illuminate \Database \Migrations \Migration ;
4+ use Illuminate \Database \Schema \Blueprint ;
5+ use Illuminate \Support \Facades \Schema ;
6+
7+ return new class extends Migration
8+ {
9+ /**
10+ * Run the migrations.
11+ */
12+ public function up (): void
13+ {
14+ Schema::create ('likes ' , function (Blueprint $ table ) {
15+ $ table ->id ();
16+ $ table ->foreignId ('user_id ' )->constrained ()->onDelete ('cascade ' );
17+ $ table ->foreignId ('post_id ' )->constrained ()->onDelete ('cascade ' );
18+ $ table ->timestamps ();
19+ $ table ->unique (['user_id ' , 'post_id ' ]); // Un utilisateur ne peut liker un post qu'une seule fois
20+ });
21+ }
22+
23+ /**
24+ * Reverse the migrations.
25+ */
26+ public function down (): void
27+ {
28+ Schema::dropIfExists ('likes ' );
29+ }
30+ };
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ use Illuminate \Database \Migrations \Migration ;
4+ use Illuminate \Database \Schema \Blueprint ;
5+ use Illuminate \Support \Facades \Schema ;
6+
7+ return new class extends Migration
8+ {
9+ /**
10+ * Run the migrations.
11+ */
12+ public function up (): void
13+ {
14+ Schema::create ('comments ' , function (Blueprint $ table ) {
15+ $ table ->id ();
16+ $ table ->text ('content ' );
17+ $ table ->foreignId ('user_id ' )->constrained ()->onDelete ('cascade ' );
18+ $ table ->foreignId ('post_id ' )->constrained ()->onDelete ('cascade ' );
19+ $ table ->timestamps ();
20+ });
21+ }
22+
23+ /**
24+ * Reverse the migrations.
25+ */
26+ public function down (): void
27+ {
28+ Schema::dropIfExists ('comments ' );
29+ }
30+ };
You can’t perform that action at this time.
0 commit comments