Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
feat: add restore endpoints
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <[email protected]>
  • Loading branch information
feryardiant committed Mar 27, 2024
1 parent 53695cd commit 253123b
Show file tree
Hide file tree
Showing 39 changed files with 553 additions and 170 deletions.
19 changes: 19 additions & 0 deletions routes/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,24 @@

Route::middleware('auth:sanctum')->group(function () {
Route::apiResource('companies', Controllers\CompanyController::class);
// Route::prefix('companies')->controller(Controllers\CompanyController::class)->group(function () {
// Route::put('{company}/restore', 'restore')->name('companies.restore')->withTrashed();
// });

Route::apiResource('employees', Controllers\EmployeeController::class);
// Route::prefix('employees')->controller(Controllers\EmployeeController::class)->group(function () {
// Route::put('{employee}/restore', 'restore')->name('employees.restore')->withTrashed();
// });

Route::apiResource('addresses', Controllers\AddressController::class);
Route::prefix('addresses')->controller(Controllers\AddressController::class)->group(function () {
Route::put('{address}/restore', 'restore')->name('addresses.restore')->withTrashed();
});

Route::apiResource('files', Controllers\FileController::class);
Route::prefix('files')->controller(Controllers\FileController::class)->group(function () {
Route::put('{file}/restore', 'restore')->name('files.restore')->withTrashed();
});

Route::apiSingleton('profile', Controllers\ProfileController::class);
Route::apiSingleton('setting', Controllers\SettingController::class);
Expand All @@ -45,5 +60,9 @@

Route::apiResource($route, Controllers\StakeholderController::class)
->parameter((string) $route, 'stakeholder');

// Route::prefix($route)->controller(Controllers\StakeholderController::class)->group(function () use ($route) {
// Route::put('{stakeholder}/restore', 'restore')->name($route.'.restore')->withTrashed();
// });
}
});
51 changes: 32 additions & 19 deletions src/Http/Controllers/AddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use Creasi\Base\Database\Models\Address;
use Creasi\Base\Database\Models\Entity;
use Creasi\Base\Http\Requests\Address\DeleteRequest;
use Creasi\Base\Http\Requests\Address\IndexRequest;
use Creasi\Base\Http\Requests\Address\StoreRequest;
use Creasi\Base\Http\Requests\Address\UpdateRequest;
use Creasi\Base\Http\Resources\Address\AddressCollection;
use Creasi\Base\Http\Resources\Address\AddressResource;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

class AddressController extends Controller
{
Expand All @@ -18,50 +21,60 @@ public function __construct()
}

/**
* @return AddressCollection
* Index endpoint for addresses.
*/
public function index(Entity $entity)
public function index(Entity $entity, IndexRequest $request): AddressCollection
{
$items = $entity->addresses()->latest();

return new AddressCollection($items->paginate());
return new AddressCollection(
$request->fulfill($entity)->paginate()
);
}

/**
* @return \Illuminate\Http\Response
* Create endpoint for address.
*/
public function store(StoreRequest $request, Entity $entity)
public function store(Entity $entity, StoreRequest $request): JsonResponse
{
$item = $request->fulfill($entity);
$address = $request->fulfill($entity);

return $this->show($item, $request)->setStatusCode(201);
return $this->show($address)->toResponse($request)->setStatusCode(201);
}

/**
* @return \Illuminate\Http\Response
* Show endpoint for a single address.
*/
public function show(Address $address, Request $request)
public function show(Address $address): AddressResource
{
return AddressResource::make($address)->toResponse($request);
return new AddressResource($address);
}

/**
* @return \Illuminate\Http\Response
* Update endpoint for a single address.
*/
public function update(UpdateRequest $request, Address $address)
public function update(Address $address, UpdateRequest $request): AddressResource
{
$request->fulfill($address);

return $this->show($address, $request);
return $this->show($address);
}

/**
* @return \Illuminate\Http\Response
* Delete endpoint for a single address.
*/
public function destroy(Address $address)
public function destroy(Address $address, DeleteRequest $request): Response
{
$address->delete();
$request->fulfill($address);

return response()->noContent();
}

/**
* Restore endpoint for a single address.
*/
public function restore(Address $address): AddressResource
{
$address->restore();

return $this->show($address);
}
}
53 changes: 32 additions & 21 deletions src/Http/Controllers/CompanyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace Creasi\Base\Http\Controllers;

use Creasi\Base\Database\Models\Contracts\Company;
use Creasi\Base\Http\Requests\Company\DeleteRequest;
use Creasi\Base\Http\Requests\Company\IndexRequest;
use Creasi\Base\Http\Requests\Company\StoreRequest;
use Creasi\Base\Http\Requests\Company\UpdateRequest;
use Creasi\Base\Http\Resources\Company\CompanyCollection;
use Creasi\Base\Http\Resources\Company\CompanyResource;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

class CompanyController extends Controller
{
Expand All @@ -17,52 +20,60 @@ public function __construct()
}

/**
* @return CompanyCollection
* Index endpoint for companies.
*/
public function index(Company $company)
public function index(Company $company, IndexRequest $request): CompanyCollection
{
$items = $company->subsidiaries()->latest('id');

$items->with('stakeholder');

return new CompanyCollection($items->paginate());
return new CompanyCollection(
$request->fulfill($company)->paginate()
);
}

/**
* @return CompanyResource
* Create endpoint for company.
*/
public function store(StoreRequest $request, Company $company)
public function store(Company $company, StoreRequest $request): JsonResponse
{
$item = $request->fulfill($company);
$company = $request->fulfill($company);

return $this->show($item, $request)->setStatusCode(201);
return $this->show($company)->toResponse($request)->setStatusCode(201);
}

/**
* @return CompanyResource
* Show endpoint for a single company.
*/
public function show(Company $company, Request $request)
public function show(Company $company): CompanyResource
{
return CompanyResource::make($company)->toResponse($request);
return new CompanyResource($company);
}

/**
* @return CompanyResource
* Update endpoint for a single company.
*/
public function update(UpdateRequest $request, Company $company)
public function update(Company $company, UpdateRequest $request): CompanyResource
{
$request->fulfill($company);

return $this->show($company, $request);
return $this->show($company);
}

/**
* @return \Illuminate\Http\Response
* Delete endpoint for a single company.
*/
public function destroy(Company $company)
public function destroy(Company $company, DeleteRequest $request): Response
{
$company->delete();
$request->fulfill($company);

return response()->noContent();
}

/**
* Restore endpoint for a single company.
*/
public function restore(Company $company): CompanyResource
{
$company->restore();

return $this->show($company);
}
}
1 change: 0 additions & 1 deletion src/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ protected function resourceAbilityMap()
{
$abilities = [
'restore' => 'restore',
'forceDestroy' => 'forceDelete',
];

return \array_merge(
Expand Down
51 changes: 32 additions & 19 deletions src/Http/Controllers/EmployeeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use Creasi\Base\Database\Models\Contracts\Company;
use Creasi\Base\Database\Models\Contracts\Employee;
use Creasi\Base\Http\Requests\Employee\DeleteRequest;
use Creasi\Base\Http\Requests\Employee\IndexRequest;
use Creasi\Base\Http\Requests\Employee\StoreRequest;
use Creasi\Base\Http\Requests\Employee\UpdateRequest;
use Creasi\Base\Http\Resources\Employee\EmployeeCollection;
use Creasi\Base\Http\Resources\Employee\EmployeeResource;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

class EmployeeController extends Controller
{
Expand All @@ -18,51 +21,61 @@ public function __construct()
}

/**
* @return EmployeeCollection
* Index endpoint for employees.
*/
public function index(Company $company)
public function index(Company $company, IndexRequest $request): EmployeeCollection
{
$items = $company->employees()->latest();

return new EmployeeCollection($items->paginate());
return new EmployeeCollection(
$request->fulfill($company)->paginate()
);
}

/**
* @return EmployeeResource
* Create endpoint for employee.
*/
public function store(StoreRequest $request, Company $company)
public function store(Company $company, StoreRequest $request): JsonResponse
{
/** @var Employee */
$item = $request->fulfill($company);
$employee = $request->fulfill($company);

return $this->show($item, $request)->setStatusCode(201);
return $this->show($employee)->toResponse($request)->setStatusCode(201);
}

/**
* @return EmployeeResource
* Show endpoint for a single employee.
*/
public function show(Employee $employee, Request $request)
public function show(Employee $employee): EmployeeResource
{
return EmployeeResource::make($employee)->toResponse($request);
return new EmployeeResource($employee);
}

/**
* @return EmployeeResource
* Update endpoint for a single employee.
*/
public function update(UpdateRequest $request, Employee $employee)
public function update(Employee $employee, UpdateRequest $request): EmployeeResource
{
$request->fulfill($employee);

return $this->show($employee, $request);
return $this->show($employee);
}

/**
* @return \Illuminate\Http\Response
* Delete endpoint for a single employee.
*/
public function destroy(Employee $employee)
public function destroy(Employee $employee, DeleteRequest $request): Response
{
$employee->delete();
$request->fulfill($employee);

return response()->noContent();
}

/**
* Restore endpoint for a single employee.
*/
public function restore(Employee $employee): EmployeeResource
{
$employee->restore();

return $this->show($employee);
}
}
Loading

0 comments on commit 253123b

Please sign in to comment.