Skip to content

Commit

Permalink
Merge pull request #52 from binaryk/search-coverage
Browse files Browse the repository at this point in the history
Search coverage
  • Loading branch information
Lupacescu Eduard authored Dec 23, 2019
2 parents 867974f + be8748d commit 007bcea
Show file tree
Hide file tree
Showing 21 changed files with 748 additions and 312 deletions.
29 changes: 0 additions & 29 deletions src/Commands/stubs/repository.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,4 @@ class DummyClass extends Repository
* @var string
*/
public static $model = 'DummyFullModel';

/**
* The single value that should be used to represent the repository when being displayed.
*
* @var string
*/
public static $title = 'id';

/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id',
];

/**
* Get the fields displayed by the repository.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make()->sortable(),
];
}
}
6 changes: 3 additions & 3 deletions src/Contracts/RestifySearchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Binaryk\LaravelRestify\Contracts;

use Illuminate\Http\Request;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;

/**
* @author Eduard Lupacescu <[email protected]>
Expand All @@ -16,11 +16,11 @@ interface RestifySearchable
const MATCH_INTEGER = 'integer';

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

/**
* @return array
Expand Down
20 changes: 15 additions & 5 deletions src/Controllers/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
use Binaryk\LaravelRestify\Exceptions\Guard\EntityNotFoundException;
use Binaryk\LaravelRestify\Exceptions\Guard\GatePolicy;
use Binaryk\LaravelRestify\Exceptions\InstanceOfException;
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;
use Illuminate\Config\Repository as Config;
use Illuminate\Container\Container;
use Illuminate\Contracts\Auth\Access\Gate;
Expand Down Expand Up @@ -84,8 +85,8 @@ public function config()
{
$container = Container::getInstance();

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

return $this->config;
Expand Down Expand Up @@ -130,18 +131,27 @@ protected function response($data = null, $status = 200, array $headers = [])
* @param array $filters
* @return array
* @throws BindingResolutionException
* @throws InstanceOfException
*/
public function search($modelClass, $filters = [])
{
$results = SearchService::instance()
->setPredefinedFilters($filters)
->search($this->request(), new $modelClass);
->search($this->request(), $modelClass instanceof Repository ? $modelClass->model() : new $modelClass);

$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));
$items = $paginator->getCollection()->map->serializeForIndex($this->request());
if ($modelClass instanceof Repository) {
$items = $paginator->getCollection()->mapInto(get_class($modelClass))->map->serializeForIndex($this->request());
} else {
$items = $paginator->getCollection()->map->serializeForIndex($this->request());
}

return array_merge($paginator->toArray(), [
'data' => $items,
Expand Down
12 changes: 12 additions & 0 deletions src/Exceptions/InstanceOfException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Binaryk\LaravelRestify\Exceptions;

use Exception;

/**
* @author Eduard Lupacescu <[email protected]>
*/
class InstanceOfException extends Exception
{
}
4 changes: 1 addition & 3 deletions src/Http/Controllers/RepositoryIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class RepositoryIndexController extends RepositoryController
*/
public function handle(RestifyRequest $request)
{
$resource = $request->repository();

$data = $this->search($resource::newModel());
$data = $this->search($request->newRepository());

return $this->respond($data);
}
Expand Down
40 changes: 40 additions & 0 deletions src/Http/Requests/InteractWithRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,44 @@ public function rules()
//
];
}

/**
* Get the route handling the request.
*
* @param string|null $param
* @param mixed $default
* @return \Illuminate\Routing\Route|object|string
*/
abstract public function route($param = null, $default = null);

/**
* Get a new instance of the repository being requested.
*
* @return Repository
* @throws EntityNotFoundException
* @throws UnauthorizedException
*/
public function newRepository()
{
$repository = $this->repository();

return new $repository($repository::newModel());
}

/**
* Check if the route is resolved by the Repository class, or it uses the classical Models.
* @return bool
*/
public function isResolvedByRestify()
{
try {
$this->repository();

return true;
} catch (EntityNotFoundException $e) {
return false;
} catch (UnauthorizedException $e) {
return true;
}
}
}
39 changes: 22 additions & 17 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,45 @@

namespace Binaryk\LaravelRestify\Repositories;

use Binaryk\LaravelRestify\Traits\AuthorizableModels;
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
use Binaryk\LaravelRestify\Traits\InteractWithSearch;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\DelegatesToResource;
use Illuminate\Support\Str;

/**
* @author Eduard Lupacescu <[email protected]>
*/
abstract class Repository
abstract class Repository implements RestifySearchable
{
use AuthorizableModels;
use InteractWithSearch,
DelegatesToResource;

/**
* This is named `resource` because of the forwarding properties from DelegatesToResource trait.
* @var Model
*/
public $modelInstance;
public $resource;

/**
* Create a new resource instance.
*
* @param \Illuminate\Database\Eloquent\Model $resource
* @param \Illuminate\Database\Eloquent\Model $model
*/
public function __construct($resource)
public function __construct($model)
{
$this->modelInstance = $resource;
$this->resource = $model;
}

/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
abstract public function fields(Request $request);

/**
* Get the underlying model instance for the resource.
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function model()
{
return $this->modelInstance;
return $this->resource;
}

/**
Expand Down Expand Up @@ -77,4 +72,14 @@ public static function query()
{
return static::newModel()->query();
}

/**
* @return array
*/
public function toArray()
{
$model = $this->model();

return $model->toArray();
}
}
Loading

0 comments on commit 007bcea

Please sign in to comment.