From bbc81763d71024e4ef74424865b0242913ce9a20 Mon Sep 17 00:00:00 2001 From: Jaton David Date: Thu, 24 Oct 2024 18:36:36 +0200 Subject: [PATCH] Friendlist controller view and migration --- .../Controllers/FriendshipsController.php | 58 +++++++++++++++++ app/Models/Friendship.php | 62 +++++++++++++++++++ ..._10_24_063135_create_friendships_table.php | 29 +++++++++ resources/views/friendships/index.blade.php | 28 +++++++++ routes/web.php | 6 ++ 5 files changed, 183 insertions(+) create mode 100644 app/Http/Controllers/FriendshipsController.php create mode 100644 app/Models/Friendship.php create mode 100644 database/migrations/2024_10_24_063135_create_friendships_table.php create mode 100644 resources/views/friendships/index.blade.php diff --git a/app/Http/Controllers/FriendshipsController.php b/app/Http/Controllers/FriendshipsController.php new file mode 100644 index 0000000..ca4d154 --- /dev/null +++ b/app/Http/Controllers/FriendshipsController.php @@ -0,0 +1,58 @@ +where('status', 'accepted') + ->with(['user', 'friend']) + ->get(); + + $friendsFromFriends = Friendship::where('friend_id', Auth::id()) + ->where('status', 'accepted') + ->with(['user', 'friend']) + ->get(); + + $friends = $friendsFromFriends->merge($friendsFromUser); + return view('friendships.index', ['friends' => $friends]); + } + + + + + //show one friend profile + function friends() + { + + } + + function addFriend(Request $request) + { + + } + + function removeFriend(Request $request) + { + + } + + function declineFriend(Request $request) + { + + } + + function acceptFriend(Request $request) + { + + } +} diff --git a/app/Models/Friendship.php b/app/Models/Friendship.php new file mode 100644 index 0000000..a4a48e3 --- /dev/null +++ b/app/Models/Friendship.php @@ -0,0 +1,62 @@ +belongsTo(User::class, 'user_id'); // Relation avec le modèle User + } + + /** + * Obtenir l'ami associé à cette amitié. + */ + public function friend(): BelongsTo + { + return $this->belongsTo(User::class, 'friend_id'); // Relation avec le modèle User + } + + /** + * Vérifier si l'amitié est acceptée. + */ + public function isAccepted(): bool + { + return $this->status === 'accepted'; // Retourne true si le statut est 'accepted' + } + + /** + * Vérifier si l'amitié est en attente. + */ + public function isPending(): bool + { + return $this->status === 'pending'; // Retourne true si le statut est 'pending' + } + + /** + * Vérifier si l'amitié est bloquée. + */ + public function isBlocked(): bool + { + return $this->status === 'blocked'; // Retourne true si le statut est 'blocked' + } +} diff --git a/database/migrations/2024_10_24_063135_create_friendships_table.php b/database/migrations/2024_10_24_063135_create_friendships_table.php new file mode 100644 index 0000000..75b7075 --- /dev/null +++ b/database/migrations/2024_10_24_063135_create_friendships_table.php @@ -0,0 +1,29 @@ +foreignId('user_id')->constrained('users')->onDelete('cascade'); + $table->foreignId('friend_id')->constrained('users')->onDelete('cascade'); + $table->timestamps(); + $table->enum('status', ['pending', 'accepted', 'blocked'])->default('pending'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('friendships'); + } +}; diff --git a/resources/views/friendships/index.blade.php b/resources/views/friendships/index.blade.php new file mode 100644 index 0000000..b9cec08 --- /dev/null +++ b/resources/views/friendships/index.blade.php @@ -0,0 +1,28 @@ + +

Friends List

+ + @if($friends->isEmpty()) +

You have no friends yet.

+ @else +
+ + + + + + + + + @foreach($friends as $friend) + + + + @endforeach + +
NameAction
+ {{-- Affichage du nom de l'ami --}} + {{ $friend->user_id == Auth::id() ? $friend->friend->name : $friend->user->name }} +
+
+ @endif +
diff --git a/routes/web.php b/routes/web.php index b5f794e..6d74893 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,6 +3,8 @@ use App\Http\Controllers\ProfileController; use Illuminate\Support\Facades\Route; +use App\Http\Controllers\FriendshipsController; + Route::get('/', function () { if (Auth::check()) { return view('dashboard'); @@ -16,10 +18,14 @@ return view('dashboard'); })->middleware(['auth', 'verified'])->name('dashboard'); +Route::get('/dashboard/friends', [FriendshipsController::class, 'index'])->name('friends.index'); + + Route::middleware('auth')->group(function () { Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit'); Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update'); Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy'); }); + require __DIR__.'/auth.php';