Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
38a056e
Add fields and JSON:API support for post and list
binaryk Dec 24, 2019
3574d57
Apply fixes from StyleCI (#64)
Dec 24, 2019
680e75f
Tests coverage
binaryk Dec 24, 2019
b44b612
Merge branch 'field' of https://github.com/binaryk/laravel-restify in…
binaryk Dec 24, 2019
4505ef4
Apply fixes from StyleCI (#66)
Dec 24, 2019
e82c241
Support for customizing response from the repository. Using Laravel r…
binaryk Dec 26, 2019
bb0d2e9
Merge branch 'field' of https://github.com/binaryk/laravel-restify in…
binaryk Dec 26, 2019
fa0585d
Apply fixes from StyleCI (#67)
Dec 26, 2019
8bc2a2a
Update endpoint with validation
binaryk Dec 26, 2019
e388d42
Merge branch 'field' of https://github.com/binaryk/laravel-restify in…
binaryk Dec 26, 2019
affd6e5
Apply fixes from StyleCI (#68)
Dec 26, 2019
3d7372c
Destroy endpoint
binaryk Dec 26, 2019
6d6f08f
Merge branch 'field' of https://github.com/binaryk/laravel-restify in…
binaryk Dec 26, 2019
a03c0e6
Apply fixes from StyleCI (#69)
Dec 26, 2019
41a1c0a
Unit tests
binaryk Jan 3, 2020
d5fdc90
Moving show method to the Crudable trait
binaryk Jan 6, 2020
08ea2c2
Merge branch 'field' of https://github.com/binaryk/laravel-restify in…
binaryk Jan 6, 2020
6bedcd5
Apply fixes from StyleCI (#70)
Jan 6, 2020
ed3845f
Renaming fire to make
binaryk Jan 6, 2020
3d5ead7
Apply fixes from StyleCI (#71)
Jan 6, 2020
a5546a2
Move CRUD methods to the repository
binaryk Jan 6, 2020
316d339
Merge branch 'field' of https://github.com/binaryk/laravel-restify in…
binaryk Jan 6, 2020
5aac969
Apply fixes from StyleCI (#72)
Jan 6, 2020
75d993e
Move index method to the repository
binaryk Jan 6, 2020
994132a
Merge branch 'field' of https://github.com/binaryk/laravel-restify in…
binaryk Jan 6, 2020
6afd2c2
Integration test
binaryk Jan 6, 2020
5b5705f
Stub improvements
binaryk Jan 6, 2020
eafffc5
Clean up
binaryk Jan 6, 2020
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
4 changes: 4 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<?php

Route::get('/{repository}', 'RepositoryIndexController@handle');
Route::post('/{repository}', 'RepositoryStoreController@handle');
Route::get('/{repository}/{repositoryId}', 'RepositoryShowController@handle');
Route::patch('/{repository}/{repositoryId}', 'RepositoryUpdateController@handle');
Route::delete('/{repository}/{repositoryId}', 'RepositoryDestroyController@handle');
12 changes: 6 additions & 6 deletions src/Commands/CheckPassport.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public function handle()
{
$userClass = $this->config->get('auth.providers.users.model');

if (false === $this->provider()) {
if (false === $this->hasProvider()) {
return;
}

if (false === $this->passportable($userClass)) {
if (false === $this->isPassportable($userClass)) {
return;
}

if (false === $this->passportClient()) {
if (false === $this->hasPassportClient()) {
return;
}

Expand All @@ -87,7 +87,7 @@ public function handle()
* @param $userClass
* @return bool
*/
public function passportable($userClass = null): bool
public function isPassportable($userClass = null): bool
{
try {
$userInstance = $this->container->get($userClass);
Expand Down Expand Up @@ -116,7 +116,7 @@ public function passportable($userClass = null): bool
/**
* @return bool
*/
public function provider(): bool
public function hasProvider(): bool
{
$provider = $this->app->getProviders('Laravel\\Passport\\PassportServiceProvider');

Expand All @@ -132,7 +132,7 @@ public function provider(): bool
/**
* @return bool
*/
public function passportClient(): bool
public function hasPassportClient(): bool
{
try {
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/stubs/RestifyServiceProvider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Binaryk\LaravelRestify\Restify;
use Illuminate\Http\Resources\Json\Resource;
use Binaryk\LaravelRestify\RestifyApplicationServiceProvider;

class RestifyServiceProvider extends RestifyApplicationServiceProvider
Expand All @@ -16,6 +17,8 @@ class RestifyServiceProvider extends RestifyApplicationServiceProvider
public function boot()
{
parent::boot();

Resource::withoutWrapping();
}

/**
Expand Down
67 changes: 66 additions & 1 deletion src/Commands/stubs/repository.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace DummyNamespace;

use Illuminate\Http\Request;
use Binaryk\LaravelRestify\Fields\Field;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Binaryk\LaravelRestify\Repositories\Repository;
use Illuminate\Contracts\Pagination\Paginator;

class DummyClass extends Repository
{
Expand All @@ -13,4 +15,67 @@ class DummyClass extends Repository
* @var string
*/
public static $model = 'DummyFullModel';

/**
* @param RestifyRequest $request
* @return array
*/
public function fields(RestifyRequest $request)
{
return [
// Field::make('title')->storingRules('required')->messages([
// 'required' => 'This field is required bro.',
// ]),
];

}

/**
* @param RestifyRequest $request
* @param Paginator $paginated
* @return \Illuminate\Http\JsonResponse
*/
public function index(RestifyRequest $request, Paginator $paginated)
{
return parent::index($request, $paginated);
}

/**
* @param RestifyRequest $request
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Throwable
*/
public function show(RestifyRequest $request)
{
return parent::show($request);
}

/**
* @param RestifyRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(RestifyRequest $request)
{
return parent::store($request);
}

/**
* @param RestifyRequest $request
* @param $model
* @return \Illuminate\Http\JsonResponse|void
*/
public function update(RestifyRequest $request, $model)
{
return parent::update($request, $model);
}

/**
* @param RestifyRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(RestifyRequest $request)
{
return parent::destroy($request);
}
}
9 changes: 0 additions & 9 deletions src/Contracts/RestifySearchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Binaryk\LaravelRestify\Contracts;

use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;

/**
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
Expand All @@ -15,13 +13,6 @@ interface RestifySearchable
const MATCH_BOOL = 'bool';
const MATCH_INTEGER = 'integer';

/**
* @param RestifyRequest $request
* @param array $fields
* @return array
*/
public function serializeForIndex(RestifyRequest $request, array $fields = []);

/**
* @return array
*/
Expand Down
51 changes: 34 additions & 17 deletions src/Controllers/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Binaryk\LaravelRestify\Repositories\Repository;
use Binaryk\LaravelRestify\Services\Search\SearchService;
use Binaryk\LaravelRestify\Traits\PerformsQueries;
use Illuminate\Config\Repository as Config;
use Illuminate\Container\Container;
use Illuminate\Contracts\Auth\Access\Gate;
Expand All @@ -22,7 +21,9 @@
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Password;
use Throwable;

/**
* Abstract Class RestController.
Expand All @@ -35,7 +36,7 @@
*/
abstract class RestController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests, PerformsQueries;
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

/**
* @var RestResponse
Expand Down Expand Up @@ -132,30 +133,46 @@ protected function response($data = null, $status = 200, array $headers = [])
* @return array
* @throws BindingResolutionException
* @throws InstanceOfException
* @throws Throwable
*/
public function search($modelClass, $filters = [])
{
$results = SearchService::instance()
->setPredefinedFilters($filters)
->search($this->request(), $modelClass instanceof Repository ? $modelClass->model() : new $modelClass);
$paginator = $this->paginator($modelClass, $filters);

$results->tap(function ($query) {
static::indexQuery($this->request(), $query);
});

/**
* @var \Illuminate\Pagination\Paginator
*/
$paginator = $results->paginate($this->request()->get('perPage') ?? ($modelClass::$defaultPerPage ?? RestifySearchable::DEFAULT_PER_PAGE));
if ($modelClass instanceof Repository) {
$items = $paginator->getCollection()->mapInto(get_class($modelClass))->map->serializeForIndex($this->request());
$items = $paginator->getCollection()->mapInto(get_class($modelClass))->map->toArray($this->request());
} else {
$items = $paginator->getCollection()->map->serializeForIndex($this->request());
$items = $paginator->getCollection();
}

return array_merge($paginator->toArray(), [
return [
'meta' => Arr::except($paginator->toArray(), ['data', 'next_page_url', 'last_page_url', 'first_page_url', 'prev_page_url', 'path']),
'links' => Arr::only($paginator->toArray(), ['next_page_url', 'last_page_url', 'first_page_url', 'prev_page_url', 'path']),
'data' => $items,
]);
];
}

/**
* @param $modelClass
* @param array $filters
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
* @throws BindingResolutionException
* @throws InstanceOfException
* @throws Throwable
*/
public function paginator($modelClass, $filters = [])
{
$results = SearchService::instance()
->setPredefinedFilters($filters)
->search($this->request(), $modelClass instanceof Repository ? $modelClass->model() : new $modelClass);

$results->tap(function ($query) use ($modelClass) {
if ($modelClass instanceof Repository) {
$modelClass::indexQuery($this->request(), $query);
}
});

return $results->paginate($this->request()->get('perPage') ?? ($modelClass::$defaultPerPage ?? RestifySearchable::DEFAULT_PER_PAGE));
}

/**
Expand Down
Loading