|
| 1 | +@php |
| 2 | + echo "<?php".PHP_EOL; |
| 3 | +@endphp |
| 4 | + |
| 5 | +namespace {{ $config->namespaces->apiController }}; |
| 6 | + |
| 7 | +use {{ $config->namespaces->apiRequest }}\Create{{ $config->modelNames->name }}APIRequest; |
| 8 | +use {{ $config->namespaces->apiRequest }}\Update{{ $config->modelNames->name }}APIRequest; |
| 9 | +use {{ $config->namespaces->model }}\{{ $config->modelNames->name }}; |
| 10 | +use Illuminate\Http\JsonResponse; |
| 11 | +use Illuminate\Http\Request; |
| 12 | +use {{ $config->namespaces->app }}\Http\Controllers\AppBaseController; |
| 13 | + |
| 14 | +{!! $docController !!} |
| 15 | +class {{ $config->modelNames->name }}APIController extends AppBaseController |
| 16 | +{ |
| 17 | + {!! $docIndex !!} |
| 18 | + public function index(Request $request): JsonResponse |
| 19 | + { |
| 20 | + $query = {{ $config->modelNames->name }}::query(); |
| 21 | + |
| 22 | + if ($request->get('skip')) { |
| 23 | + $query->skip($request->get('skip')); |
| 24 | + } |
| 25 | + if ($request->get('limit')) { |
| 26 | + $query->limit($request->get('limit')); |
| 27 | + } |
| 28 | + |
| 29 | + ${{ $config->modelNames->camelPlural }} = $query->get(); |
| 30 | + |
| 31 | +@if($config->options->localized) |
| 32 | + return $this->sendResponse( |
| 33 | + ${{ $config->modelNames->camelPlural }}->toArray(), |
| 34 | + __('messages.retrieved', ['model' => __('models/{{ $config->modelNames->camelPlural }}.plural')]) |
| 35 | + ); |
| 36 | +@else |
| 37 | + return $this->sendResponse(${{ $config->modelNames->camelPlural }}->toArray(), '{{ $config->modelNames->humanPlural }} retrieved successfully'); |
| 38 | +@endif |
| 39 | + } |
| 40 | + |
| 41 | + {!! $docStore !!} |
| 42 | + public function store(Create{{ $config->modelNames->name }}APIRequest $request): JsonResponse |
| 43 | + { |
| 44 | + $input = $request->all(); |
| 45 | + |
| 46 | + /** @var {{ $config->modelNames->name }} ${{ $config->modelNames->camel }} */ |
| 47 | + ${{ $config->modelNames->camel }} = {{ $config->modelNames->name }}::create($input); |
| 48 | + |
| 49 | +@if($config->options->localized) |
| 50 | + return $this->sendResponse( |
| 51 | + ${{ $config->modelNames->camel }}->toArray(), |
| 52 | + __('messages.saved', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')]) |
| 53 | + ); |
| 54 | +@else |
| 55 | + return $this->sendResponse(${{ $config->modelNames->camel }}->toArray(), '{{ $config->modelNames->human }} saved successfully'); |
| 56 | +@endif |
| 57 | + } |
| 58 | + |
| 59 | + {!! $docShow !!} |
| 60 | + public function show($id): JsonResponse |
| 61 | + { |
| 62 | + /** @var {{ $config->modelNames->name }} ${{ $config->modelNames->camel }} */ |
| 63 | + ${{ $config->modelNames->camel }} = {{ $config->modelNames->name }}::find($id); |
| 64 | + |
| 65 | + if (empty(${{ $config->modelNames->camel }})) { |
| 66 | +@if($config->options->localized) |
| 67 | + return $this->sendError( |
| 68 | + __('messages.not_found', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')]) |
| 69 | + ); |
| 70 | +@else |
| 71 | + return $this->sendError('{{ $config->modelNames->human }} not found'); |
| 72 | +@endif |
| 73 | + } |
| 74 | + |
| 75 | +@if($config->options->localized) |
| 76 | + return $this->sendResponse( |
| 77 | + ${{ $config->modelNames->camel }}->toArray(), |
| 78 | + __('messages.retrieved', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')]) |
| 79 | + ); |
| 80 | +@else |
| 81 | + return $this->sendResponse(${{ $config->modelNames->camel }}->toArray(), '{{ $config->modelNames->human }} retrieved successfully'); |
| 82 | +@endif |
| 83 | + } |
| 84 | + |
| 85 | + {!! $docUpdate !!} |
| 86 | + public function update($id, Update{{ $config->modelNames->name }}APIRequest $request): JsonResponse |
| 87 | + { |
| 88 | + /** @var {{ $config->modelNames->name }} ${{ $config->modelNames->camel }} */ |
| 89 | + ${{ $config->modelNames->camel }} = {{ $config->modelNames->name }}::find($id); |
| 90 | + |
| 91 | + if (empty(${{ $config->modelNames->camel }})) { |
| 92 | +@if($config->options->localized) |
| 93 | + return $this->sendError( |
| 94 | + __('messages.not_found', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')]) |
| 95 | + ); |
| 96 | +@else |
| 97 | + return $this->sendError('{{ $config->modelNames->human }} not found'); |
| 98 | +@endif |
| 99 | + } |
| 100 | + |
| 101 | + ${{ $config->modelNames->camel }}->fill($request->all()); |
| 102 | + ${{ $config->modelNames->camel }}->save(); |
| 103 | + |
| 104 | +@if($config->options->localized) |
| 105 | + return $this->sendResponse( |
| 106 | + ${{ $config->modelNames->camel }}->toArray(), |
| 107 | + __('messages.updated', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')]) |
| 108 | + ); |
| 109 | +@else |
| 110 | + return $this->sendResponse(${{ $config->modelNames->camel }}->toArray(), '{{ $config->modelNames->name }} updated successfully'); |
| 111 | +@endif |
| 112 | + } |
| 113 | + |
| 114 | + {!! $docDestroy !!} |
| 115 | + public function destroy($id): JsonResponse |
| 116 | + { |
| 117 | + /** @var {{ $config->modelNames->name }} ${{ $config->modelNames->camel }} */ |
| 118 | + ${{ $config->modelNames->camel }} = {{ $config->modelNames->name }}::find($id); |
| 119 | + |
| 120 | + if (empty(${{ $config->modelNames->camel }})) { |
| 121 | +@if($config->options->localized) |
| 122 | + return $this->sendError( |
| 123 | + __('messages.not_found', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')]) |
| 124 | + ); |
| 125 | +@else |
| 126 | + return $this->sendError('{{ $config->modelNames->human }} not found'); |
| 127 | +@endif |
| 128 | + } |
| 129 | + |
| 130 | + ${{ $config->modelNames->camel }}->delete(); |
| 131 | + |
| 132 | +@if($config->options->localized) |
| 133 | + return $this->sendResponse( |
| 134 | + $id, |
| 135 | + __('messages.deleted', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')]) |
| 136 | + ); |
| 137 | +@else |
| 138 | + return $this->sendSuccess('{{ $config->modelNames->human }} deleted successfully'); |
| 139 | +@endif |
| 140 | + } |
| 141 | +} |
0 commit comments