some progress: users slugs, bookmarks page, upvotes page, design
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class GenerateUserSlugs extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'users:generate-slugs';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generate unique slugs for all users without slugs';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->info('Generating slugs for users...');
|
||||
|
||||
$usersWithoutSlugs = User::whereNull('slug')->get();
|
||||
|
||||
if ($usersWithoutSlugs->isEmpty()) {
|
||||
$this->info('All users already have slugs!');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$bar = $this->output->createProgressBar($usersWithoutSlugs->count());
|
||||
$bar->start();
|
||||
|
||||
foreach ($usersWithoutSlugs as $user) {
|
||||
$user->slug = User::generateUniqueSlug($user->name);
|
||||
$user->save();
|
||||
$bar->advance();
|
||||
}
|
||||
|
||||
$bar->finish();
|
||||
$this->newLine();
|
||||
$this->info("Successfully generated slugs for {$usersWithoutSlugs->count()} users!");
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,7 @@ class RegisterController extends Controller
|
||||
'name' => $data['name'],
|
||||
'email' => $data['email'],
|
||||
'password' => Hash::make($data['password']),
|
||||
'slug' => User::generateUniqueSlug($data['name']),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,24 @@ use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class BookmarkController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the user's bookmarked products.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$bookmarks = $user->bookmarks()
|
||||
->with(['product.category', 'product.user'])
|
||||
->whereHas('product', function ($query) {
|
||||
$query->approved();
|
||||
})
|
||||
->latest()
|
||||
->paginate(12);
|
||||
|
||||
return view('bookmarks.index', compact('bookmarks'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle bookmark for a product.
|
||||
*/
|
||||
|
||||
@@ -73,6 +73,11 @@ class ProfileController extends Controller
|
||||
$validated['avatar'] = $avatarName;
|
||||
}
|
||||
|
||||
// Regenerate slug if name changed
|
||||
if ($request->name !== $user->name) {
|
||||
$validated['slug'] = User::generateUniqueSlug($request->name);
|
||||
}
|
||||
|
||||
// Update user profile
|
||||
$user->update($validated);
|
||||
|
||||
|
||||
@@ -10,6 +10,24 @@ use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class UpvoteController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the user's upvoted products.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$upvotes = $user->upvotes()
|
||||
->with(['product.category', 'product.user'])
|
||||
->whereHas('product', function ($query) {
|
||||
$query->approved();
|
||||
})
|
||||
->latest()
|
||||
->paginate(12);
|
||||
|
||||
return view('upvotes.index', compact('upvotes'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle upvote for a product.
|
||||
*/
|
||||
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
@@ -23,6 +24,7 @@ class User extends Authenticatable
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
'slug',
|
||||
'avatar',
|
||||
'bio',
|
||||
'website',
|
||||
@@ -144,4 +146,30 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->hasMany(Bookmark::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique slug for the user based on their name.
|
||||
*/
|
||||
public static function generateUniqueSlug(string $name): string
|
||||
{
|
||||
$slug = Str::slug($name);
|
||||
$originalSlug = $slug;
|
||||
$count = 1;
|
||||
|
||||
// Keep appending numbers until we find a unique slug
|
||||
while (static::where('slug', $slug)->exists()) {
|
||||
$slug = $originalSlug . '-' . $count;
|
||||
$count++;
|
||||
}
|
||||
|
||||
return $slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the route key for the model.
|
||||
*/
|
||||
public function getRouteKeyName(): string
|
||||
{
|
||||
return 'slug';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user