diff --git a/app/Http/Controllers/Api/V1/NomenclatureController.php b/app/Http/Controllers/Api/V1/NomenclatureController.php index 57bb523..c641003 100644 --- a/app/Http/Controllers/Api/V1/NomenclatureController.php +++ b/app/Http/Controllers/Api/V1/NomenclatureController.php @@ -5,8 +5,8 @@ namespace App\Http\Controllers\Api\V1; use App\Http\Controllers\Controller; -use App\Http\Resources\CountryResource; -use App\Http\Resources\CountyResource; +use App\Http\Resources\Nomenclature\CountryResource; +use App\Http\Resources\Nomenclature\CountyResource; use App\Http\Resources\Nomenclature\ElectionResource; use App\Models\Country; use App\Models\County; diff --git a/app/Http/Controllers/Api/V1/TurnoutController.php b/app/Http/Controllers/Api/V1/TurnoutController.php index c089a76..663db4e 100644 --- a/app/Http/Controllers/Api/V1/TurnoutController.php +++ b/app/Http/Controllers/Api/V1/TurnoutController.php @@ -6,14 +6,20 @@ use App\Enums\DataLevel; use App\Http\Controllers\Controller; -use App\Http\Resources\TurnoutResource; +use App\Http\Resources\Turnout\Diaspora\TurnoutDiasporaAggregatedResource; +use App\Http\Resources\Turnout\Diaspora\TurnoutDiasporaResource; +use App\Http\Resources\Turnout\National\TurnoutNationalAggregatedResource; +use App\Http\Resources\Turnout\National\TurnoutNationalResource; +use App\Http\Resources\Turnout\TurnoutResource; +use App\Models\Country; +use App\Models\County; use App\Models\Election; use App\Models\Turnout; use Illuminate\Http\JsonResponse; class TurnoutController extends Controller { - public function general(Election $election): JsonResponse + public function total(Election $election): JsonResponse { $result = Turnout::query() ->whereBelongsTo($election) @@ -25,4 +31,81 @@ public function general(Election $election): JsonResponse return response()->json(TurnoutResource::make($result)); } + + public function diaspora(Election $election): JsonResponse + { + $general = Turnout::query() + ->whereBelongsTo($election) + ->forLevel( + level: DataLevel::DIASPORA, + aggregate: true, + ) + ->toBase() + ->first(); + + $general->uats = Turnout::query() + ->whereBelongsTo($election) + ->forLevel( + level: DataLevel::DIASPORA, + ) + ->toBase() + ->get() + ->toArray(); + + return response()->json(TurnoutDiasporaAggregatedResource::make($general)); + } + + public function country(Election $election, Country $country): JsonResponse + { + return response()->json( + TurnoutDiasporaResource::make( + Turnout::query() + ->whereBelongsTo($election) + ->forLevel( + level: DataLevel::DIASPORA, + country: $country->id, + ) + ->toBase() + ->first() + ) + ); + } + + public function national(Election $election): JsonResponse + { + $result = Turnout::query() + ->whereBelongsTo($election) + ->forLevel( + level: DataLevel::NATIONAL, + aggregate: true, + ) + ->toBase() + ->first(); + + $result->uats = Turnout::query()->whereBelongsTo($election) + ->forLevel( + level: DataLevel::NATIONAL, + ) + ->toBase() + ->get() + ->toArray(); + + return response()->json(TurnoutNationalAggregatedResource::make($result)); + } + + public function county(Election $election, County $county): JsonResponse + { + return response()->json( + TurnoutNationalResource::make( + Turnout::query() + ->whereBelongsTo($election) + ->forLevel( + level: DataLevel::NATIONAL, + county: $county->id, + ) + ->toBase() + ->first() + ) + ); + } } diff --git a/app/Http/Resources/CountryResource.php b/app/Http/Resources/Nomenclature/CountryResource.php similarity index 90% rename from app/Http/Resources/CountryResource.php rename to app/Http/Resources/Nomenclature/CountryResource.php index bc85da6..552e2a1 100644 --- a/app/Http/Resources/CountryResource.php +++ b/app/Http/Resources/Nomenclature/CountryResource.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace App\Http\Resources; +namespace App\Http\Resources\Nomenclature; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; diff --git a/app/Http/Resources/CountyResource.php b/app/Http/Resources/Nomenclature/CountyResource.php similarity index 93% rename from app/Http/Resources/CountyResource.php rename to app/Http/Resources/Nomenclature/CountyResource.php index 6dbb508..7a69ab5 100644 --- a/app/Http/Resources/CountyResource.php +++ b/app/Http/Resources/Nomenclature/CountyResource.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace App\Http\Resources; +namespace App\Http\Resources\Nomenclature; use App\Models\County; use Illuminate\Http\Request; diff --git a/app/Http/Resources/LocationResource.php b/app/Http/Resources/Nomenclature/LocationResource.php similarity index 90% rename from app/Http/Resources/LocationResource.php rename to app/Http/Resources/Nomenclature/LocationResource.php index a7d8637..ede1b34 100644 --- a/app/Http/Resources/LocationResource.php +++ b/app/Http/Resources/Nomenclature/LocationResource.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace App\Http\Resources; +namespace App\Http\Resources\Nomenclature; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; diff --git a/app/Http/Resources/Turnout/Diaspora/TurnoutDiasporaAggregatedResource.php b/app/Http/Resources/Turnout/Diaspora/TurnoutDiasporaAggregatedResource.php new file mode 100644 index 0000000..b1eec40 --- /dev/null +++ b/app/Http/Resources/Turnout/Diaspora/TurnoutDiasporaAggregatedResource.php @@ -0,0 +1,40 @@ + + */ + public function toArray(Request $request): array + { + return [ + /* + * Total number of voters subscribed to the election permanent list . + * @var integer + */ + 'initial_total' => $this->initial_total, + + /* + * Total number of people who voted in the election. + * @var integer + */ + 'total' => $this->total, + + /* + * Array of countries and their respective turnout. + * @var TurnoutDiasporaResource[] + */ + 'uats' => TurnoutDiasporaResource::collection($this->uats), + + ]; + } +} diff --git a/app/Http/Resources/Turnout/Diaspora/TurnoutDiasporaResource.php b/app/Http/Resources/Turnout/Diaspora/TurnoutDiasporaResource.php new file mode 100644 index 0000000..d78f180 --- /dev/null +++ b/app/Http/Resources/Turnout/Diaspora/TurnoutDiasporaResource.php @@ -0,0 +1,39 @@ + + */ + public function toArray(Request $request): array + { + return [ + /* + * Total number of voters subscribed to the election permanent list . + * @var integer + */ + 'initial_total' => $this->initial_total, + + /* + * Total number of people who voted in the election. + * @var integer + */ + 'total' => $this->total, + + /* + * Country code in ISO2. + * @var string + */ + 'code' => $this->place, + ]; + } +} diff --git a/app/Http/Resources/Turnout/National/TurnoutNationalAggregatedResource.php b/app/Http/Resources/Turnout/National/TurnoutNationalAggregatedResource.php new file mode 100644 index 0000000..dd2cbe8 --- /dev/null +++ b/app/Http/Resources/Turnout/National/TurnoutNationalAggregatedResource.php @@ -0,0 +1,40 @@ + + */ + public function toArray(Request $request): array + { + return [ + /* + * Total number of voters subscribed to the election permanent list . + * @var integer + */ + 'initial_total' => $this->initial_total, + + /* + * Total number of people who voted in the election. + * @var integer + */ + 'total' => $this->total, + + /* + * Array uats. + * @var TurnoutNationalResource[] + */ + 'uats' => TurnoutNationalResource::collection($this->uats), + + ]; + } +} diff --git a/app/Http/Resources/Turnout/National/TurnoutNationalResource.php b/app/Http/Resources/Turnout/National/TurnoutNationalResource.php new file mode 100644 index 0000000..dea407f --- /dev/null +++ b/app/Http/Resources/Turnout/National/TurnoutNationalResource.php @@ -0,0 +1,59 @@ + + */ + public function toArray(Request $request): array + { + return [ + /* + * Total number of voters subscribed to the election permanent list . + * @var integer + */ + 'initial_total' => $this->initial_total, + + /* + * Total number of people who voted in the election. + * @var integer + */ + 'total' => $this->total, + + /* + * Uat id referenced in nomenclature. + * @var string + */ + 'code' => $this->place, + + /* + * Uat name. + * @var string + */ + 'uat_name' => $this->getUatName($this->place), + ]; + } + + private function getUatName(int $place) + { + $uats = \Cache::rememberForever('uats', function () { + return [ + 'localities' => Locality::whereNull('parent_id')->pluck('name', 'id')->toArray(), + 'counties' => County::pluck('name', 'id')->toArray(), + ]; + }); + + return $uats['localities'][$place] ?? $uats['counties'][$place]; + } +} diff --git a/app/Http/Resources/TurnoutResource.php b/app/Http/Resources/Turnout/TurnoutResource.php similarity index 90% rename from app/Http/Resources/TurnoutResource.php rename to app/Http/Resources/Turnout/TurnoutResource.php index 8adc840..04786c9 100644 --- a/app/Http/Resources/TurnoutResource.php +++ b/app/Http/Resources/Turnout/TurnoutResource.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace App\Http\Resources; +namespace App\Http\Resources\Turnout; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; @@ -17,13 +17,13 @@ class TurnoutResource extends JsonResource public function toArray(Request $request): array { return [ - /** + /* * Total number of voters subscribed to the election permanent list . * @var integer */ 'initial_total' => $this->initial_total, - /** + /* * Total number of people who voted in the election. * @var integer */ diff --git a/app/Models/Turnout.php b/app/Models/Turnout.php index 005fda3..9ce0e3d 100644 --- a/app/Models/Turnout.php +++ b/app/Models/Turnout.php @@ -123,10 +123,10 @@ public function scopeForLevel(Builder $query, DataLevel $level, ?string $country if ($level->is(DataLevel::DIASPORA)) { $query + ->whereNotNull('country_id') ->when( $country, fn (Builder $query) => $query->where('country_id', $country), - fn (Builder $query) => $query->whereNotNull('country_id') ); if (! $aggregate) { diff --git a/config/scramble.php b/config/scramble.php index 2d96016..b0f9f31 100644 --- a/config/scramble.php +++ b/config/scramble.php @@ -54,7 +54,7 @@ /* * URL to an image that displays as a small square logo next to the title, above the table of contents. */ - 'logo' => null, + 'logo' => '/favicon/web-app-manifest-512x512.png', /* * Use to fetch the credential policy for the Try It feature. Options are: omit, include (default), and same-origin diff --git a/resources/views/vendor/scramble/.gitkeep b/resources/views/vendor/scramble/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/routes/api.php b/routes/api.php index 60587b4..134c428 100644 --- a/routes/api.php +++ b/routes/api.php @@ -20,23 +20,31 @@ Route::get('counties', [NomenclatureController::class, 'counties']) ->name('counties'); - Route::get('counties/{county:code}', [NomenclatureController::class, 'county']) + Route::get('counties/{county}', [NomenclatureController::class, 'county']) ->name('county'); })->name('nomenclatures.'); - Route::group(['prefix' => '{election:slug}'], function () { + Route::group(['prefix' => '{election}'], function () { Route::group(['prefix' => 'turnout'], function () { - Route::get('/', [TurnoutController::class, 'general']) - ->name('general'); - - Route::get('counties', [TurnoutController::class, 'counties']) - ->name('by_counties'); - - Route::get('counties/{county:code}', [TurnoutController::class, 'county']) - ->name('by_county'); - - Route::get('localities/{locality:code}', [TurnoutController::class, 'locality']) - ->name('by_locality'); + Route::get('/', [TurnoutController::class, 'total']) + ->name('total'); + + /* + * Diaspora turnout + */ + Route::group(['prefix' => 'diaspora'], function () { + Route::get('/', [TurnoutController::class, 'diaspora']) + ->name('diaspora'); + Route::get('{country}', [TurnoutController::class, 'country']) + ->name('country'); + })->name('diaspora'); + + Route::group(['prefix' => 'national'], function () { + Route::get('/', [TurnoutController::class, 'national']) + ->name('national'); + Route::get('{county}', [TurnoutController::class, 'county']) + ->name('county'); + })->name('national'); })->name('turnout.'); })->name('elections.'); });