Fixes API sorting and filtering. Closes #1489
This commit is contained in:
committed by
James Brooks
parent
98550c31c9
commit
919c7127e7
@@ -12,6 +12,7 @@
|
||||
namespace CachetHQ\Cachet\Models;
|
||||
|
||||
use AltThree\Validator\ValidatingTrait;
|
||||
use CachetHQ\Cachet\Models\Traits\SortableTrait;
|
||||
use CachetHQ\Cachet\Presenters\ComponentPresenter;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -20,7 +21,7 @@ use McCool\LaravelAutoPresenter\HasPresenter;
|
||||
|
||||
class Component extends Model implements HasPresenter
|
||||
{
|
||||
use SoftDeletes, ValidatingTrait;
|
||||
use SoftDeletes, SortableTrait, ValidatingTrait;
|
||||
|
||||
/**
|
||||
* List of attributes that have default values.
|
||||
@@ -76,6 +77,20 @@ class Component extends Model implements HasPresenter
|
||||
'link' => 'url',
|
||||
];
|
||||
|
||||
/**
|
||||
* The sortable fields.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $sortable = [
|
||||
'id',
|
||||
'name',
|
||||
'status',
|
||||
'order',
|
||||
'group_id',
|
||||
'enabled',
|
||||
];
|
||||
|
||||
/**
|
||||
* Components can belong to a group.
|
||||
*
|
||||
|
||||
@@ -12,13 +12,14 @@
|
||||
namespace CachetHQ\Cachet\Models;
|
||||
|
||||
use AltThree\Validator\ValidatingTrait;
|
||||
use CachetHQ\Cachet\Models\Traits\SortableTrait;
|
||||
use CachetHQ\Cachet\Presenters\ComponentGroupPresenter;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use McCool\LaravelAutoPresenter\HasPresenter;
|
||||
|
||||
class ComponentGroup extends Model implements HasPresenter
|
||||
{
|
||||
use ValidatingTrait;
|
||||
use SortableTrait, ValidatingTrait;
|
||||
|
||||
/**
|
||||
* The attributes that should be casted to native types.
|
||||
@@ -49,6 +50,18 @@ class ComponentGroup extends Model implements HasPresenter
|
||||
'collapsed' => 'bool',
|
||||
];
|
||||
|
||||
/**
|
||||
* The sortable fields.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $sortable = [
|
||||
'id',
|
||||
'name',
|
||||
'order',
|
||||
'collapsed',
|
||||
];
|
||||
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
namespace CachetHQ\Cachet\Models;
|
||||
|
||||
use AltThree\Validator\ValidatingTrait;
|
||||
use CachetHQ\Cachet\Models\Traits\SortableTrait;
|
||||
use CachetHQ\Cachet\Presenters\IncidentPresenter;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -20,7 +21,7 @@ use McCool\LaravelAutoPresenter\HasPresenter;
|
||||
|
||||
class Incident extends Model implements HasPresenter
|
||||
{
|
||||
use SoftDeletes, ValidatingTrait;
|
||||
use SoftDeletes, SortableTrait, ValidatingTrait;
|
||||
|
||||
/**
|
||||
* The attributes that should be casted to native types.
|
||||
@@ -62,6 +63,19 @@ class Incident extends Model implements HasPresenter
|
||||
'message' => 'required',
|
||||
];
|
||||
|
||||
/**
|
||||
* The sortable fields.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $sortable = [
|
||||
'id',
|
||||
'name',
|
||||
'status',
|
||||
'visible',
|
||||
'message',
|
||||
];
|
||||
|
||||
/**
|
||||
* Finds all visible incidents.
|
||||
*
|
||||
|
||||
@@ -12,13 +12,14 @@
|
||||
namespace CachetHQ\Cachet\Models;
|
||||
|
||||
use AltThree\Validator\ValidatingTrait;
|
||||
use CachetHQ\Cachet\Models\Traits\SortableTrait;
|
||||
use CachetHQ\Cachet\Presenters\MetricPresenter;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use McCool\LaravelAutoPresenter\HasPresenter;
|
||||
|
||||
class Metric extends Model implements HasPresenter
|
||||
{
|
||||
use ValidatingTrait;
|
||||
use SortableTrait, ValidatingTrait;
|
||||
|
||||
/**
|
||||
* The calculation type of sum.
|
||||
@@ -92,6 +93,19 @@ class Metric extends Model implements HasPresenter
|
||||
'default_view' => 'numeric|between:0,3',
|
||||
];
|
||||
|
||||
/**
|
||||
* The sortable fields.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $sortable = [
|
||||
'id',
|
||||
'name',
|
||||
'display_chart',
|
||||
'default_value',
|
||||
'calc_type',
|
||||
];
|
||||
|
||||
/**
|
||||
* Metrics contain many metric points.
|
||||
*
|
||||
|
||||
40
app/Models/Traits/SortableTrait.php
Normal file
40
app/Models/Traits/SortableTrait.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?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 sortable trait.
|
||||
*
|
||||
* @author James Brooks <james@alt-three.com>
|
||||
*/
|
||||
trait SortableTrait
|
||||
{
|
||||
/**
|
||||
* Adds a sort scope.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $column
|
||||
* @param string $direction
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeSort(Builder $query, $column, $direction)
|
||||
{
|
||||
if (!in_array($column, $this->sortable)) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
return $query->orderBy($column, $direction);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user