Implement very basic API searching. Closes #1348
This commit is contained in:
committed by
James Brooks
parent
9379ab131c
commit
0b3483fb8a
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
+14
-1
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user