Skip to content

Commit

Permalink
wip on turnouts
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghelupu17 committed Nov 12, 2024
1 parent 03f1c0d commit 4d726e2
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/V1/NomenclatureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
87 changes: 85 additions & 2 deletions app/Http/Controllers/Api/V1/TurnoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources\Turnout\Diaspora;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class TurnoutDiasporaAggregatedResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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),

];
}
}
39 changes: 39 additions & 0 deletions app/Http/Resources/Turnout/Diaspora/TurnoutDiasporaResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources\Turnout\Diaspora;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class TurnoutDiasporaResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources\Turnout\National;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class TurnoutNationalAggregatedResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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),

];
}
}
59 changes: 59 additions & 0 deletions app/Http/Resources/Turnout/National/TurnoutNationalResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources\Turnout\National;

use App\Models\County;
use App\Models\Locality;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class TurnoutNationalResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Turnout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion config/scramble.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file.
Loading

0 comments on commit 4d726e2

Please sign in to comment.