mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-25 21:54:14 -08:00
Implement API for labels
This commit is contained in:
parent
bb09f0168f
commit
5558a005b9
71
app/Http/Controllers/Api/LabelsController.php
Normal file
71
app/Http/Controllers/Api/LabelsController.php
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Helpers\Helper;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Transformers\LabelsTransformer;
|
||||||
|
use App\Models\Labels\Label;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\ItemNotFoundException;
|
||||||
|
use Auth;
|
||||||
|
|
||||||
|
class LabelsController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns JSON listing of all labels.
|
||||||
|
*
|
||||||
|
* @author Grant Le Roux <grant.leroux+snipe-it@gmail.com>
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$this->authorize('view', Label::class);
|
||||||
|
|
||||||
|
$labels = Label::find();
|
||||||
|
|
||||||
|
if ($request->filled('search')) {
|
||||||
|
$search = $request->get('search');
|
||||||
|
$labels = $labels->filter(function ($label, $index) use ($search) {
|
||||||
|
return stripos($label->getName(), $search) !== false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$total = $labels->count();
|
||||||
|
|
||||||
|
$offset = $request->get('offset', 0);
|
||||||
|
$offset = ($offset > $total) ? $total : $offset;
|
||||||
|
|
||||||
|
$maxLimit = config('app.max_results');
|
||||||
|
$limit = $request->get('limit', $maxLimit);
|
||||||
|
$limit = ($limit > $maxLimit) ? $maxLimit : $limit;
|
||||||
|
|
||||||
|
$labels = $labels->skip($offset)->take($limit);
|
||||||
|
|
||||||
|
return (new LabelsTransformer)->transformLabels($labels, $total, $request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns JSON with information about a label for detail view.
|
||||||
|
*
|
||||||
|
* @author Grant Le Roux <grant.leroux+snipe-it@gmail.com>
|
||||||
|
* @param string $labelName
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
public function show(string $labelName)
|
||||||
|
{
|
||||||
|
$labelName = str_replace('/', '\\', $labelName);
|
||||||
|
try {
|
||||||
|
$label = Label::find($labelName);
|
||||||
|
} catch(ItemNotFoundException $e) {
|
||||||
|
return response()
|
||||||
|
->json(
|
||||||
|
Helper::formatStandardApiResponse('error', null, trans('admin/labels/message.does_not_exist')),
|
||||||
|
404
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$this->authorize('view', $label);
|
||||||
|
return (new LabelsTransformer)->transformLabel($label);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
70
app/Http/Transformers/LabelsTransformer.php
Normal file
70
app/Http/Transformers/LabelsTransformer.php
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Transformers;
|
||||||
|
|
||||||
|
use App\Helpers\Helper;
|
||||||
|
use App\Models\Labels\Label;
|
||||||
|
use App\Models\Labels\Sheet;
|
||||||
|
use App\Models\Labels\RectangleSheet;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
class LabelsTransformer
|
||||||
|
{
|
||||||
|
public function transformLabels(Collection $labels, $total)
|
||||||
|
{
|
||||||
|
$array = [];
|
||||||
|
foreach ($labels as $label) {
|
||||||
|
$array[] = self::transformLabel($label);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function transformLabel(Label $label)
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
'name' => $label->getName(),
|
||||||
|
'unit' => $label->getUnit(),
|
||||||
|
|
||||||
|
'width' => $label->getWidth(),
|
||||||
|
'height' => $label->getHeight(),
|
||||||
|
|
||||||
|
'margin_top' => $label->getMarginTop(),
|
||||||
|
'margin_bottom' => $label->getMarginBottom(),
|
||||||
|
'margin_left' => $label->getMarginLeft(),
|
||||||
|
'margin_right' => $label->getMarginRight(),
|
||||||
|
|
||||||
|
'support_1d_barcode' => $label->getSupport1DBarcode(),
|
||||||
|
'support_2d_barcode' => $label->getSupport2DBarcode(),
|
||||||
|
'support_fields' => $label->getSupportFields(),
|
||||||
|
'support_logo' => $label->getSupportLogo(),
|
||||||
|
'support_title' => $label->getSupportTitle(),
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($label instanceof Sheet) {
|
||||||
|
$array['sheet_info'] = [
|
||||||
|
'label_width' => $label->getLabelWidth(),
|
||||||
|
'label_height' => $label->getLabelHeight(),
|
||||||
|
|
||||||
|
'label_margin_top' => $label->getLabelMarginTop(),
|
||||||
|
'label_margin_bottom' => $label->getLabelMarginBottom(),
|
||||||
|
'label_margin_left' => $label->getLabelMarginLeft(),
|
||||||
|
'label_margin_right' => $label->getLabelMarginRight(),
|
||||||
|
|
||||||
|
'labels_per_page' => $label->getLabelsPerPage(),
|
||||||
|
'label_border' => $label->getLabelBorder(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($label instanceof RectangleSheet) {
|
||||||
|
$array['rectanglesheet_info'] = [
|
||||||
|
'columns' => $label->getColumns(),
|
||||||
|
'rows' => $label->getRows(),
|
||||||
|
'column_spacing' => $label->getLabelColumnSpacing(),
|
||||||
|
'row_spacing' => $label->getLabelRowSpacing(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
}
|
|
@ -611,6 +611,16 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'throttle:api']], functi
|
||||||
); // end imports API routes
|
); // end imports API routes
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Labels API routes
|
||||||
|
*/
|
||||||
|
Route::group(['prefix' => 'labels'], function() {
|
||||||
|
Route::get('{name}', [ Api\LabelsController::class, 'show'])
|
||||||
|
->where('name', '.*')
|
||||||
|
->name('api.labels.show');
|
||||||
|
Route::get('', [ Api\LabelsController::class, 'index'])
|
||||||
|
->name('api.labels.index');
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Licenses API routes
|
* Licenses API routes
|
||||||
|
|
Loading…
Reference in a new issue