diff --git a/app/Http/Controllers/FriendshipsController.php b/app/Http/Controllers/FriendshipsController.php index 3217d2f..3a8d296 100644 --- a/app/Http/Controllers/FriendshipsController.php +++ b/app/Http/Controllers/FriendshipsController.php @@ -37,14 +37,14 @@ public function create() public function pending() { $pendingRequestsFromFriends = Friendship::where('friend_id', Auth::id()) - ->where('status', 'pending') - ->with('user', 'friend') - ->get(); + ->where('status', 'pending') + ->with('user', 'friend') + ->get(); $pendingRequestsFromUser = Friendship::where('user_id', Auth::id()) - ->where('status', 'pending') - ->with(['user', 'friend']) - ->get(); + ->where('status', 'pending') + ->with(['user', 'friend']) + ->get(); $pendingRequests = $pendingRequestsFromFriends->merge($pendingRequestsFromUser); @@ -74,7 +74,7 @@ public function store(Request $request) })->exists(); if ($friendshipExists) { - return back()->with('error', 'You are already friends or a request is pending.'); + return back()->with('error', 'You are already friends, blocked or a request is pending.'); } Friendship::create([ @@ -111,7 +111,54 @@ public function decline(Request $request, $friendship_id) return back()->with('error', 'Could not decline the friend request.'); } - public function removeFriend(Request $request) + public function destroy($friend) + { + $friendship = Friendship::where(function ($query) use ($friend) { + $query->where('user_id', Auth::id()) + ->where('friend_id', $friend); + })->orWhere(function ($query) use ($friend) { + $query->where('user_id', $friend) + ->where('friend_id', Auth::id()); + })->first(); + + if ($friendship) { + $friendship->delete(); + return back()->with('success', 'Friend removed successfully!'); + } + + return back()->with('error', 'Could not find the friend to remove.'); + } + + public function block($friend) { + $friend = User::findOrFail($friend); + + if ($friend->id === Auth::id()) { + return back()->with('error', 'You cannot block yourself.'); + } + + $friendship = Friendship::where(function ($query) use ($friend) { + $query->where('user_id', auth()->id()) + ->where('friend_id', $friend->id); + })->orWhere(function ($query) use ($friend) { + $query->where('user_id', $friend->id) + ->where('friend_id', auth()->id()); + })->first(); + + if ($friendship) { + if ($friendship->status === 'blocked') { + return back()->with('error', 'This user is already blocked.'); + } + $friendship->update(['status' => 'blocked']); + return back()->with('success', 'User blocked successfully!'); + } else { + // Create a new friendship with 'blocked' status if no existing relation is found + Friendship::create([ + 'user_id' => auth()->id(), + 'friend_id' => $friend->id, + 'status' => 'blocked', + ]); + return back()->with('success', 'User blocked successfully!'); + } } } diff --git a/public/source/assets/images/block_icon.png b/public/source/assets/images/block_icon.png new file mode 100644 index 0000000..42d4d2e Binary files /dev/null and b/public/source/assets/images/block_icon.png differ diff --git a/resources/views/friendships/index.blade.php b/resources/views/friendships/index.blade.php index 54e7bf1..17bf48c 100644 --- a/resources/views/friendships/index.blade.php +++ b/resources/views/friendships/index.blade.php @@ -37,10 +37,26 @@
- + + + + +
+ @csrf + @method('DELETE') + +
+ +
+ @csrf + +
@endforeach diff --git a/routes/web.php b/routes/web.php index 90851eb..3529a1f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -30,6 +30,9 @@ Route::get('/friends/pending', [FriendshipsController::class, 'pending'])->name('friends.pending'); Route::post('/friends/{friendship_id}/accept', [FriendshipsController::class, 'accept'])->name('friends.accept'); Route::post('/friends/{friendship_id}/decline', [FriendshipsController::class, 'decline'])->name('friends.decline'); + Route::post('/friends/{friend}/block', [FriendshipsController::class, 'block'])->name('friends.block'); + + });