A free open source IT asset/license management system
Implement API for labels
Cram425558a005b9c4
diff --git a/app/Http/Controllers/Api/LabelsController.php b/app/Http/Controllers/Api/LabelsController.phpnew file mode 100644index 0000000000..6576ec0373--- /dev/null+++ b/app/Http/Controllers/Api/LabelsController.php@@ -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 <[email protected]>+ * @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 <[email protected]>+ * @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);+ }++}diff --git a/app/Http/Transformers/LabelsTransformer.php b/app/Http/Transformers/LabelsTransformer.phpnew file mode 100644index 0000000000..f575f91d6c--- /dev/null+++ b/app/Http/Transformers/LabelsTransformer.php@@ -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;+ }+}diff --git a/routes/api.php b/routes/api.phpindex d27f146013..19ec4fd74a 100644--- a/routes/api.php+++ b/routes/api.php@@ -611,6 +611,16 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'throttle:api']], functi); // 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