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

Conversation

feryardiant
Copy link
Contributor

@feryardiant feryardiant commented Oct 11, 2023

Di PR ini saya propose alur storing & updating data payload ke model "tersentralisasi" yaitu di FormRequest dengan method fulfill. Cara ini memungkinkan kita untuk meng-handle mekanisme storing & updating data di satu tempat yang sama baik untuk API controller maupun UI controller.

Misal kita ada api controller seperti ini

// Creasi\Base\Http\Controllers\Api\CompanyController di package ini
public function store(StoreRequest $request, Company $company) {
    $item = $request->fulfill($company);

    return CompanyResource::make($item);
}

dan UI controller harusnya cukup dituliskan seperti ini.

// App\Http\Controllers\CompanyController dari app
public function store(StoreRequest $request, Company $company) {
    $item = $request->fulfill();

    return back()->with(['message' => __('company.data.stored')]);
}

Yang membedakan hanyalah return atau output dari controller tersebut, untuk API controller dia me-return json, sedangkan untuk UI controller me-return html / view.

Jadi nantinya aplikasi yang dikembangkan menggunakan package ini harusnya "hanya pakai" fungsional dan fitur yang sudah disediakan di package ini saja, gak perlu lagi mendefinisikan sendiri mekanisme penyimpanan data nya.

Tidak menutup kemungkinan metode ini juga akan kita gunakan pada project lain (app) baik yang menggunakan atau tidak menggunakan package base ini.

Rule

  1. Setiap FormRequest harus memiliki method fulfill untuk meng-handle penyimpanan data
  2. Parameter dari method fulfill boleh berbeda menyesuaikan kebutuhan pada update dan store di Controller.
  3. Perlu dipastikan dulu bahwa kebutuhan baik untuk data handling maupun validasi antara api controller dan ui controller adalah sama

This way we try to ensure either ui and api controller has
similar way to store the request payload to model. Say we have

```php
// Api\CompanyController
public function store(StoreRequest $request, Company $company) {
    $item = $request->fulfill($company);

   return CompanyResource::make($item);
}
```

and in the UI controller should looks like this

```php
//  CompanyController
public function store(StoreRequest $request, Company $company) {
    $item = $request->fulfill();

    return back()->with(['message' => __('company.data.stored')]);
}
```

But we has to make sure that the way UI works the same way as the API

Signed-off-by: Fery Wardiyanto <[email protected]>
@feryardiant feryardiant added chore A task that needs to be done enhancement New feature or request labels Oct 11, 2023
@feryardiant feryardiant self-assigned this Oct 11, 2023
@feryardiant feryardiant requested a review from a team as a code owner October 11, 2023 21:23
@feryardiant feryardiant changed the title Patch api workflow Feature FormRequest::fulfill() method untuk handle storing & updating data dari front-end Oct 11, 2023
@feryardiant feryardiant force-pushed the patch-api branch 2 times, most recently from 50ab71b to 3cd32cb Compare October 11, 2023 21:57
@feryardiant feryardiant merged commit 3bd952e into main Oct 11, 2023
7 checks passed
@feryardiant feryardiant deleted the patch-api branch October 11, 2023 22:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
chore A task that needs to be done enhancement New feature or request
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

1 participant