-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: Lupu Gheorghe <[email protected]>
- Loading branch information
1 parent
2ef04dc
commit 65ac597
Showing
12 changed files
with
366 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers\Api\V1; | ||
|
||
use App\Enums\DataLevel; | ||
use App\Http\Controllers\Controller; | ||
use App\Http\Resources\Result\ResultResource; | ||
use App\Models\Country; | ||
use App\Models\County; | ||
use App\Models\Election; | ||
use App\Models\Locality; | ||
use App\Repositories\RecordsRepository; | ||
use App\Repositories\VotesRepository; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class ResultsController extends Controller | ||
{ | ||
/** | ||
* @operationId Results/Total | ||
*/ | ||
public function total(Election $election): JsonResource | ||
{ | ||
$result = RecordsRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::TOTAL, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->votes = VotesRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::TOTAL, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->last_updated_at = $election->records_updated_at; | ||
|
||
$result->name = DataLevel::TOTAL->getLabel(); | ||
|
||
return ResultResource::make($result); | ||
} | ||
|
||
/** | ||
* @operationId Results/Diaspora | ||
*/ | ||
public function diaspora(Election $election): JsonResource | ||
{ | ||
$result = RecordsRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::DIASPORA, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->votes = VotesRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::DIASPORA, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->last_updated_at = $election->records_updated_at; | ||
|
||
$result->name = DataLevel::DIASPORA->getLabel(); | ||
|
||
return ResultResource::make($result); | ||
} | ||
|
||
/** | ||
* @operationId Results/Diaspora/Country | ||
*/ | ||
public function country(Election $election, Country $country): JsonResource | ||
{ | ||
$result = RecordsRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::DIASPORA, | ||
country: $country->id, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->votes = VotesRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::DIASPORA, | ||
country: $country->id, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->last_updated_at = $election->records_updated_at; | ||
|
||
$result->name = $country->name; | ||
|
||
return ResultResource::make($result); | ||
} | ||
|
||
/** | ||
* @operationId Results/National | ||
*/ | ||
public function national(Election $election): JsonResource | ||
{ | ||
$result = RecordsRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::NATIONAL, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->votes = VotesRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::NATIONAL, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->last_updated_at = $election->records_updated_at; | ||
|
||
$result->name = DataLevel::NATIONAL->getLabel(); | ||
|
||
return ResultResource::make($result); | ||
} | ||
|
||
/** | ||
* @operationId Results/National/County | ||
*/ | ||
public function county(Election $election, County $county): JsonResource | ||
{ | ||
$result = RecordsRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::NATIONAL, | ||
county: $county->id, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->votes = VotesRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::NATIONAL, | ||
county: $county->id, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->last_updated_at = $election->records_updated_at; | ||
|
||
$result->name = $county->name; | ||
|
||
return ResultResource::make($result); | ||
} | ||
|
||
/** | ||
* @operationId Results/National/County/Locality | ||
*/ | ||
public function locality(Election $election, County $county, Locality $locality): JsonResource | ||
{ | ||
abort_unless($locality->county_id === $county->id, 404); | ||
|
||
$result = RecordsRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::NATIONAL, | ||
county: $county->id, | ||
locality: $locality->id, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->votes = VotesRepository::getForLevel( | ||
election: $election, | ||
level: DataLevel::NATIONAL, | ||
county: $county->id, | ||
locality: $locality->id, | ||
aggregate: true, | ||
toBase: true, | ||
); | ||
|
||
$result->last_updated_at = $election->records_updated_at; | ||
|
||
$result->name = "{$locality->name}, {$county->name}"; | ||
|
||
return ResultResource::make($result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Resources\Result; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class CandidatesResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
/* | ||
* @var string $name | ||
* Name of the candidate | ||
*/ | ||
'name' => $this['name'], | ||
/* | ||
* @var int $votes | ||
* Number of votes the candidate received | ||
*/ | ||
'votes' => (int) $this['votes'], | ||
|
||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Resources\Result; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class ResultResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
|
||
/* | ||
* @var \DateTime $last_updated_at | ||
*/ | ||
'last_updated_at' => data_get($this->resource, 'last_updated_at')?->toDateTimeString(), | ||
|
||
/* | ||
* @var string $name | ||
*/ | ||
|
||
'name' => $this->name, | ||
|
||
/* | ||
* @var int $eligible_voters_total | ||
*/ | ||
'eligible_voters_total' => (int) $this->eligible_voters_total, | ||
|
||
/* | ||
* @var int $present_voters_total | ||
*/ | ||
'present_voters_total' => (int) $this->present_voters_total, | ||
|
||
/* | ||
* @var int $votes_valid | ||
* Number of valid votes | ||
*/ | ||
'votes_valid' => (int) $this->votes_valid, | ||
|
||
/* | ||
* @var int $votes_null | ||
* Number of null votes | ||
*/ | ||
'votes_null' => (int) $this->votes_null, | ||
|
||
'candidates' => CandidatesResource::collection($this->votes), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.