Skip to content

Commit

Permalink
Adding a friend
Browse files Browse the repository at this point in the history
  • Loading branch information
Saodus committed Oct 25, 2024
1 parent d31c520 commit 3485f9e
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 35 deletions.
86 changes: 73 additions & 13 deletions app/Http/Controllers/FriendshipsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
{
}
}
19 changes: 0 additions & 19 deletions app/Models/Friendship.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('friendships', function (Blueprint $table) {
$table->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');
}
};
28 changes: 28 additions & 0 deletions resources/views/friendships/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<x-app-layout>
<h1 class="text-2xl font-bold mb-4">Add a Friend</h1>

@if(session('success'))
<div class="bg-green-500 text-white p-4 rounded mb-4">
{{ session('success') }}
</div>
@elseif(session('error'))
<div class="bg-red-500 text-white p-4 rounded mb-4">
{{ session('error') }}
</div>
@endif

<form action="{{ route('friends.store') }}" method="POST" class="space-y-4">
@csrf
<div>
<label for="name" class="block text-sm font-medium text-gray-700">Name</label>
<input type="text" name="name" id="name" required
class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500">
</div>

<div>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
Add Friend
</button>
</div>
</form>
</x-app-layout>
47 changes: 46 additions & 1 deletion resources/views/friendships/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,52 @@
<x-app-layout>
<h1 class="text-2xl font-bold mb-4">Friends List</h1>
<div>
<x-nav-link :href="route('friends.create')" :active="request()->routeIs('friends.create')">
Add a friend
</x-nav-link>
</div>
<div></div>

@if($friends->isEmpty())
@if(session('error'))
<div class="bg-red-500 text-white p-4 rounded mb-4">
{{ session('error') }}
</div>
@endif

@if(session('success'))
<div class="bg-green-500 text-white p-4 rounded mb-4">
{{ session('success') }}
</div>
@endif

@if(!$pendingRequests->isEmpty())
@foreach($pendingRequests as $request)
@if($request->user_id != Auth::id())
<tr class="border-b">
<td class="text-center px-4 py-2 border-r">
{{ $request->user->name }}
</td>
<td class="text-center px-4 py-2">
<form action="{{ route('friends.accept', $request->id) }}" method="POST" class="inline">
@csrf
<button type="submit" class="bg-green-500 text-white px-3 py-1 rounded hover:bg-green-600">
Accept
</button>
</form>
<form action="{{ route('friends.decline', $request->id) }}" method="POST" class="inline">
@csrf
<button type="submit" class="bg-red-500 text-white px-3 py-1 rounded hover:bg-red-600">
Decline
</button>
</form>
</td>
</tr>
@endif
@endforeach
@endif


@if($friends->isEmpty())
<p class="text-gray-600">You have no friends yet.</p>
@else
<div class="overflow-x-auto">
Expand Down
1 change: 0 additions & 1 deletion resources/views/layouts/guest.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<body class="font-sans text-gray-900 antialiased">
<div class="min-h-screen flex flex-col sm:justify-center items-center pt-6 sm:pt-0 background-app">

<!-- Ajustement pour centrer le logo -->
<a href="/" class="flex justify-center">
<x-application-logo class="w-20 h-20 fill-current text-gray-500" />
</a>
Expand Down
6 changes: 5 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');



});

Expand Down

0 comments on commit 3485f9e

Please sign in to comment.