mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-09 23:24:06 -08:00
Refactor category API and transformer for query optimization
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
17c308f3ca
commit
249b188654
|
@ -22,6 +22,24 @@ class CategoriesTransformer
|
|||
|
||||
public function transformCategory(Category $category = null)
|
||||
{
|
||||
|
||||
switch ($category->category_type) {
|
||||
case 'asset':
|
||||
$category->item_count = $category->assets_count;
|
||||
break;
|
||||
case 'accessory':
|
||||
$category->item_count = $category->accessories_count;
|
||||
break;
|
||||
case 'consumable':
|
||||
$category->item_count = $category->consumables_count;
|
||||
break;
|
||||
case 'component':
|
||||
$category->item_count = $category->components_count;
|
||||
break;
|
||||
default:
|
||||
$category->item_count = 0;
|
||||
}
|
||||
|
||||
if ($category) {
|
||||
$array = [
|
||||
'id' => (int) $category->id,
|
||||
|
@ -33,7 +51,7 @@ class CategoriesTransformer
|
|||
'eula' => ($category->getEula()),
|
||||
'checkin_email' => ($category->checkin_email == '1'),
|
||||
'require_acceptance' => ($category->require_acceptance == '1'),
|
||||
'item_count' => (int) $category->itemCount(),
|
||||
'item_count' => (int) $category->item_count,
|
||||
'assets_count' => (int) $category->assets_count,
|
||||
'accessories_count' => (int) $category->accessories_count,
|
||||
'consumables_count' => (int) $category->consumables_count,
|
||||
|
|
|
@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
|||
use Illuminate\Support\Facades\Gate;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
use App\Helpers\Helper;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Model for Categories. Categories are a higher-level group
|
||||
|
@ -97,8 +98,11 @@ class Category extends SnipeModel
|
|||
*/
|
||||
public function isDeletable()
|
||||
{
|
||||
return Gate::allows('delete', $this)
|
||||
&& ($this->itemCount() == 0);
|
||||
if (Gate::allows('delete', $this)) {
|
||||
$category_type_var = Str::plural($this->category_type).'_count';
|
||||
return $this->{$category_type_var};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -148,31 +152,7 @@ class Category extends SnipeModel
|
|||
{
|
||||
return $this->hasMany(\App\Models\Component::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of items in the category
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v2.0]
|
||||
* @return int
|
||||
*/
|
||||
public function itemCount()
|
||||
{
|
||||
switch ($this->category_type) {
|
||||
case 'asset':
|
||||
return $this->assets()->count();
|
||||
case 'accessory':
|
||||
return $this->accessories()->count();
|
||||
case 'component':
|
||||
return $this->components()->count();
|
||||
case 'consumable':
|
||||
return $this->consumables()->count();
|
||||
case 'license':
|
||||
return $this->licenses()->count();
|
||||
}
|
||||
|
||||
return '0';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Establishes the category -> assets relationship
|
||||
|
|
Loading…
Reference in a new issue