Skip to content

Commit

Permalink
Adding index query and make static filters
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryk committed Dec 22, 2019
1 parent e304026 commit cc6d31e
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 167 deletions.
10 changes: 5 additions & 5 deletions src/Contracts/RestifySearchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public function serializeForIndex(Request $request, array $fields = []);
/**
* @return array
*/
public function getSearchableFields();
public static function getSearchableFields();

/**
* @return array
*/
public function getWiths();
public static function getWiths();

/**
* @return array
*/
public function getInFields();
public static function getInFields();

/**
* Find matches in the table by given value
Expand All @@ -48,10 +48,10 @@ public function getInFields();
* [ 'match' => [ 'id' => 1 ] ]
* @return array
*/
public function getMatchByFields();
public static function getMatchByFields();

/**
* @return array
*/
public function getOrderByFields();
public static function getOrderByFields();
}
45 changes: 22 additions & 23 deletions src/Controllers/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
use Binaryk\LaravelRestify\Exceptions\Guard\EntityNotFoundException;
use Binaryk\LaravelRestify\Exceptions\Guard\GatePolicy;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Binaryk\LaravelRestify\Services\Search\SearchService;
use Binaryk\LaravelRestify\Traits\PerformsQueries;
use Illuminate\Config\Repository;
use Illuminate\Config\Repository as Config;
use Illuminate\Container\Container;
Expand All @@ -14,13 +16,10 @@
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Password;

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

/**
* @var RestResponse
Expand All @@ -48,7 +47,7 @@ abstract class RestController extends BaseController
protected $gate;

/**
* @var Request
* @var RestifyRequest
*/
protected $request;

Expand All @@ -63,13 +62,15 @@ abstract class RestController extends BaseController
protected $guard;

/**
* @return Request
* @return RestifyRequest
* @throws BindingResolutionException
*/
public function request()
{
if (($this->request instanceof Request) === false) {
$this->request = app()->make(Request::class);
$container = Container::getInstance();

if (($this->request instanceof RestifyRequest) === false) {
$this->request = $container->make(RestifyRequest::class);
}

return $this->request;
Expand All @@ -81,8 +82,10 @@ public function request()
*/
public function config()
{
$container = Container::getInstance();

if (($this->config instanceof Repository) === false) {
$this->config = app()->make(Repository::class);
$this->config = $container->make(Repository::class);
}

return $this->config;
Expand All @@ -95,6 +98,7 @@ public function config()
* @param int $httpCode
*
* @return JsonResponse
* @throws BindingResolutionException
*/
protected function respond($data = null, $httpCode = 200)
{
Expand Down Expand Up @@ -130,30 +134,22 @@ protected function response($data = null, $status = 200, array $headers = [])
*/
public function search($modelClass, $filters = [])
{
$container = Container::getInstance();

/** * @var SearchService $searchService */
$searchService = $container->make(SearchService::class);
$results = $searchService
$results = SearchService::instance()
->setPredefinedFilters($filters)
->search($this->request(), ($modelClass instanceof Model ? $modelClass : $container->make($modelClass)));
->search($this->request(), new $modelClass);
$results->tap(function ($query) {
static::indexQuery($this->request(), $query);
});

$paginator = $results->paginate($this->request()->get('perPage') ?? ($modelClass::$defaultPerPage ?? RestifySearchable::DEFAULT_PER_PAGE));
$items = $paginator->getCollection()->map->serializeForIndex($this->request());


return array_merge($paginator->toArray(), [
'data' => $items,
]);
}

public function index(Request $request, $model = null)
{
$data = $this->paginator($model)->getCollection()->map->serializeForIndex($this->request());

return $this->respond($data);
}


/**
* @param $policy
* @param $objects
Expand Down Expand Up @@ -210,6 +206,7 @@ public function broker()
* Returns with a message.
* @param $msg
* @return JsonResponse
* @throws BindingResolutionException
*/
public function message($msg)
{
Expand All @@ -221,7 +218,9 @@ public function message($msg)
/**
* Returns with a list of errors.
*
* @param array $errors
* @return JsonResponse
* @throws BindingResolutionException
*/
protected function errors(array $errors)
{
Expand Down
13 changes: 9 additions & 4 deletions src/Http/Controllers/RepositoryIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
*/
class RepositoryIndexController extends RepositoryController
{
/**
* @param RestifyRequest $request
* @return \Illuminate\Http\JsonResponse
* @throws \Binaryk\LaravelRestify\Exceptions\Eloquent\EntityNotFoundException
* @throws \Binaryk\LaravelRestify\Exceptions\UnauthorizedException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function handle(RestifyRequest $request)
{
$resource = $request->repository();

$paginator = $resource::query()
->where('id', '>', 10)
->simplePaginate();
$data = $this->search($resource::newModel());

return $this->respond($paginator);
return $this->respond($data);
}
}
67 changes: 67 additions & 0 deletions src/Http/Requests/InteractWithRepositories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Binaryk\LaravelRestify\Http\Requests;

use Binaryk\LaravelRestify\Exceptions\Eloquent\EntityNotFoundException;
use Binaryk\LaravelRestify\Exceptions\UnauthorizedException;
use Binaryk\LaravelRestify\Repositories\Repository;
use Binaryk\LaravelRestify\Restify;
use Illuminate\Database\Eloquent\Model;

/**
* @package Binaryk\LaravelRestify\Http\Requests;
* @author Eduard Lupacescu <[email protected]>
*/
trait InteractWithRepositories
{
/**
* @var Model
*/
public $model;

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the class name of the repository being requested.
*
* @return Repository
* @throws EntityNotFoundException
* @throws UnauthorizedException
*/
public function repository()
{
return tap(Restify::repositoryForKey($this->route('repository')), function ($repository) {
if (is_null($repository)) {
throw new EntityNotFoundException(__('Repository :name not found.', [
'name' => $repository,
]), 404);
}

if ( ! $repository::authorizedToViewAny($this)) {
throw new UnauthorizedException(__('Unauthorized to view repository :name.', [
'name' => $repository,
]), 403);
}
});
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}
37 changes: 1 addition & 36 deletions src/Http/Requests/RestifyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,13 @@

namespace Binaryk\LaravelRestify\Http\Requests;

use Binaryk\LaravelRestify\Exceptions\Eloquent\EntityNotFoundException;
use Binaryk\LaravelRestify\Exceptions\UnauthorizedException;
use Binaryk\LaravelRestify\Repositories\Repository;
use Binaryk\LaravelRestify\Restify;
use Illuminate\Foundation\Http\FormRequest;

/**
* @author Eduard Lupacescu <[email protected]>
*/
class RestifyRequest extends FormRequest
{
/**
* Get the class name of the repository being requested.
*
* @return Repository
*/
public function repository()
{
return tap(Restify::repositoryForKey($this->route('repository')), function ($repository) {
if (is_null($repository)) {
throw new EntityNotFoundException(__('Repository :name not found.', [
'name' => $repository,
]), 404);
}
use InteractWithRepositories;

if (! $repository::authorizedToViewAny($this)) {
throw new UnauthorizedException(__('Unauthorized to view repository :name.', [
'name' => $repository,
]), 403);
}
});
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}
6 changes: 3 additions & 3 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class Repository
/**
* @var Model
*/
public $resource;
public $modelInstance;

/**
* Create a new resource instance.
Expand All @@ -27,7 +27,7 @@ abstract class Repository
*/
public function __construct($resource)
{
$this->resource = $resource;
$this->modelInstance = $resource;
}

/**
Expand All @@ -45,7 +45,7 @@ abstract public function fields(Request $request);
*/
public function model()
{
return $this->resource;
return $this->modelInstance;
}

/**
Expand Down
Loading

0 comments on commit cc6d31e

Please sign in to comment.