Skip to content

Commit

Permalink
chore(api): init rest api for all available resources
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <[email protected]>
  • Loading branch information
feryardiant committed Sep 8, 2023
1 parent f30f649 commit e443985
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/nusa.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
],

'addressable' => Address::class,

'routes_enable' => env('CREASI_NUSA_ROUTES_ENABLE', true),

'routes_prefix' => env('CREASI_NUSA_ROUTES_PREFIX', 'nusa'),
];
35 changes: 35 additions & 0 deletions routes/nusa.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Creasi\Nusa\Http\Controllers\DistrictController;
use Creasi\Nusa\Http\Controllers\ProvinceController;
use Creasi\Nusa\Http\Controllers\RegencyController;
use Creasi\Nusa\Http\Controllers\VillageController;
use Illuminate\Support\Facades\Route;

Route::name('nusa.')->group(function () {
Route::controller(ProvinceController::class)->prefix('provinces')->group(function () {
Route::get('/', 'index')->name('provinces.index');
Route::get('/{province}', 'show')->name('provinces.show');
Route::get('/{province}/regencies', 'regencies')->name('provinces.regencies');
Route::get('/{province}/districts', 'districts')->name('provinces.districts');
Route::get('/{province}/villages', 'villages')->name('provinces.villages');
});

Route::controller(RegencyController::class)->prefix('regencies')->group(function () {
Route::get('/', 'index')->name('regencies.index');
Route::get('/{regency}', 'show')->name('regencies.show');
Route::get('/{regency}/districts', 'districts')->name('regencies.districts');
Route::get('/{regency}/villages', 'villages')->name('regencies.villages');
});

Route::controller(DistrictController::class)->prefix('districts')->group(function () {
Route::get('/', 'index')->name('districts.index');
Route::get('/{district}', 'show')->name('districts.show');
Route::get('/{district}/villages', 'villages')->name('districts.villages');
});

Route::controller(VillageController::class)->prefix('villages')->group(function () {
Route::get('/', 'index')->name('villages.index');
Route::get('/{village}', 'show')->name('villages.show');
});
});
24 changes: 24 additions & 0 deletions src/Http/Controllers/DistrictController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Creasi\Nusa\Http\Controllers;

use Creasi\Nusa\Contracts\District;
use Creasi\Nusa\Http\Resources\NusaResource;

class DistrictController
{
public function index()
{
return NusaResource::collection([]);
}

public function show()
{
return new NusaResource([]);
}

public function villages(District $district)
{
return NusaResource::collection([]);
}
}
34 changes: 34 additions & 0 deletions src/Http/Controllers/ProvinceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Creasi\Nusa\Http\Controllers;

use Creasi\Nusa\Contracts\Province;
use Creasi\Nusa\Http\Resources\NusaResource;

class ProvinceController
{
public function index()
{
return NusaResource::collection([]);
}

public function show()
{
return new NusaResource([]);
}

public function regencies(Province $province)
{
return NusaResource::collection([]);
}

public function districts(Province $province)
{
return NusaResource::collection([]);
}

public function villages(Province $province)
{
return NusaResource::collection([]);
}
}
29 changes: 29 additions & 0 deletions src/Http/Controllers/RegencyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Creasi\Nusa\Http\Controllers;

use Creasi\Nusa\Contracts\Regency;
use Creasi\Nusa\Http\Resources\NusaResource;

class RegencyController
{
public function index()
{
return NusaResource::collection([]);
}

public function show()
{
return new NusaResource([]);
}

public function districts(Regency $regency)
{
return NusaResource::collection([]);
}

public function villages(Regency $regency)
{
return NusaResource::collection([]);
}
}
18 changes: 18 additions & 0 deletions src/Http/Controllers/VillageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Creasi\Nusa\Http\Controllers;

use Creasi\Nusa\Http\Resources\NusaResource;

class VillageController
{
public function index()
{
return NusaResource::collection([]);
}

public function show()
{
return new NusaResource([]);
}
}
15 changes: 15 additions & 0 deletions src/Http/Requests/NusaRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Creasi\Nusa\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class NusaRequest extends FormRequest
{
public function rules(): array
{
return [
'search' => ['nullable', 'string'],
];
}
}
14 changes: 14 additions & 0 deletions src/Http/Resources/NusaResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Creasi\Nusa\Http\Resources;

use Creasi\Nusa\Models\Model;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @property-read Model $resource
*/
class NusaResource extends JsonResource
{
//
}
13 changes: 13 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Creasi\Nusa;

use Creasi\Nusa\Console\SyncCommand;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;

class ServiceProvider extends IlluminateServiceProvider
Expand All @@ -18,6 +19,8 @@ public function boot()

$this->registerCommands();
}

$this->defineRoutes();
}

public function register()
Expand All @@ -37,6 +40,16 @@ public function register()
$this->registerBindings();
}

protected function defineRoutes()
{
if (app()->routesAreCached() && config('creasi.nusa.routes_enable') === false) {
return;
}

Route::prefix(config('creasi.nusa.routes_prefix', 'nusa'))
->group(self::LIB_PATH.'/routes/nusa.php');
}

protected function registerPublishables()
{
$this->publishes([
Expand Down
19 changes: 19 additions & 0 deletions tests/Features/DistrictsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Creasi\Tests\Features;

use Creasi\Tests\TestCase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;

#[Group('api')]
class DistrictsTest extends TestCase
{
#[Test]
public function it_should_be_true()
{
$response = $this->getJson('nusa/districts');

$response->assertOk();
}
}
19 changes: 19 additions & 0 deletions tests/Features/ProvincesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Creasi\Tests\Features;

use Creasi\Tests\TestCase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;

#[Group('api')]
class ProvincesTest extends TestCase
{
#[Test]
public function it_should_be_true()
{
$response = $this->getJson('nusa/provinces');

$response->assertOk();
}
}
19 changes: 19 additions & 0 deletions tests/Features/RegenciesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Creasi\Tests\Features;

use Creasi\Tests\TestCase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;

#[Group('api')]
class RegenciesTest extends TestCase
{
#[Test]
public function it_should_be_true()
{
$response = $this->getJson('nusa/regencies');

$response->assertOk();
}
}
19 changes: 19 additions & 0 deletions tests/Features/VillagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Creasi\Tests\Features;

use Creasi\Tests\TestCase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;

#[Group('api')]
class VillagesTest extends TestCase
{
#[Test]
public function it_should_be_true()
{
$response = $this->getJson('nusa/villages');

$response->assertOk();
}
}

0 comments on commit e443985

Please sign in to comment.