Skip to content

Commit

Permalink
Merge pull request #8 from RapidKit/feature/base-generic-class
Browse files Browse the repository at this point in the history
feature: Base class for reusable flow
  • Loading branch information
ramaID authored Jun 16, 2024
2 parents 88f0e5e + 9b3e338 commit 270f9c8
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 5 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ jobs:
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.2, 8.3]
laravel: [10.*]
os: [ubuntu-latest]
php: [8.3]
laravel: [11.*]
stability: [prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*
carbon: ^2.63

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
78 changes: 78 additions & 0 deletions src/Bases/BaseController.php
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();
}
}
9 changes: 9 additions & 0 deletions src/Bases/BaseData.php
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
{
}
17 changes: 17 additions & 0 deletions src/Bases/BaseModel.php
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 = [];
}
35 changes: 35 additions & 0 deletions src/Bases/BaseRequest.php
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());
}
}

0 comments on commit 270f9c8

Please sign in to comment.