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

Feature FormRequest::fulfill() method untuk handle storing & updating data dari front-end #62

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Contracts/FormRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Creasi\Base\Contracts;

/**
* @method \Creasi\Base\Models\User|null user()
*/
interface FormRequest
{
// .
}
8 changes: 4 additions & 4 deletions src/Http/Controllers/AddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function index(Entity $entity)
*/
public function store(StoreRequest $request, Entity $entity)
{
$item = $request->storeFor($entity);
$item = $request->fulfill($entity);

return $this->show($item, $request)->setStatusCode(201);
}
Expand All @@ -52,9 +52,9 @@ public function show(Address $model, Request $request, int $address = null)
*/
public function update(UpdateRequest $request, Address $model, int $address = null)
{
$address = $model->newQuery()->findOrFail($address);

$address->update($request->validated());
$request->fulfill(
$address = $model->newQuery()->findOrFail($address)
);

return $this->show($address, $request);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Http/Controllers/CompanyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public function index(Company $company)
*/
public function store(StoreRequest $request, Company $company)
{
/** @var Company $item */
$item = $company->create($request->validated());
$item = $request->fulfill($company);

return $this->show($item, $request)->setStatusCode(201);
}
Expand All @@ -52,7 +51,7 @@ public function show(Company $company, Request $request)
*/
public function update(UpdateRequest $request, Company $company)
{
$company->update($request->validated());
$request->fulfill($company);

return $this->show($company, $request);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/EmployeeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function index(Company $company)
public function store(StoreRequest $request, Company $company)
{
/** @var Employee */
$item = $company->employees()->create($request->validated());
$item = $request->fulfill($company);

return $this->show($item, $request)->setStatusCode(201);
}
Expand All @@ -51,7 +51,7 @@ public function show(Employee $employee, Request $request)
*/
public function update(UpdateRequest $request, Employee $employee)
{
$employee->update($request->validated());
$request->fulfill($employee);

return $this->show($employee, $request);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Http/Controllers/FileUploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function index(Entity $entity)
*/
public function store(StoreRequest $request, Entity $entity)
{
$item = $request->storeFor($entity);
$item = $request->fulfill($entity);

return $this->show($item, $request)->setStatusCode(201);
}
Expand All @@ -52,9 +52,9 @@ public function show(FileUpload $model, Request $request, string $file = null)
*/
public function update(UpdateRequest $request, FileUpload $model, string $file = null)
{
$file = $model->newQuery()->findOrFail($file);

$file->update($request->validated());
$request->fulfill(
$file = $model->newQuery()->findOrFail($file)
);

return $this->show($file, $request);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/StakeholderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function show(Stakeholder $stakeholder, Request $request)
*/
public function update(UpdateRequest $request, Stakeholder $stakeholder)
{
$stakeholder->update($request->validated());
$request->fulfill($stakeholder);

return $this->show($stakeholder, $request);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Http/Requests/Address/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Creasi\Base\Http\Requests\Address;

use Creasi\Base\Http\Requests\FormRequest;
use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Creasi\Base\Models\Address;
use Creasi\Base\Models\Enums\AddressType;
use Creasi\Nusa\Contracts\HasAddresses;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreRequest extends FormRequest
class StoreRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand All @@ -31,7 +32,7 @@ public function rules(): array
];
}

public function storeFor(HasAddresses $entity)
public function fulfill(HasAddresses $entity)
{
$address = Address::query()->make($this->validated());

Expand Down
11 changes: 9 additions & 2 deletions src/Http/Requests/Address/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Creasi\Base\Http\Requests\Address;

use Creasi\Base\Http\Requests\FormRequest;
use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Creasi\Base\Models\Address;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateRequest extends FormRequest
class UpdateRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand All @@ -27,4 +29,9 @@ public function rules(): array
'summary' => ['nullable', 'string', 'max:200'],
];
}

public function fulfill(Address $address)
{
return $address->update($this->validated());
}
}
11 changes: 9 additions & 2 deletions src/Http/Requests/Company/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Creasi\Base\Http\Requests\Company;

use Creasi\Base\Http\Requests\FormRequest;
use Creasi\Base\Contracts\Company;
use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreRequest extends FormRequest
class StoreRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand All @@ -20,4 +22,9 @@ public function rules(): array
'summary' => ['nullable', 'string', 'max:200'],
];
}

public function fulfill(Company $company)
{
return $company->create($this->validated());
}
}
11 changes: 9 additions & 2 deletions src/Http/Requests/Company/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Creasi\Base\Http\Requests\Company;

use Creasi\Base\Http\Requests\FormRequest;
use Creasi\Base\Contracts\Company;
use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Illuminate\Foundation\Http\FormRequest;

class UpdateRequest extends FormRequest
class UpdateRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand All @@ -19,4 +21,9 @@ public function rules(): array
'summary' => ['nullable', 'string', 'max:200'],
];
}

public function fulfill(Company $company)
{
return $company->update($this->validated());
}
}
11 changes: 9 additions & 2 deletions src/Http/Requests/Employee/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Creasi\Base\Http\Requests\Employee;

use Creasi\Base\Http\Requests\FormRequest;
use Creasi\Base\Contracts\Company;
use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Creasi\Base\Models\Enums\Gender;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreRequest extends FormRequest
class StoreRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand All @@ -22,4 +24,9 @@ public function rules(): array
'summary' => ['nullable', 'string', 'max:200'],
];
}

public function fulfill(Company $company)
{
return $company->employees()->create($this->validated());
}
}
11 changes: 9 additions & 2 deletions src/Http/Requests/Employee/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Creasi\Base\Http\Requests\Employee;

use Creasi\Base\Http\Requests\FormRequest;
use Creasi\Base\Contracts\Employee;
use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Illuminate\Foundation\Http\FormRequest;

class UpdateRequest extends FormRequest
class UpdateRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand All @@ -19,4 +21,9 @@ public function rules(): array
'summary' => ['nullable', 'string', 'max:200'],
];
}

public function fulfill(Employee $employee)
{
return $employee->update($this->validated());
}
}
7 changes: 4 additions & 3 deletions src/Http/Requests/FileUpload/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Creasi\Base\Http\Requests\FileUpload;

use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Creasi\Base\Contracts\HasFileUploads;
use Creasi\Base\Http\Requests\FormRequest;
use Creasi\Base\Models\Enums\FileUploadType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreRequest extends FormRequest
class StoreRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand All @@ -22,7 +23,7 @@ public function rules(): array
];
}

public function storeFor(HasFileUploads $entity)
public function fulfill(HasFileUploads $entity)
{
/** @var FileUploadType */
$type = $this->enum('type', FileUploadType::class);
Expand Down
11 changes: 9 additions & 2 deletions src/Http/Requests/FileUpload/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Creasi\Base\Http\Requests\FileUpload;

use Creasi\Base\Http\Requests\FormRequest;
use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Creasi\Base\Models\FileUpload;
use Illuminate\Foundation\Http\FormRequest;

class UpdateRequest extends FormRequest
class UpdateRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand All @@ -18,4 +20,9 @@ public function rules(): array
'upload' => ['nullable', 'file'],
];
}

public function fulfill(FileUpload $file)
{
return $file->update($this->validated());
}
}
13 changes: 0 additions & 13 deletions src/Http/Requests/FormRequest.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/Http/Requests/ProfileRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Creasi\Base\Http\Requests;

use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Creasi\Base\Models\Enums\Education;
use Creasi\Base\Models\Enums\TaxStatus;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProfileRequest extends FormRequest
class ProfileRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Requests/SettingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Creasi\Base\Http\Requests;

class SettingRequest extends FormRequest
use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Illuminate\Foundation\Http\FormRequest;

class SettingRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand Down
5 changes: 3 additions & 2 deletions src/Http/Requests/Stakeholder/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace Creasi\Base\Http\Requests\Stakeholder;

use Creasi\Base\Contracts\Company;
use Creasi\Base\Http\Requests\FormRequest;
use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Creasi\Base\Models\Enums\BusinessRelativeType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreRequest extends FormRequest
class StoreRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand Down
11 changes: 9 additions & 2 deletions src/Http/Requests/Stakeholder/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Creasi\Base\Http\Requests\Stakeholder;

use Creasi\Base\Http\Requests\FormRequest;
use Creasi\Base\Contracts\FormRequest as FormRequestContract;
use Creasi\Base\Contracts\Stakeholder;
use Illuminate\Foundation\Http\FormRequest;

class UpdateRequest extends FormRequest
class UpdateRequest extends FormRequest implements FormRequestContract
{
/**
* @return array<string, array>
Expand All @@ -19,4 +21,9 @@ public function rules(): array
'summary' => ['nullable', 'string', 'max:200'],
];
}

public function fulfill(Stakeholder $stakeholder)
{
return $stakeholder->update($this->validated());
}
}
2 changes: 1 addition & 1 deletion src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function resolveBusinessRelativeType(): BusinessRelativeType

private function currentRoutePrefix(string $prefix = null)
{
$name = \explode('.', $this->router->currentRouteName())[0];
$name = \explode('.', $this->router->currentRouteName())[1];

return $prefix ? $name === $prefix : $name;
}
Expand Down
Loading