diff --git a/app/Http/Controllers/FriendshipsController.php b/app/Http/Controllers/FriendshipsController.php index ca4d154..a4cb821 100644 --- a/app/Http/Controllers/FriendshipsController.php +++ b/app/Http/Controllers/FriendshipsController.php @@ -4,13 +4,12 @@ use App\Http\Controllers\Controller; use Illuminate\Http\Request; -use \App\Models\User; -use \App\Models\Friendship; +use App\Models\User; +use App\Models\Friendship; use Illuminate\Support\Facades\Auth; class FriendshipsController extends Controller { - //Show every user friends public function index() { $friendsFromUser = Friendship::where('user_id', Auth::id()) @@ -23,36 +22,97 @@ public function index() ->with(['user', 'friend']) ->get(); - $friends = $friendsFromFriends->merge($friendsFromUser); - return view('friendships.index', ['friends' => $friends]); - } + $pendingRequestsFromFriends = Friendship::where('friend_id', Auth::id()) + ->where('status', 'pending') + ->with('user', 'friend') + ->get(); + $pendingRequestsFromUser = Friendship::where('user_id', Auth::id()) + ->where('status', 'pending') + ->with(['user', 'friend']) + ->get(); + $friends = $friendsFromFriends->merge($friendsFromUser); + $pendingRequests = $pendingRequestsFromFriends->merge($pendingRequestsFromUser); + return view('friendships.index', [ + 'friends' => $friends, + 'pendingRequests' => $pendingRequests, + ]); + } - //show one friend profile - function friends() + public function create() { + return view('friendships.create'); + } + public function store(Request $request) + { + $validated = $request->validate([ + 'name' => 'required|string|exists:users,name', + ]); + + $friend = User::where('name', $validated['name'])->first(); + + if ($friend->id === Auth::id()) { + return redirect()->route('friends.index')->with('error', 'You cannot add yourself.'); + } + + $friendshipExists = 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()); + })->exists(); + + if ($friendshipExists) { + return redirect()->route('friends.index')->with('error', 'You are already friends or a request is pending.'); + } + + Friendship::create([ + 'user_id' => auth()->id(), + 'friend_id' => $friend->id, + 'status' => 'pending', + ]); + + return redirect()->route('friends.index')->with('success', 'Friend request sent!'); } - function addFriend(Request $request) + public function accept(Request $request, $friendship_id) { + $friendship = Friendship::find($friendship_id); + if ($friendship && $friendship->status === 'pending') { + // Met à jour le statut directement + $friendship->update(['status' => 'accepted']); + return redirect()->route('friends.index')->with('success', 'Friend request accepted!'); + } + + return redirect()->route('friends.index')->with('error', 'Could not accept the friend request.'); } - function removeFriend(Request $request) + public function decline(Request $request, $friendship_id) { + $friendship = Friendship::find($friendship_id); + + if ($friendship) { + $friendship->delete(); + return redirect()->route('friends.index')->with('success', 'Friend request declined!'); + } + return redirect()->route('friends.index')->with('error', 'Could not decline the friend request.'); } - function declineFriend(Request $request) + public function removeFriend(Request $request) { - } - function acceptFriend(Request $request) + public function declineFriend(Request $request) { + } + public function acceptFriend(Request $request) + { } } diff --git a/app/Models/Friendship.php b/app/Models/Friendship.php index a4a48e3..5cb9024 100644 --- a/app/Models/Friendship.php +++ b/app/Models/Friendship.php @@ -10,51 +10,32 @@ class Friendship extends Model { use HasFactory; - // Spécifie le nom de la table associée à ce modèle protected $table = 'friendships'; - - // Les attributs qui peuvent être assignés en masse protected $fillable = [ 'user_id', 'friend_id', 'status', ]; - /** - * Obtenir l'utilisateur qui possède cette amitié. - */ public function user(): BelongsTo { return $this->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_25_132943_create__friendships_table.php b/database/migrations/2024_10_25_132943_create__friendships_table.php new file mode 100644 index 0000000..0f4ed1c --- /dev/null +++ b/database/migrations/2024_10_25_132943_create__friendships_table.php @@ -0,0 +1,30 @@ +id(); + $table->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/create.blade.php b/resources/views/friendships/create.blade.php new file mode 100644 index 0000000..b456ebd --- /dev/null +++ b/resources/views/friendships/create.blade.php @@ -0,0 +1,28 @@ + +

Add a Friend

+ + @if(session('success')) +
+ {{ session('success') }} +
+ @elseif(session('error')) +
+ {{ session('error') }} +
+ @endif + +
+ @csrf +
+ + +
+ +
+ +
+
+
diff --git a/resources/views/friendships/index.blade.php b/resources/views/friendships/index.blade.php index b9cec08..32a94e8 100644 --- a/resources/views/friendships/index.blade.php +++ b/resources/views/friendships/index.blade.php @@ -1,7 +1,52 @@

Friends List

+
+ + Add a friend + +
+
- @if($friends->isEmpty()) + @if(session('error')) +
+ {{ session('error') }} +
+ @endif + + @if(session('success')) +
+ {{ session('success') }} +
+ @endif + + @if(!$pendingRequests->isEmpty()) + @foreach($pendingRequests as $request) + @if($request->user_id != Auth::id()) + + + {{ $request->user->name }} + + +
+ @csrf + +
+
+ @csrf + +
+ + + @endif + @endforeach + @endif + + +@if($friends->isEmpty())

You have no friends yet.

@else
diff --git a/resources/views/layouts/guest.blade.php b/resources/views/layouts/guest.blade.php index f101ddb..d8ca2b5 100644 --- a/resources/views/layouts/guest.blade.php +++ b/resources/views/layouts/guest.blade.php @@ -17,7 +17,6 @@
- diff --git a/routes/web.php b/routes/web.php index 3011597..ef6436a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -24,7 +24,11 @@ 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'); - Route::get('/friends', [FriendshipsController::class, 'index'])->name('friends.index'); + Route::resource('friends', FriendshipsController::class); + Route::post('/friends/{friendship_id}/accept', [FriendshipsController::class, 'accept'])->name('friends.accept'); + Route::post('/friends/{friendship_id}/decline', [FriendshipsController::class, 'decline'])->name('friends.decline'); + + });