Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Nov 13, 2024
1 parent 767527e commit 211fb55
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 88 deletions.
40 changes: 13 additions & 27 deletions app/Http/Controllers/Api/V1/NomenclatureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,33 @@
use App\Models\Country;
use App\Models\County;
use App\Models\Election;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;

class NomenclatureController extends Controller
{
public function elections(): JsonResponse
public function elections(): JsonResource
{
return response()->json(
ElectionResource::collection(
Election::query()
->orderBy('is_live', 'desc')
->get()
)
return ElectionResource::collection(
Election::query()
->orderBy('is_live', 'desc')
->get()
);
}

public function counties(): JsonResponse
public function countries(): JsonResource
{
return response()->json(
CountyResource::collection(
County::with(['localities' => function ($query) {
$query->whereNull('parent_id');
}])->get()
)
);
return CountryResource::collection(Country::all());
}

public function county(County $county): JsonResponse
public function counties(): JsonResource
{
return response()->json(
new CountyResource(
$county->load('localities')
)
);
return CountyResource::collection(County::all());
}

public function countries(): JsonResponse
public function county(County $county): JsonResource
{
return response()->json(
CountryResource::collection(
Country::all()
)
return CountyResource::make(
$county->loadMissing('localities')
);
}
}
24 changes: 10 additions & 14 deletions app/Http/Controllers/Api/V1/TurnoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Models\Election;
use App\Models\Turnout;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;

class TurnoutController extends Controller
{
Expand All @@ -26,7 +27,6 @@ public function total(Election $election): JsonResponse
->forLevel(
level: DataLevel::TOTAL,
)
->toBase()
->first();

return response()->json(TurnoutResource::make($result));
Expand All @@ -40,7 +40,6 @@ public function diaspora(Election $election): JsonResponse
level: DataLevel::DIASPORA,
aggregate: true,
)
->toBase()
->first();

$general->uats = Turnout::query()
Expand All @@ -55,19 +54,16 @@ public function diaspora(Election $election): JsonResponse
return response()->json(TurnoutDiasporaAggregatedResource::make($general));
}

public function country(Election $election, Country $country): JsonResponse
public function country(Election $election, Country $country): JsonResource
{
return response()->json(
TurnoutDiasporaResource::make(
Turnout::query()
->whereBelongsTo($election)
->forLevel(
level: DataLevel::DIASPORA,
country: $country->id,
)
->toBase()
->first()
)
return TurnoutDiasporaResource::make(
Turnout::query()
->whereBelongsTo($election)
->forLevel(
level: DataLevel::DIASPORA,
country: $country->id,
)
->first()
);
}

Expand Down
3 changes: 1 addition & 2 deletions app/Http/Resources/Nomenclature/CountyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public function toArray(Request $request): array
return [
'id' => $this->id,
'name' => $this->name,
'code' => $this->code,
'localities' => LocationResource::collection(($this->whenLoaded('localities'))),
'localities' => LocalityResource::collection($this->whenLoaded('localities')),
];
}
}
1 change: 0 additions & 1 deletion app/Http/Resources/Nomenclature/ElectionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function toArray(Request $request): array
'type' => $this->type->getLabel(),
'is_live' => $this->is_live,
'slug' => $this->slug,
'created_at' => $this->created_at,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class LocationResource extends JsonResource
class LocalityResource extends JsonResource
{
/**
* Transform the resource into an array.
Expand Down
10 changes: 6 additions & 4 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\QueryException;
use Illuminate\Routing\Route;
use Illuminate\Encryption\MissingAppKeyException;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Number;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
Expand All @@ -29,6 +29,8 @@ public function register(): void
}

$this->registerStrMacros();

JsonResource::withoutWrapping();
}

/**
Expand All @@ -49,9 +51,9 @@ public function boot(): void

$this->setSeoDefaults();

Scramble::routes(function (Route $route) {
return Str::startsWith($route->uri, 'api/');
});
Scramble::registerApi('v1', [
'api_path' => 'api/v1',
]);
}

protected function registerStrMacros(): void
Expand Down
80 changes: 41 additions & 39 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,47 @@
use App\Http\Controllers\Api\V1\TurnoutController;
use Illuminate\Support\Facades\Route;

Route::group(['prefix' => 'v1'], function () {
Route::get('/health', function () {
return response()->json(['status' => 'ok']);
Route::group([
'as' => 'api.v1.',
'prefix' => 'v1',
], function () {
Route::group([
'as' => 'nomenclatures.',
'prefix' => 'nomenclatures',
'controller' => NomenclatureController::class,
], function () {
Route::get('elections', 'elections')->name('elections');
Route::get('countries', 'countries')->name('countries');
Route::get('counties', 'counties')->name('counties');
Route::get('counties/{county}', 'county')->name('county');
});
Route::group(['prefix' => 'nomenclatures'], function () {
Route::get('elections', [NomenclatureController::class, 'elections'])
->name('elections');

Route::get('countries', [NomenclatureController::class, 'countries'])
->name('countries');

Route::get('counties', [NomenclatureController::class, 'counties'])
->name('counties');

Route::get('counties/{county}', [NomenclatureController::class, 'county'])
->name('county');
})->name('nomenclatures.');

Route::group(['prefix' => '{election}'], function () {
Route::group(['prefix' => 'turnout'], function () {
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.');
Route::group([
'as' => 'elections.',
'prefix' => '{election}',
], function () {
Route::group([
'as' => 'turnout.',
'prefix' => 'turnout',
'controller' => TurnoutController::class,
], function () {
Route::get('/', 'total')->name('total');

Route::group([
'as' => 'diaspora.',
'prefix' => 'diaspora',
], function () {
Route::get('/', 'diaspora')->name('diaspora');
Route::get('{country}', 'country')->name('country');
});

Route::group([
'as' => 'national.',
'prefix' => 'national',
], function () {
Route::get('/', 'national')->name('national');
Route::get('{county}', 'county')->name('county');
});
});
});
});

0 comments on commit 211fb55

Please sign in to comment.