From 360b5e41045f9847b492ab2479b26ea4dedb320e Mon Sep 17 00:00:00 2001 From: Lupu Gheorghe Date: Thu, 7 Nov 2024 22:18:19 +0200 Subject: [PATCH] add general turnouts --- .../Controllers/Api/V1/TurnoutController.php | 16 +++++++++++-- app/Http/Resources/TurnoutResource.php | 24 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 app/Http/Resources/TurnoutResource.php diff --git a/app/Http/Controllers/Api/V1/TurnoutController.php b/app/Http/Controllers/Api/V1/TurnoutController.php index 2944d8b..c089a76 100644 --- a/app/Http/Controllers/Api/V1/TurnoutController.php +++ b/app/Http/Controllers/Api/V1/TurnoutController.php @@ -4,13 +4,25 @@ namespace App\Http\Controllers\Api\V1; +use App\Enums\DataLevel; use App\Http\Controllers\Controller; +use App\Http\Resources\TurnoutResource; use App\Models\Election; +use App\Models\Turnout; +use Illuminate\Http\JsonResponse; class TurnoutController extends Controller { - public function turnout(Election $election) + public function general(Election $election): JsonResponse { - dd($election); + $result = Turnout::query() + ->whereBelongsTo($election) + ->forLevel( + level: DataLevel::TOTAL, + ) + ->toBase() + ->first(); + + return response()->json(TurnoutResource::make($result)); } } diff --git a/app/Http/Resources/TurnoutResource.php b/app/Http/Resources/TurnoutResource.php new file mode 100644 index 0000000..803cfbc --- /dev/null +++ b/app/Http/Resources/TurnoutResource.php @@ -0,0 +1,24 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'initial_total' => $this->initial_total, + 'total' => $this->total, + ]; + } +}