generated from beyondCRUD/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from RapidKit/feature/base-generic-class
feature: Base class for reusable flow
- Loading branch information
Showing
5 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RapidKit\LaravelRestify\Bases; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Pagination\LengthAwarePaginator; | ||
use Illuminate\Pagination\Paginator; | ||
use Spatie\QueryBuilder\QueryBuilder; | ||
|
||
class BaseController extends Controller | ||
{ | ||
/** | ||
* Get the model instance. | ||
* | ||
* @return BaseModel | ||
*/ | ||
public function model() | ||
{ | ||
return new BaseModel; | ||
} | ||
|
||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return Paginator|LengthAwarePaginator<array-key, BaseData> | ||
*/ | ||
public function index(): Paginator|LengthAwarePaginator | ||
{ | ||
return QueryBuilder::for($this->model()->query()) | ||
->allowedFilters($this->model()->filterable ?? []) | ||
->defaultSort($this->model()->sortable ?? []) | ||
->allowedSorts($this->model()->sortable ?? []) | ||
->latest('id') | ||
->paginate(request()->get('limit', 15)); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
*/ | ||
public function store(BaseRequest $request): BaseModel | ||
{ | ||
$attributes = $request->toDto(); | ||
|
||
return $this->model()->query()->create($attributes->toArray()); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
*/ | ||
public function show(string|int $id): BaseModel | ||
{ | ||
return $this->model()->query()->findOrFail($id); | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
*/ | ||
public function update(BaseRequest $request, string|int $id): \Illuminate\Http\Response | ||
{ | ||
$item = $this->model()->query()->findOrFail($id); | ||
$item->update($request->toDto()->toArray()); | ||
|
||
return response()->noContent(); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
*/ | ||
public function destroy(string|int $id): \Illuminate\Http\Response | ||
{ | ||
$item = $this->model()->query()->findOrFail($id); | ||
$item->delete(); | ||
|
||
return response()->noContent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RapidKit\LaravelRestify\Bases; | ||
|
||
class BaseData extends \Spatie\LaravelData\Data | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RapidKit\LaravelRestify\Bases; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUlids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class BaseModel extends \Illuminate\Database\Eloquent\Model | ||
{ | ||
use HasFactory, HasUlids; | ||
|
||
public $filterable = []; | ||
|
||
public $sortable = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RapidKit\LaravelRestify\Bases; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class BaseRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
|
||
public function toDto() | ||
{ | ||
return BaseData::from(...$this->all()); | ||
} | ||
} |