Skip to content

Commit

Permalink
feat: api votes (#110)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Lupu Gheorghe <[email protected]>
  • Loading branch information
andreiio and gheorghelupu17 authored Nov 28, 2024
1 parent 2ef04dc commit 65ac597
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 91 deletions.
8 changes: 4 additions & 4 deletions app/Http/Controllers/Api/V1/NomenclatureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class NomenclatureController extends Controller
{
/**
* @operationId Elections
* @operationId Nomenclature/Elections
*/
public function elections(): JsonResource
{
Expand All @@ -29,23 +29,23 @@ public function elections(): JsonResource
}

/**
* @operationId Countries
* @operationId Nomenclature/Countries
*/
public function countries(): JsonResource
{
return CountryResource::collection(Country::all());
}

/**
* @operationId Counties
* @operationId Nomenclature/Counties
*/
public function counties(): JsonResource
{
return CountyResource::collection(County::all());
}

/**
* @operationId County
* @operationId Nomenclature/County
*/
public function county(County $county): JsonResource
{
Expand Down
185 changes: 185 additions & 0 deletions app/Http/Controllers/Api/V1/ResultsController.php
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);
}
}
12 changes: 6 additions & 6 deletions app/Http/Controllers/Api/V1/TurnoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
class TurnoutController extends Controller
{
/**
* @operationId Total
* @operationId Turnout/Total
*/
public function total(Election $election)//: JsonResource
public function total(Election $election): JsonResource
{
$result = TurnoutRepository::getForLevel(
election: $election,
Expand Down Expand Up @@ -52,7 +52,7 @@ public function total(Election $election)//: JsonResource
}

/**
* @operationId Diaspora
* @operationId Turnout/Diaspora
*/
public function diaspora(Election $election): JsonResource
{
Expand Down Expand Up @@ -110,7 +110,7 @@ public function diaspora(Election $election): JsonResource
}

/**
* @operationId DiasporaCountry
* @operationId Turnout/Diaspora/Country
*/
public function country(Election $election, Country $country): JsonResource
{
Expand Down Expand Up @@ -145,7 +145,7 @@ public function country(Election $election, Country $country): JsonResource
}

/**
* @operationId National
* @operationId Turnout/National
*/
public function national(Election $election): JsonResource
{
Expand Down Expand Up @@ -201,7 +201,7 @@ public function national(Election $election): JsonResource
}

/**
* @operationId NationalCounty
* @operationId Turnout/National/County
*/
public function county(Election $election, County $county): JsonResource
{
Expand Down
33 changes: 33 additions & 0 deletions app/Http/Resources/Result/CandidatesResource.php
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'],

];
}
}
57 changes: 57 additions & 0 deletions app/Http/Resources/Result/ResultResource.php
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),
];
}
}
1 change: 0 additions & 1 deletion app/Jobs/Parlamentare06122020/Records/FetchRecordsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function execute(): void
);
});


$electionName = $this->scheduledJob->election->getFilamentName();
$electionId = $this->scheduledJob->election_id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function handle(): void
'section' => $row['precinct_nr'],
'part' => $part,

'eligible_voters_permanent' => $row ['a'],
'eligible_voters_permanent' => $row['a'],
'eligible_voters_special' => 0,

'present_voters_permanent' => $row['b1'],
Expand Down
1 change: 0 additions & 1 deletion app/Jobs/Presidential241124/Records/FetchRecordsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function execute(): void
);
});


$electionName = $this->scheduledJob->election->getFilamentName();
$electionId = $this->scheduledJob->election_id;

Expand Down
Loading

0 comments on commit 65ac597

Please sign in to comment.