diff --git a/app/Actions/GetBrokerStatsAction.php b/app/Actions/GetBrokerStatsAction.php new file mode 100644 index 0000000..88421c2 --- /dev/null +++ b/app/Actions/GetBrokerStatsAction.php @@ -0,0 +1,22 @@ + + */ + public function execute(User $user): array + { + return [ + 'listings' => $user->deals()->count(), + 'likes' => $user->dealsInteractions()->where('type', InteractionType::Like)->count(), + 'views' => $user->dealsInteractions()->where('type', InteractionType::View)->sum('count'), + 'clicks' => $user->dealsInteractions()->where('type', InteractionType::Redirection)->sum('count'), + ]; + } +} diff --git a/app/Http/Controllers/Broker/BrokerDashboardController.php b/app/Http/Controllers/Broker/BrokerDashboardController.php index 3424b24..546326b 100644 --- a/app/Http/Controllers/Broker/BrokerDashboardController.php +++ b/app/Http/Controllers/Broker/BrokerDashboardController.php @@ -2,15 +2,17 @@ namespace App\Http\Controllers\Broker; +use App\Actions\GetBrokerStatsAction; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; class BrokerDashboardController extends Controller { - public function index() + public function index(GetBrokerStatsAction $getBrokerStatsAction) { return view('dashboards.broker.index') - ->with('deals', $this->deals()); + ->with('deals', $this->deals()) + ->with('stats', $getBrokerStatsAction->execute(Auth::user())); } protected function deals() @@ -28,6 +30,9 @@ protected function deals() 'deal_category_id', ]) ->with('category:id,name') + ->WithLikePerDeal() + ->WithRedirectionPerDeal() + ->withViewPerDeal() ->latest() ->paginate(); } diff --git a/app/Models/Deal.php b/app/Models/Deal.php index ef3c218..5c347f0 100644 --- a/app/Models/Deal.php +++ b/app/Models/Deal.php @@ -86,6 +86,19 @@ public function WithRedirectionPerDeal(Builder $query): Builder ], 'count'); } + /** + * Scope a query to get a view count per deal + */ + #[Scope] + public function withViewPerDeal(Builder $query): Builder + { + return $query->withSum([ + 'interactions as total_views' => function ($query) { + $query->where('type', InteractionType::View); + }, + ], 'count'); + } + /** * Scope a search in a query */ @@ -105,4 +118,5 @@ public function filterByCategory(Builder $query, string $category): Builder { return $query->where('deal_category_id', $category); } + } diff --git a/app/Models/User.php b/app/Models/User.php index 029d879..eebf54c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,7 +4,9 @@ // use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -82,4 +84,9 @@ public function isCustomer(): bool { return $this->type instanceof Customer; } + + public function dealsInteractions(): HasManyThrough + { + return $this->hasManyThrough(Interaction::class, Deal::class); + } } diff --git a/resources/views/components/dashboard/broker/listing-card.blade.php b/resources/views/components/dashboard/broker/listing-card.blade.php index 088c217..c540c35 100644 --- a/resources/views/components/dashboard/broker/listing-card.blade.php +++ b/resources/views/components/dashboard/broker/listing-card.blade.php @@ -1,5 +1,4 @@ @props(['deal']) -
@@ -27,7 +26,7 @@ class="text-xs underline text-accent-601 mt-1">

{{$deal->category->name}}

- +
0, 'likes' => 0, 'clicks' => 0]) +@props(['views' => 0, 'likes' => 0, 'clicks' => 0])
-

{{$impression}}

+

{{$views}}

diff --git a/resources/views/components/dashboard/broker/stats.blade.php b/resources/views/components/dashboard/broker/stats.blade.php index d1439ab..9272c6a 100644 --- a/resources/views/components/dashboard/broker/stats.blade.php +++ b/resources/views/components/dashboard/broker/stats.blade.php @@ -1,22 +1,22 @@ -@props(['list_count' => 0]) +@props(['stats' => []])
- + - + - + - + diff --git a/resources/views/dashboards/broker/index.blade.php b/resources/views/dashboards/broker/index.blade.php index 435ccc1..a28662b 100644 --- a/resources/views/dashboards/broker/index.blade.php +++ b/resources/views/dashboards/broker/index.blade.php @@ -2,6 +2,6 @@ - +