Basic components API

This commit is contained in:
snipe 2017-01-26 19:16:06 -08:00
parent 5e9e6da577
commit 231f4dce07
12 changed files with 145 additions and 237 deletions

View file

@ -4,6 +4,10 @@ namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Transformers\ComponentsTransformer;
use App\Models\Component;
use App\Models\Company;
class ComponentsController extends Controller
{
@ -15,9 +19,41 @@ class ComponentsController extends Controller
*
* @return \Illuminate\Http\Response
*/
public function index()
public function index(Request $request)
{
//
$this->authorize('view', Component::class);
$components = Company::scopeCompanyables(Component::select('components.*')->whereNull('components.deleted_at')
->with('company', 'location', 'category'));
if ($request->has('search')) {
$components = $components->TextSearch($request->input('search'));
}
$offset = request('offset', 0);
$limit = request('limit', 50);
$allowed_columns = ['id','name','min_amt','order_number','serial','purchase_date','purchase_cost','company','category','qty','location'];
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
switch ($sort) {
case 'category':
$components = $components->OrderCategory($order);
break;
case 'location':
$components = $components->OrderLocation($order);
break;
case 'company':
$components = $components->OrderCompany($order);
break;
default:
$components = $components->orderBy($sort, $order);
break;
}
$total = $components->count();
$components = $components->skip($offset)->take($limit)->get();
return (new ComponentsTransformer)->transformComponents($components, $total);
}
@ -31,7 +67,14 @@ class ComponentsController extends Controller
*/
public function store(Request $request)
{
//
$this->authorize('create', Component::class);
$component = new Component;
$component->fill($request->all());
if ($component->save()) {
return response()->json(Helper::formatStandardApiResponse('success', $component, trans('admin/components/message.create.success')));
}
return response()->json(Helper::formatStandardApiResponse('error', null, $component->getErrors()));
}
/**
@ -43,7 +86,9 @@ class ComponentsController extends Controller
*/
public function show($id)
{
//
$this->authorize('view', Component::class);
$component = Component::findOrFail($id);
return (new ComponentsTransformer)->transformComponent($component);
}
@ -58,7 +103,15 @@ class ComponentsController extends Controller
*/
public function update(Request $request, $id)
{
//
$this->authorize('edit', Component::class);
$component = Component::findOrFail($id);
$component->fill($request->all());
if ($component->save()) {
return response()->json(Helper::formatStandardApiResponse('success', $component, trans('admin/components/message.update.success')));
}
return response()->json(Helper::formatStandardApiResponse('error', null, $component->getErrors()));
}
/**
@ -71,7 +124,11 @@ class ComponentsController extends Controller
*/
public function destroy($id)
{
//
$this->authorize('delete', Component::class);
$component = Component::findOrFail($id);
$this->authorize('delete', $component);
$component->delete();
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.delete.success')));
}
}

View file

@ -34,7 +34,7 @@ class ConsumablesController extends Controller
$offset = request('offset', 0);
$limit = request('limit', 50);
$allowed_columns = ['id','name','order_number','min_amt','purchase_date','purchase_cost','company','category','model_number', 'item_no', 'manufacturer','location'];
$allowed_columns = ['id','name','order_number','min_amt','purchase_date','purchase_cost','company','category','model_number', 'item_no', 'manufacturer','location','qty'];
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';

View file

@ -300,62 +300,6 @@ class ComponentsController extends Controller
}
/**
* Generates the JSON response for accessories listing view.
*
* For debugging, see at /api/accessories/list
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return string JSON
**/
public function getDatatable()
{
$this->authorize('view', Component::class);
$components = Company::scopeCompanyables(Component::select('components.*')->whereNull('components.deleted_at')
->with('company', 'location', 'category'));
if (Input::has('search')) {
$components = $components->TextSearch(Input::get('search'));
}
$offset = request('offset', 0);
$limit = request('limit', 50);
$allowed_columns = ['id','name','min_amt','order_number','serial','purchase_date','purchase_cost','companyName','category','total_qty'];
$order = Input::get('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
switch ($sort) {
case 'category':
$components = $components->OrderCategory($order);
break;
case 'location':
$components = $components->OrderLocation($order);
break;
case 'companyName':
$components = $components->OrderCompany($order);
break;
default:
$components = $components->orderBy($sort, $order);
break;
}
$componentsCount = $components->count();
$components = $components->skip($offset)->take($limit)->get();
$rows = array();
foreach ($components as $component) {
$rows[] = $component->present()->forDataTable();
}
$data = array('total' => $componentsCount, 'rows' => $rows);
return $data;
}
/**
* Return JSON data to populate the components view,
*

View file

@ -0,0 +1,66 @@
<?php
namespace App\Http\Transformers;
use App\Models\Component;
use Illuminate\Database\Eloquent\Collection;
use App\Helpers\Helper;
class ComponentsTransformer
{
public function transformComponents (Collection $components, $total)
{
$array = array();
foreach ($components as $component) {
$array[] = self::transformComponent($component);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
public function transformComponent (Component $component)
{
$transformed = [
'id' => $component->id,
'name' => e($component->name),
'serial_number' => e($component->serial),
'location' => ($component->location) ?
[
'id' => $component->location->id,
'name' => $component->location->name
] : null,
'qty' => number_format($component->qty),
'min_amt' => e($component->min_amt),
'category' => ($component->category) ?
[
'id' => $component->category->id,
'name' => e($component->category->name)
] : null,
'order_number' => e($component->order_number),
'purchase_date' => $component->purchase_date,
'purchase_cost' => Helper::formatCurrencyOutput($component->purchase_cost),
'remaining' => $component->numRemaining(),
'company' => ($component->company) ?
[
'id' => $component->company->id,
'name' => e($component->company->name)
] : null,
];
return $transformed;
}
public function transformCheckedoutComponents (Collection $components_users, $total)
{
$array = array();
foreach ($components_users as $user) {
$array[] = (new UsersTransformer)->transformUser($user);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
}

View file

@ -29,7 +29,7 @@ class ConsumablesTransformer
'min_amt' => $consumable->min_amt,
'model_number' => $consumable->model_number,
'name' => $consumable->name,
'numRemaining' => $consumable->numRemaining(),
'remaining' => $consumable->numRemaining(),
'order_number' => $consumable->order_number,
'purchase_cost' => Helper::formatCurrencyOutput($consumable->purchase_cost),
'purchase_date' => $consumable->purchase_date,

View file

@ -1,78 +0,0 @@
<?php
namespace App\Presenters;
use App\Helpers\Helper;
use Illuminate\Support\Facades\Gate;
/**
* Class ComponentPresenter
* @package App\Presenters
*/
class ComponentPresenter extends Presenter
{
/**
* Formatted JSON string for data table.
* @return array
*/
public function forDataTable()
{
$actions = '<nobr>';
if (Gate::allows('checkout', $this->model)) {
$actions .= Helper::generateDatatableButton('checkout', route('checkout/component', $this->id), $this->numRemaining() > 0);
}
if (Gate::allows('update', $this->model)) {
$actions .= Helper::generateDatatableButton('edit', route('components.edit', $this->id));
}
if (Gate::allows('delete', $this->model)) {
$actions .= Helper::generateDatatableButton(
'delete',
route('components.destroy', $this->id),
true, /* enabled */
trans('admin/components/message.delete.confirm'),
$this->name
);
}
$actions .='</nobr>';
$results = [
'checkbox' =>'<div class="text-center"><input type="checkbox" name="component['.$this->id.']" class="one_required"></div>',
'id' => $this->id,
'name' => $this->nameUrl(),
'serial_number' => $this->serial,
'location' => $this->locationUrl(),
'qty' => number_format($this->qty),
'min_amt' => e($this->min_amt),
'category' => $this->categoryUrl(),
'order_number' => $this->order_number,
'purchase_date' => $this->purchase_date,
'purchase_cost' => Helper::formatCurrencyOutput($this->purchase_cost),
'numRemaining' => $this->numRemaining(),
'actions' => $actions,
'companyName' => $this->companyUrl(),
];
return $results;
}
/**
* Link to this components name
* @return string
*/
public function nameUrl()
{
return (string) link_to_route('components.show', $this->name, $this->id);
}
/**
* Url to view this item.
* @return string
*/
public function viewUrl()
{
return route('components.show', $this->id);
}
}

View file

@ -1,83 +0,0 @@
<?php
namespace App\Presenters;
use App\Helpers\Helper;
use Illuminate\Support\Facades\Gate;
/**
* Class ConsumablePresenter
* @package App\Presenters
*/
class ConsumablePresenter extends Presenter
{
/**
* Formatted JSON for data table.
* @return array
*/
public function forDataTable()
{
$actions = '<nobr>';
if (Gate::allows('checkout', $this->model)) {
$actions .= Helper::generateDatatableButton('checkout', route('checkout/consumable', $this->id), $this->numRemaining() > 0);
}
if (Gate::allows('update', $this->model)) {
$actions .= Helper::generateDatatableButton('edit', route('consumables.edit', $this->id));
}
if (Gate::allows('delete', $this->model)) {
$actions .= Helper::generateDatatableButton(
'delete',
route('consumables.destroy', $this->id),
true, /* enabled */
trans('admin/consumables/message.delete.confirm'),
$this->name
);
}
$actions .='</nobr>';
$results = [
'actions' => $actions,
'category' => $this->categoryUrl(),
'companyName' => $this->companyUrl(),
'id' => $this->id,
'item_no' => $this->item_no,
'location' => $this->locationUrl(),
'manufacturer' => $this->manufacturerUrl(),
'min_amt' => $this->min_amt,
'model_number' => $this->model_number,
'name' => $this->nameUrl(),
'numRemaining' => $this->numRemaining(),
'order_number' => $this->order_number,
'purchase_cost' => Helper::formatCurrencyOutput($this->purchase_cost),
'purchase_date' => $this->purchase_date,
'qty' => $this->qty,
];
return $results;
}
/**
* Displayable name of consumable
* @return string
*/
public function name()
{
return $this->model->name;
}
public function nameUrl()
{
return (string)link_to_route('consumables.show', $this->name, $this->id);
}
/**
* Url to view this item.
* @return string
*/
public function viewUrl()
{
return route('consumables.show', $this->id);
}
}

View file

@ -44,20 +44,21 @@
>
<thead>
<tr>
<th data-class="hidden-xs" data-switchable="false" data-searchable="false" data-sortable="false" data-field="checkbox"><div class="text-center"><input type="checkbox" id="checkAll" style="padding-left: 0px;"></div></th>
{{--<th data-checkbox="true" data-field="checkbox"></th>--}}
<th data-sortable="true" data-field="id" data-visible="false">{{ trans('general.id') }}</th>
<th data-switchable="true" data-visible="false" data-searchable="true" data-sortable="true" data-field="companyName">{{ trans('admin/companies/table.title') }}</th>
<th data-sortable="true" data-searchable="true" data-field="name">{{ trans('admin/components/table.title') }}</th>
<th data-switchable="true" data-visible="false" data-searchable="true" data-sortable="true" data-field="company" data-formatter="companiesLinkObjFormatter">{{ trans('admin/companies/table.title') }}</th>
<th data-sortable="true" data-searchable="true" data-field="name" data-formatter="componentsLinkFormatter">{{ trans('admin/components/table.title') }}</th>
<th data-sortable="true" data-searchable="true" data-field="serial_number" data-visible="false">{{ trans('admin/hardware/form.serial') }}</th>
<th data-searchable="true" data-sortable="true" data-field="location">{{ trans('general.location') }}</th>
<th data-searchable="true" data-sortable="true" data-field="category">{{ trans('general.category') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="total_qty"> {{ trans('admin/components/general.total') }}</th>
<th data-searchable="true" data-sortable="true" data-field="location" data-formatter="locationsLinkObjFormatter">{{ trans('general.location') }}</th>
<th data-searchable="true" data-sortable="true" data-field="category" data-formatter="categoriesLinkObjFormatter">{{ trans('general.category') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="true" data-field="qty"> {{ trans('admin/components/general.total') }}</th>
<th data-switchable="true" data-searchable="false" data-sortable="true" data-field="min_amt"> {{ trans('general.min_amt') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="numRemaining"> {{ trans('admin/components/general.remaining') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="remaining"> {{ trans('admin/components/general.remaining') }}</th>
<th data-sortable="true" data-searchable="true" data-field="order_number" data-visible="false">{{ trans('admin/components/general.order') }}</th>
<th data-sortable="true" data-searchable="true" data-field="purchase_date" data-visible="false">{{ trans('admin/components/general.date') }}</th>
<th data-sortable="true" data-searchable="true" data-field="purchase_cost" data-visible="false">{{ trans('admin/components/general.cost') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions"> {{ trans('table.actions') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions" data-formatter="componentsActionsFormatter"> {{ trans('table.actions') }}</th>
</tr>
</thead>
</table>

View file

@ -49,7 +49,7 @@
name="component_users"
class="table table-striped snipe-table"
id="table"
data-url="{{route('api.components.view', $component->id)}}"
data-url="{{route('api.components.show', $component->id)}}"
data-cookie="true"
data-click-to-select="true"
data-cookie-id-table="componentDetailTable-{{ config('version.hash_version') }}"

View file

@ -36,8 +36,8 @@
<th data-sortable="true" data-searchable="true" data-field="name" data-formatter="consumablesLinkFormatter">{{ trans('admin/consumables/table.title') }}</th>
<th data-searchable="true" data-sortable="true" data-field="location" data-formatter="locationsLinkObjFormatter">{{ trans('general.location') }}</th>
<th data-searchable="true" data-sortable="true" data-field="category" data-formatter="categoriesLinkObjFormatter">{{ trans('general.category') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="qty"> {{ trans('admin/consumables/general.total') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="numRemaining"> {{ trans('admin/consumables/general.remaining') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="true" data-field="qty"> {{ trans('admin/consumables/general.total') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="remaining"> {{ trans('admin/consumables/general.remaining') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="true" data-field="min_amt"> {{ trans('general.min_amt') }}</th>
<th data-sortable="true" data-field="manufacturer" data-formatter="manufacturersLinkObjFormatter" data-visible="false">{{ trans('general.manufacturer') }}</th>
<th data-sortable="true" data-field="model_number" data-visible="false">{{ trans('general.model_no') }}</th>

View file

@ -35,7 +35,7 @@
name="consumable_users"
class="table table-striped snipe-table"
id="table"
data-url="{{route('api.consumables.view', $consumable->id)}}"
data-url="{{route('api.consumables.show', $consumable->id)}}"
data-cookie="true"
data-click-to-select="true"
data-cookie-id-table="consumableDetailTable-{{ config('version.hash_version') }}"

View file

@ -209,6 +209,7 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
[
'index' => 'api.consumables.index',
'create' => 'api.consumables.create',
'show' => 'api.consumables.show',
'destroy' => 'api.consumables.destroy'
],
'parameters' =>