Skip to content

Commit

Permalink
Friendlist controller view and migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Saodus committed Oct 24, 2024
1 parent 2698b2e commit bbc8176
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 0 deletions.
58 changes: 58 additions & 0 deletions app/Http/Controllers/FriendshipsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
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())
->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)
{

}
}
62 changes: 62 additions & 0 deletions app/Models/Friendship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

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'
}
}
29 changes: 29 additions & 0 deletions database/migrations/2024_10_24_063135_create_friendships_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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->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/index.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">Friends List</h1>

@if($friends->isEmpty())
<p class="text-gray-600">You have no friends yet.</p>
@else
<div class="overflow-x-auto">
<table class="min-w-full bg-white border border-gray-200">
<thead>
<tr class="bg-gray-100 border-b">
<th scope="col" class="text-center px-4 py-2 border-r text-sm font-medium text-gray-700">Name</th>
<th scope="col" class="text-center px-4 py-2 text-sm font-medium text-gray-700">Action</th>
</tr>
</thead>
<tbody>
@foreach($friends as $friend)
<tr class="border-b">
<td class="text-center px-4 py-2 border-r">
{{-- Affichage du nom de l'ami --}}
{{ $friend->user_id == Auth::id() ? $friend->friend->name : $friend->user->name }}
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endif
</x-app-layout>
6 changes: 6 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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';

0 comments on commit bbc8176

Please sign in to comment.