Merge pull request #1579 from CachetHQ/basic-api-searching

Implement very basic API searching
This commit is contained in:
James Brooks 2016-03-08 20:20:52 +00:00
commit f9e332cac7
7 changed files with 93 additions and 3 deletions

View File

@ -37,6 +37,8 @@ class ComponentController extends AbstractApiController
$components = Component::enabled();
}
$components->search(Binput::except(['sort', 'order', 'per_page']));
if ($sortBy = Binput::get('sort')) {
$direction = Binput::has('order') && Binput::get('order') == 'desc';

View File

@ -31,6 +31,8 @@ class ComponentGroupController extends AbstractApiController
{
$groups = ComponentGroup::whereRaw('1=1');
$groups->search(Binput::except(['sort', 'order', 'per_page']));
if ($sortBy = Binput::get('sort')) {
$direction = Binput::has('order') && Binput::get('order') == 'desc';

View File

@ -34,6 +34,8 @@ class IncidentController extends AbstractApiController
$incidents = Incident::where('visible', '>=', $incidentVisibility);
$incidents->search(Binput::except(['sort', 'order', 'per_page']));
if ($sortBy = Binput::get('sort')) {
$direction = Binput::has('order') && Binput::get('order') == 'desc';

View File

@ -12,6 +12,7 @@
namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
use CachetHQ\Cachet\Models\Traits\SearchableTrait;
use CachetHQ\Cachet\Models\Traits\SortableTrait;
use CachetHQ\Cachet\Presenters\ComponentPresenter;
use Illuminate\Database\Eloquent\Builder;
@ -21,7 +22,7 @@ use McCool\LaravelAutoPresenter\HasPresenter;
class Component extends Model implements HasPresenter
{
use SoftDeletes, SortableTrait, ValidatingTrait;
use SearchableTrait, SoftDeletes, SortableTrait, ValidatingTrait;
/**
* List of attributes that have default values.
@ -77,6 +78,20 @@ class Component extends Model implements HasPresenter
'link' => 'url',
];
/**
* The searchable fields.
*
* @var string[]
*/
protected $searchable = [
'id',
'name',
'status',
'order',
'group_id',
'enabled',
];
/**
* The sortable fields.
*

View File

@ -12,6 +12,7 @@
namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
use CachetHQ\Cachet\Models\Traits\SearchableTrait;
use CachetHQ\Cachet\Models\Traits\SortableTrait;
use CachetHQ\Cachet\Presenters\ComponentGroupPresenter;
use Illuminate\Database\Eloquent\Model;
@ -19,7 +20,7 @@ use McCool\LaravelAutoPresenter\HasPresenter;
class ComponentGroup extends Model implements HasPresenter
{
use SortableTrait, ValidatingTrait;
use SearchableTrait, SortableTrait, ValidatingTrait;
/**
* The attributes that should be casted to native types.
@ -50,6 +51,18 @@ class ComponentGroup extends Model implements HasPresenter
'collapsed' => 'bool',
];
/**
* The searchable fields.
*
* @var string[]
*/
protected $searchable = [
'id',
'name',
'order',
'collapsed',
];
/**
* The sortable fields.
*

View File

@ -12,6 +12,7 @@
namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
use CachetHQ\Cachet\Models\Traits\SearchableTrait;
use CachetHQ\Cachet\Models\Traits\SortableTrait;
use CachetHQ\Cachet\Presenters\IncidentPresenter;
use Carbon\Carbon;
@ -21,7 +22,7 @@ use McCool\LaravelAutoPresenter\HasPresenter;
class Incident extends Model implements HasPresenter
{
use SoftDeletes, SortableTrait, ValidatingTrait;
use SearchableTrait, SoftDeletes, SortableTrait, ValidatingTrait;
/**
* The attributes that should be casted to native types.
@ -63,6 +64,18 @@ class Incident extends Model implements HasPresenter
'message' => 'required',
];
/**
* The searchable fields.
*
* @var string[]
*/
protected $searchable = [
'id',
'name',
'status',
'visible',
];
/**
* The sortable fields.
*

View File

@ -0,0 +1,43 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Models\Traits;
use Illuminate\Database\Eloquent\Builder;
/**
* This is the searchable trait.
*
* @author James Brooks <james@alt-three.com>
*/
trait SearchableTrait
{
/**
* Adds a sort scope.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $column
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeSearch(Builder $query, array $search = [])
{
if (empty($search)) {
return $query;
}
if (!array_intersect(array_keys($search), $this->searchable)) {
return $query;
}
return $query->where($search);
}
}