Skip to content

Commit

Permalink
Merge branch 'Crud-Friendlist' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Saodus committed Nov 26, 2024
2 parents 178e37a + 2afbf5c commit e5d47ac
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
63 changes: 55 additions & 8 deletions app/Http/Controllers/FriendshipsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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!');
}
}
}
Binary file added public/source/assets/images/block_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 20 additions & 4 deletions resources/views/friendships/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,26 @@
</div>

<div class="flex space-x-2">
<button>
<img src="{{ asset('source/assets/images/delete_friend_icon.png') }}" alt="Supprimer"
class="h-10 w-10">
</button>
<!-- Boutons d'action en icônes -->

<button>
<img src="{{ asset('source/assets/images/message_friend_icon.png') }}" alt="Message" class="h-10 w-10">
</button>

<form action="{{ route('friends.destroy', ['friend' => $friend->user_id == Auth::id() ? $friend->friend->id : $friend->user->id]) }}" method="POST" onsubmit="return confirm('Are you sure you want to remove this friend?');">
@csrf
@method('DELETE')
<button type="submit">
<img src="{{ asset('source/assets/images/delete_friend_icon.png') }}" alt="Supprimer" class="h-10 w-10">
</button>
</form>

<form action="{{ route('friends.block', ['friend' => $friend->user_id == Auth::id() ? $friend->friend->id : $friend->user->id]) }}" method="POST" onsubmit="return confirm('Are you sure you want to block this user?');">
@csrf
<button type="submit">
<img src="{{ asset('source/assets/images/block_icon.png') }}" alt="Block" class="h-10 w-10">
</button>
</form>
</div>
</div>
@endforeach
Expand Down
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');


});


Expand Down

0 comments on commit e5d47ac

Please sign in to comment.