mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-24 21:24:13 -08:00
Adds basic GET api support for CustomFieldsets (#3697)
* Adds basic GET api support for CustomFieldsets Currently there is not support for getting what fields a given fieldset contains from the API. This commit creates a new API Controller for CustomFieldsets as well as Transformers for CustomFields CustomFieldsets. Additionally, the api route has been updated so that a show method can be access from http://myapp/api/v1/fieldsets/{id} * CustomFieldsetsTransformer only returns id and name of model * Added index api method for CustomFieldsets * Removes copy/paste error in CustomFieldsetController (including search) * Added id to CustomFieldsetsTransformers * Adds custom_fieldset_id as a field when storing and updating AssetModels * Removed uncessesary parameter from CustomFieldsetsController.index * Cleaned up CustomFieldset API
This commit is contained in:
parent
f432f98e12
commit
3cfcc43efa
|
@ -74,6 +74,7 @@ class AssetModelsController extends Controller
|
|||
$this->authorize('create', AssetModel::class);
|
||||
$assetmodel = new AssetModel;
|
||||
$assetmodel->fill($request->all());
|
||||
//$assetmodel->fieldset_id = $request->get("custom_fieldset_id");
|
||||
|
||||
if ($assetmodel->save()) {
|
||||
return response()->json(Helper::formatStandardApiResponse('success', $assetmodel, trans('admin/assetmodels/message.create.success')));
|
||||
|
@ -127,6 +128,7 @@ class AssetModelsController extends Controller
|
|||
$this->authorize('edit', AssetModel::class);
|
||||
$assetmodel = AssetModel::findOrFail($id);
|
||||
$assetmodel->fill($request->all());
|
||||
$assetmodel->fieldset_id = $request->get("custom_fieldset_id");
|
||||
|
||||
if ($assetmodel->save()) {
|
||||
return response()->json(Helper::formatStandardApiResponse('success', $assetmodel, trans('admin/assetmodels/message.update.success')));
|
||||
|
|
72
app/Http/Controllers/Api/CustomFieldsetsController.php
Normal file
72
app/Http/Controllers/Api/CustomFieldsetsController.php
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use View;
|
||||
use App\Models\CustomFieldset;
|
||||
use App\Models\CustomField;
|
||||
use Input;
|
||||
use Validator;
|
||||
use Redirect;
|
||||
use App\Models\AssetModel;
|
||||
use Lang;
|
||||
use Auth;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Helpers\Helper;
|
||||
use App\Http\Transformers\CustomFieldsTransformer;
|
||||
use App\Http\Transformers\CustomFieldsetsTransformer;
|
||||
use App\Http\Requests\AssetRequest;
|
||||
|
||||
/**
|
||||
* This controller handles all actions related to Custom Asset Fieldsets for
|
||||
* the Snipe-IT Asset Management application.
|
||||
*
|
||||
* @todo Improve documentation here.
|
||||
* @todo Check for raw DB queries and try to convert them to query builder statements
|
||||
* @version v2.0
|
||||
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
||||
* @author [Josh Gibson]
|
||||
*/
|
||||
|
||||
class CustomFieldsetsController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* Shows the given fieldset and its fields
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @author [Josh Gibson]
|
||||
* @param int $id
|
||||
* @since [v1.8]
|
||||
* @return View
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->authorize('index', CustomFieldset::class);
|
||||
$fieldsets = CustomFieldset::withCount(['fields', 'models'])->get();
|
||||
|
||||
$total = count($fieldsets);
|
||||
return (new CustomFieldsetsTransformer)->transformCustomFieldsets($fieldsets, $total);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the given fieldset and its fields
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @author [Josh Gibson]
|
||||
* @param int $id
|
||||
* @since [v1.8]
|
||||
* @return View
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$this->authorize('show', CustomFieldset::class);
|
||||
if ($fieldset = CustomFieldset::find($id)) {
|
||||
return (new CustomFieldsetsTransformer)->transformCustomFieldset($fieldset);
|
||||
}
|
||||
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/custom_fields/message.fieldset.does_not_exist')), 200);
|
||||
|
||||
}
|
||||
|
||||
}
|
30
app/Http/Transformers/CustomFieldsTransformer.php
Normal file
30
app/Http/Transformers/CustomFieldsTransformer.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
namespace App\Http\Transformers;
|
||||
|
||||
use App\Models\CustomField;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use App\Helpers\Helper;
|
||||
use Gate;
|
||||
|
||||
class CustomFieldsTransformer
|
||||
{
|
||||
|
||||
public function transformCustomFields (Collection $fields, $total)
|
||||
{
|
||||
$array = array();
|
||||
foreach ($fields as $field) {
|
||||
$array[] = self::transformCustomField($field);
|
||||
}
|
||||
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
||||
}
|
||||
|
||||
public function transformCustomField (CustomField $field)
|
||||
{
|
||||
$array = [
|
||||
'name' => $field->name,
|
||||
'db_column_name' => $field->db_column_name(),
|
||||
'format' => $field->format
|
||||
];
|
||||
return $array;
|
||||
}
|
||||
}
|
43
app/Http/Transformers/CustomFieldsetsTransformer.php
Normal file
43
app/Http/Transformers/CustomFieldsetsTransformer.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
namespace App\Http\Transformers;
|
||||
|
||||
use App\Models\CustomFieldset;
|
||||
use App\Models\CustomField;
|
||||
use App\Models\AssetModel;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use App\Helpers\Helper;
|
||||
use Gate;
|
||||
|
||||
class CustomFieldsetsTransformer
|
||||
{
|
||||
|
||||
public function transformCustomFieldsets (Collection $fieldsets, $total)
|
||||
{
|
||||
$array = array();
|
||||
foreach ($fieldsets as $fieldset) {
|
||||
$array[] = self::transformCustomFieldset($fieldset);
|
||||
}
|
||||
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
||||
}
|
||||
|
||||
public function transformCustomFieldset (CustomFieldset $fieldset)
|
||||
{
|
||||
$fields = $fieldset->fields;
|
||||
$models = $fieldset->models;
|
||||
$modelsArray = array();
|
||||
foreach ($models as $model)
|
||||
{
|
||||
$modelsArray[] = [
|
||||
'id' => $model->id,
|
||||
'name' => $model->name
|
||||
];
|
||||
}
|
||||
$array = [
|
||||
'id' => $fieldset->id,
|
||||
'name' => $fieldset->name,
|
||||
'fields' => (new CustomFieldsTransformer)->transformCustomFields($fields, $fieldset->fields_count),
|
||||
'models' => (new DatatablesTransformer)->transformDatatables($modelsArray, $fieldset->models_count)
|
||||
];
|
||||
return $array;
|
||||
}
|
||||
}
|
|
@ -55,7 +55,7 @@ class AssetModel extends SnipeModel
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['name','manufacturer_id','category_id','eol', 'user_id'];
|
||||
protected $fillable = ['name','manufacturer_id','category_id','eol', 'user_id', 'fieldset_id'];
|
||||
|
||||
public function assets()
|
||||
{
|
||||
|
|
|
@ -28,7 +28,7 @@ return array(
|
|||
|
||||
'fieldset' => array(
|
||||
|
||||
|
||||
'does_not_exist' => 'Fieldset does not exist',
|
||||
|
||||
'create' => array(
|
||||
'error' => 'Fieldset was not created, please try again.',
|
||||
|
|
|
@ -173,6 +173,22 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
|
|||
}); // Fields group
|
||||
|
||||
|
||||
/*--- Fieldsets API ---*/
|
||||
|
||||
Route::group(['prefix' => 'fieldsets'], function () {
|
||||
Route::get('',
|
||||
[
|
||||
'as' => 'api.customfieldset.index',
|
||||
'uses' => 'CustomFieldsetsController@index'
|
||||
]);
|
||||
Route::get('{id}',
|
||||
[
|
||||
'as' => 'api.customfieldsets.show',
|
||||
'uses' => 'CustomFieldsetsController@show'
|
||||
]);
|
||||
}); // Fieldsets group
|
||||
|
||||
|
||||
/*--- Groups API ---*/
|
||||
|
||||
Route::resource('groups', 'GroupsController',
|
||||
|
|
Loading…
Reference in a new issue