feature(weightage and sorting in explore page):

add like and click weitage in deals explore page
add sorting by like and click
This commit is contained in:
kusowl 2026-01-19 19:01:50 +05:30
parent e5ebe21ed1
commit 6c7f411946
4 changed files with 49 additions and 17 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace App\Enums;
use App\Traits\EnumAsArray;
enum ExplorePageFilters: string
{
use EnumAsArray;
case Like = 'like';
case Click = 'click';
}

View File

@ -2,22 +2,29 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Enums\ExplorePageFilters;
use App\Enums\UserTypes; use App\Enums\UserTypes;
use App\Models\Deal; use App\Models\Deal;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
class ExplorePageController extends Controller class ExplorePageController extends Controller
{ {
public function __invoke() public function __invoke(Request $request)
{ {
$sortBy = $request->validate([
'sortBy' => ['nullable', 'string', Rule::in(ExplorePageFilters::values())],
]);
return view('explore') return view('explore')
->with('profileLink', $this->profileLink()) ->with('profileLink', $this->profileLink())
->with('deals', $this->deals()); ->with('deals', $this->deals(ExplorePageFilters::tryFrom($sortBy['sortBy'] ?? null)));
} }
protected function deals() protected function deals(?ExplorePageFilters $sortBy)
{ {
return Deal::query() $query = Deal::query()
->select([ ->select([
'id', 'title', 'description', 'image', 'active', 'slug', 'link', 'id', 'title', 'description', 'image', 'active', 'slug', 'link',
'deal_category_id', 'user_id', 'deal_category_id', 'user_id',
@ -34,9 +41,21 @@ protected function deals()
->withActiveDeals() ->withActiveDeals()
// Check if the current user interacted with the deal // Check if the current user interacted with the deal
->withCurrentUserInteractions() ->withCurrentUserInteractions()
->withLikes() ->withLikePerDeal()
->withRedirections() ->withRedirectionPerDeal();
->latest()
// 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(); ->paginate();
} }

View File

@ -52,7 +52,7 @@ public function scopeWithCurrentUserInteractions(Builder $query): Builder
]); ]);
} }
public function scopeWithLikes(Builder $query): Builder public function scopeWithLikePerDeal(Builder $query): Builder
{ {
return $query->withCount([ return $query->withCount([
'interactions as total_likes' => function ($query) { 'interactions as total_likes' => function ($query) {
@ -61,7 +61,7 @@ public function scopeWithLikes(Builder $query): Builder
]); ]);
} }
public function scopeWithRedirections(Builder $query): Builder public function scopeWithRedirectionPerDeal(Builder $query): Builder
{ {
return $query->withSum([ return $query->withSum([
'interactions as total_redirection' => function ($query) { 'interactions as total_redirection' => function ($query) {

View File

@ -10,7 +10,7 @@
<div class="flex items-center"> <div class="flex items-center">
<div class="relative group"> <div class="relative group">
<x-ui.button icon="user-circle" class="cursor-pointer" onclick="showMenu(this)"></x-ui.button> <x-ui.button icon="user-circle" class="cursor-pointer" onclick="showMenu(this)"></x-ui.button>
<ul class="menu invisible group-hover:visible w-48 absolute right-0 bg-white border border-gray-300 rounded-md shadow-xl py-2 text-accent-600"> <ul class="menu invisible group-hover:visible transition-all duration-300 ease-in-out w-48 absolute right-0 bg-white border border-gray-300 rounded-md shadow-xl py-2 text-accent-600">
<li class="py-2 px-4 hover:bg-gray-100 hover:text-gray-900 hover:cursor-pointer hover:font-bold"> <li class="py-2 px-4 hover:bg-gray-100 hover:text-gray-900 hover:cursor-pointer hover:font-bold">
<a href="{{$profileLink}}" class="flex space-x-4"> <a href="{{$profileLink}}" class="flex space-x-4">
<div class="p-1 bg-gray-200 rounded-xl text-gray-900"> <div class="p-1 bg-gray-200 rounded-xl text-gray-900">
@ -36,7 +36,7 @@
<form method="post" action="{{route('logout')}}"> <form method="post" action="{{route('logout')}}">
@csrf @csrf
@method('delete') @method('delete')
<x-ui.button class="flex space-x-3 hover:bg-gray-100 hover:border-gray-300 hover:border"> <x-ui.button class="flex space-x-3 hover:bg-red-50 hover:border-red-100 hover:text-red-500 border border-white">
<x-heroicon-o-arrow-right-start-on-rectangle class="w-4 stroke-2 mr-2"/> <x-heroicon-o-arrow-right-start-on-rectangle class="w-4 stroke-2 mr-2"/>
<p class="hidden sm:block">Logout</p> <p class="hidden sm:block">Logout</p>
</x-ui.button> </x-ui.button>
@ -64,22 +64,22 @@
</x-dashboard.card> </x-dashboard.card>
<x-ui.toggle-button-group> <x-ui.toggle-button-group>
<x-ui.toggle-button active> <x-ui.toggle-button :active="request('sortBy') == null">
<a href="" class="flex items-center px-2 py-1 space-x-2"> <a href="{{route('explore')}}" class="flex items-center px-2 py-1 space-x-2">
<x-heroicon-o-clock class="w-4 stroke-2"/> <x-heroicon-o-clock class="w-4 stroke-2"/>
<p class="font-bold text-xs sm:text-sm md:text-md">All Deals</p> <p class="font-bold text-xs sm:text-sm md:text-md">All Deals</p>
</a> </a>
</x-ui.toggle-button> </x-ui.toggle-button>
<x-ui.toggle-button> <x-ui.toggle-button :active="request('sortBy') === \App\Enums\ExplorePageFilters::Like->value" >
<a href="" class="flex items-center px-2 py-1 space-x-2"> <a href="{{route('explore', ['sortBy' => 'like'])}}" class="flex items-center px-2 py-1 space-x-2">
<x-heroicon-o-arrow-trending-up class="w-4 stroke-2"/> <x-heroicon-o-arrow-trending-up class="w-4 stroke-2"/>
<p class="font-bold text-xs sm:text-sm md:text-md">Most Liked</p> <p class="font-bold text-xs sm:text-sm md:text-md">Most Liked</p>
</a> </a>
</x-ui.toggle-button> </x-ui.toggle-button>
<x-ui.toggle-button> <x-ui.toggle-button :active="request('sortBy') === \App\Enums\ExplorePageFilters::Click->value" >
<a href="" class="flex items-center px-2 py-1 space-x-2"> <a href="{{route('explore', ['sortBy'=>'click'])}}" class="flex items-center px-2 py-1 space-x-2">
<x-heroicon-o-star class="w-4 stroke-2"/> <x-heroicon-o-star class="w-4 stroke-2"/>
<p class="font-bold text-xs sm:text-sm md:text-md">Most Clicked</p> <p class="font-bold text-xs sm:text-sm md:text-md">Most Clicked</p>
</a> </a>