- refactor controller to handle favorite - show like count and update when liked a deal - maintain like and favorite state with server
56 lines
1.5 KiB
PHP
56 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\UserTypes;
|
|
use App\Models\Deal;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class ExplorePageController extends Controller
|
|
{
|
|
public function __invoke()
|
|
{
|
|
return view('explore')
|
|
->with('profileLink', $this->profileLink())
|
|
->with('deals', $this->deals());
|
|
}
|
|
|
|
protected function deals()
|
|
{
|
|
return Deal::query()
|
|
->select([
|
|
'id', 'title', 'description', 'image', 'active', 'slug', 'link',
|
|
'deal_category_id', 'user_id',
|
|
])
|
|
// Select additional details
|
|
->with([
|
|
'category:id,name',
|
|
'broker' => function ($query) {
|
|
$query->select('id', 'name', 'email', 'role_type', 'role_id')
|
|
->with('type');
|
|
},
|
|
])
|
|
// Select only admin-approved deals
|
|
->withActiveDeals()
|
|
// Check if the current user interacted with the deal
|
|
->withCurrentUserInteractions()
|
|
->withLikes()
|
|
->latest()
|
|
->paginate();
|
|
}
|
|
|
|
/**
|
|
* Determines the link to the user's profile dashboard
|
|
* based on the user's role.
|
|
*
|
|
* @return string The URL for the user's dashboard.
|
|
*/
|
|
protected function profileLink()
|
|
{
|
|
$user = Auth::user();
|
|
if ($user->role === UserTypes::Broker->value) {
|
|
return route('broker.profile.show', $user);
|
|
}
|
|
}
|
|
}
|