76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\ExplorePageFilters;
|
|
use App\Enums\UserTypes;
|
|
use App\Models\Deal;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class ExplorePageController extends Controller
|
|
{
|
|
public function __invoke(Request $request)
|
|
{
|
|
$sortBy = $request->validate([
|
|
'sortBy' => ['nullable', 'string', Rule::in(ExplorePageFilters::values())],
|
|
]);
|
|
|
|
return view('explore')
|
|
->with('profileLink', $this->profileLink())
|
|
->with('deals', $this->deals(ExplorePageFilters::tryFrom($sortBy['sortBy'] ?? null)));
|
|
}
|
|
|
|
protected function deals(?ExplorePageFilters $sortBy)
|
|
{
|
|
$query = 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()
|
|
->withLikePerDeal()
|
|
->withRedirectionPerDeal();
|
|
|
|
// Add filters
|
|
if ($sortBy === ExplorePageFilters::Like) {
|
|
$query->orderBy('total_likes', 'desc');
|
|
} elseif ($sortBy === ExplorePageFilters::Click) {
|
|
$query->orderBy('total_redirection', 'desc');
|
|
} else {
|
|
$query->orderByRaw(
|
|
'(COALESCE(total_likes, 0) / 70.0) + (COALESCE(total_redirection, 0) / 30.0) DESC'
|
|
);
|
|
}
|
|
|
|
return $query->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);
|
|
}
|
|
}
|
|
}
|