diff --git a/app/Home/Queries/GroupQuery.php b/app/Home/Queries/GroupQuery.php new file mode 100644 index 00000000..f7dd3a03 --- /dev/null +++ b/app/Home/Queries/GroupQuery.php @@ -0,0 +1,36 @@ + + */ + private Builder $query; + + public function execute(): self + { + $this->query = Membership::select('subactivities.slug', 'subactivities.name') + ->selectRaw('COUNT(member_id) AS count') + ->join('activities', 'memberships.activity_id', 'activities.id') + ->join('subactivities', 'memberships.subactivity_id', 'subactivities.id') + ->isAgeGroup() + ->isMember() + ->groupBy('subactivities.slug', 'subactivities.name') + ->orderBy('subactivity_id'); + + return $this; + } + + /** + * @return array + */ + public function getResult(): array + { + return $this->query->get()->toArray(); + } +} diff --git a/app/Http/Views/HomeView.php b/app/Http/Views/HomeView.php index ef6642f9..f075a4a3 100644 --- a/app/Http/Views/HomeView.php +++ b/app/Http/Views/HomeView.php @@ -2,6 +2,7 @@ namespace App\Http\Views; +use App\Home\Queries\GroupQuery; use App\Member\Member; use App\Payment\Payment; use Illuminate\Http\Request; @@ -21,14 +22,7 @@ class HomeView 'all_users' => Member::count(), 'amount' => number_format($amount->a / 100, 2, ',', '.').' €', ], - 'groups' => Member::select('subactivities.slug', 'subactivities.name')->selectRaw('COUNT(members.id) AS count')->join('memberships', 'memberships.member_id', 'members.id') - ->join('activities', 'memberships.activity_id', 'activities.id') - ->join('subactivities', 'memberships.subactivity_id', 'subactivities.id') - ->where('subactivities.is_age_group', true) - ->where('activities.is_member', true) - ->groupBy('subactivities.name', 'subactivities.slug') - ->orderBy('subactivities.id') - ->get(), + 'groups' => app(GroupQuery::class)->execute()->getResult(), 'ending_tries' => MemberTriesResource::collection(Member::endingTries()->get()), ], ]; diff --git a/tests/Feature/HomeTest.php b/tests/Feature/HomeTest.php new file mode 100644 index 00000000..642d5095 --- /dev/null +++ b/tests/Feature/HomeTest.php @@ -0,0 +1,44 @@ +withoutExceptionHandling(); + Member::factory()->count(3) + ->has(Membership::factory()->in('€ Mitglied', 1, 'Biber', 2)) + ->defaults() + ->create(); + Member::factory()->count(4) + ->has(Membership::factory()->in('€ Mitglied', 1, 'Wölfling', 3)) + ->defaults() + ->create(); + Member::factory()->has(Membership::factory()->in('€ LeiterIn', 2, 'Wölfling', 3)) + ->defaults() + ->create(); + + $this->login()->loginNami(); + + $response = $this->get('/'); + + $this->assertInertiaHas([ + 'slug' => 'biber', + 'name' => 'Biber', + 'count' => 3, + ], $response, 'data.groups.0'); + $this->assertInertiaHas([ + 'slug' => 'woelfling', + 'name' => 'Wölfling', + 'count' => 4, + ], $response, 'data.groups.1'); + } +}