@@ -4,9 +4,6 @@ |
||
| 4 | 4 | |
| 5 | 5 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 6 | 6 | use Illuminate\Contracts\Validation\ValidationException; |
| 7 | -use Illuminate\Foundation\Bus\DispatchesCommands; |
|
| 8 | -use Illuminate\Foundation\Validation\ValidatesRequests; |
|
| 9 | -use Illuminate\Routing\Controller as BaseController; |
|
| 10 | 7 | |
| 11 | 8 | use \Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController; |
| 12 | 9 | |
@@ -14,182 +14,182 @@ |
||
| 14 | 14 | |
| 15 | 15 | abstract class ApiController extends ApiGuardController implements ApiInterface |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Laraponse object, which abstracts Fractal |
|
| 19 | - * |
|
| 20 | - * @var \EllipseSynergie\ApiResponse\Laravel\Response |
|
| 21 | - */ |
|
| 22 | - public $response; |
|
| 17 | + /** |
|
| 18 | + * Laraponse object, which abstracts Fractal |
|
| 19 | + * |
|
| 20 | + * @var \EllipseSynergie\ApiResponse\Laravel\Response |
|
| 21 | + */ |
|
| 22 | + public $response; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The string name of the Eloquent Model |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - protected $modelName; |
|
| 24 | + /** |
|
| 25 | + * The string name of the Eloquent Model |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + protected $modelName; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * The Eloquent Model |
|
| 33 | - * |
|
| 34 | - * @var \Illuminate\Database\Eloquent\Model |
|
| 35 | - */ |
|
| 36 | - protected $model; |
|
| 31 | + /** |
|
| 32 | + * The Eloquent Model |
|
| 33 | + * |
|
| 34 | + * @var \Illuminate\Database\Eloquent\Model |
|
| 35 | + */ |
|
| 36 | + protected $model; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * The string name of the Transformer class |
|
| 40 | - * |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - protected $transformerName; |
|
| 38 | + /** |
|
| 39 | + * The string name of the Transformer class |
|
| 40 | + * |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + protected $transformerName; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * The name of the collection that is returned in the JSON response |
|
| 47 | - * |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 50 | - protected $collectionName; |
|
| 45 | + /** |
|
| 46 | + * The name of the collection that is returned in the JSON response |
|
| 47 | + * |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | + protected $collectionName; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * The Fractal Manager |
|
| 54 | - * |
|
| 55 | - * @var \League\Fractal\Manager |
|
| 56 | - */ |
|
| 57 | - public $manager; |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Constructor |
|
| 62 | - */ |
|
| 63 | - public function __construct() |
|
| 64 | - { |
|
| 65 | - $this->response = \Response::api(); |
|
| 52 | + /** |
|
| 53 | + * The Fractal Manager |
|
| 54 | + * |
|
| 55 | + * @var \League\Fractal\Manager |
|
| 56 | + */ |
|
| 57 | + public $manager; |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Constructor |
|
| 62 | + */ |
|
| 63 | + public function __construct() |
|
| 64 | + { |
|
| 65 | + $this->response = \Response::api(); |
|
| 66 | 66 | |
| 67 | - if (!empty($this->modelName)) { |
|
| 68 | - $this->model = new $this->modelName; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // if no collection name provided, use the model's table name |
|
| 72 | - if (empty($this->collectionName)) { |
|
| 73 | - $this->collectionName = $this->model->getTable(); |
|
| 74 | - } |
|
| 67 | + if (!empty($this->modelName)) { |
|
| 68 | + $this->model = new $this->modelName; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // if no collection name provided, use the model's table name |
|
| 72 | + if (empty($this->collectionName)) { |
|
| 73 | + $this->collectionName = $this->model->getTable(); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - // parse includes |
|
| 77 | - if (\Input::get('include') != '') { |
|
| 78 | - $this->manager = new \League\Fractal\Manager; |
|
| 79 | - $this->manager->parseIncludes(explode(',', \Input::get('include'))); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - parent::__construct(); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Creates an item |
|
| 87 | - * |
|
| 88 | - * @param \Illuminate\Http\Request $data |
|
| 89 | - * @return \Illuminate\Http\JsonResponse |
|
| 90 | - */ |
|
| 91 | - public function create(Request $data = null) |
|
| 92 | - { |
|
| 93 | - $data = $data ?: \Input::json(); |
|
| 94 | - $json = $data->all(); |
|
| 95 | - $json = $this->addAttribution($json); |
|
| 96 | - |
|
| 97 | - try { |
|
| 98 | - $this->model->validate($json); |
|
| 99 | - $item = $this->model->create($json); |
|
| 100 | - return $this->showByObject($item); |
|
| 76 | + // parse includes |
|
| 77 | + if (\Input::get('include') != '') { |
|
| 78 | + $this->manager = new \League\Fractal\Manager; |
|
| 79 | + $this->manager->parseIncludes(explode(',', \Input::get('include'))); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + parent::__construct(); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Creates an item |
|
| 87 | + * |
|
| 88 | + * @param \Illuminate\Http\Request $data |
|
| 89 | + * @return \Illuminate\Http\JsonResponse |
|
| 90 | + */ |
|
| 91 | + public function create(Request $data = null) |
|
| 92 | + { |
|
| 93 | + $data = $data ?: \Input::json(); |
|
| 94 | + $json = $data->all(); |
|
| 95 | + $json = $this->addAttribution($json); |
|
| 96 | + |
|
| 97 | + try { |
|
| 98 | + $this->model->validate($json); |
|
| 99 | + $item = $this->model->create($json); |
|
| 100 | + return $this->showByObject($item); |
|
| 101 | 101 | |
| 102 | - } catch (ValidationException $e) { |
|
| 103 | - return $this->response->setStatusCode(422)->withError($e->errors()->all(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 104 | - } catch (MassAssignmentException $e) { |
|
| 105 | - return $this->response->setStatusCode(422)->withError("Cannot mass assign " . $e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 106 | - } catch (\Exception $e) { |
|
| 107 | - return $this->response->setStatusCode(422)->withError($e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Adds created_by and updated_by to the array if the model supports it |
|
| 113 | - * |
|
| 114 | - * @param array $data |
|
| 115 | - * @return array $data |
|
| 116 | - */ |
|
| 117 | - protected function addAttribution(array $data) |
|
| 118 | - { |
|
| 119 | - if ($this->model->attirbution) { |
|
| 120 | - $data['created_by'] = $this->apiKey->user_id; |
|
| 121 | - $data['updated_by'] = $this->apiKey->user_id; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $data; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Returns a single item |
|
| 129 | - * |
|
| 130 | - * @param int $id |
|
| 131 | - * @return \Illuminate\Http\JsonResponse |
|
| 132 | - */ |
|
| 133 | - public function show($id) |
|
| 134 | - { |
|
| 135 | - try { |
|
| 102 | + } catch (ValidationException $e) { |
|
| 103 | + return $this->response->setStatusCode(422)->withError($e->errors()->all(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 104 | + } catch (MassAssignmentException $e) { |
|
| 105 | + return $this->response->setStatusCode(422)->withError("Cannot mass assign " . $e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 106 | + } catch (\Exception $e) { |
|
| 107 | + return $this->response->setStatusCode(422)->withError($e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Adds created_by and updated_by to the array if the model supports it |
|
| 113 | + * |
|
| 114 | + * @param array $data |
|
| 115 | + * @return array $data |
|
| 116 | + */ |
|
| 117 | + protected function addAttribution(array $data) |
|
| 118 | + { |
|
| 119 | + if ($this->model->attirbution) { |
|
| 120 | + $data['created_by'] = $this->apiKey->user_id; |
|
| 121 | + $data['updated_by'] = $this->apiKey->user_id; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $data; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Returns a single item |
|
| 129 | + * |
|
| 130 | + * @param int $id |
|
| 131 | + * @return \Illuminate\Http\JsonResponse |
|
| 132 | + */ |
|
| 133 | + public function show($id) |
|
| 134 | + { |
|
| 135 | + try { |
|
| 136 | 136 | |
| 137 | - return $this->response->withItem($this->model->findOrFail($id), new $this->transformerName); |
|
| 137 | + return $this->response->withItem($this->model->findOrFail($id), new $this->transformerName); |
|
| 138 | 138 | |
| 139 | - } catch (ModelNotFoundException $e) { |
|
| 140 | - |
|
| 141 | - return $this->respondNotFound(); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Returns a single item |
|
| 147 | - * |
|
| 148 | - * @param object $object |
|
| 149 | - * @return \Illuminate\Http\JsonResponse |
|
| 150 | - */ |
|
| 151 | - public function showByObject($object) |
|
| 152 | - { |
|
| 153 | - try { |
|
| 154 | - return $this->response->withItem($object, new $this->transformerName); |
|
| 139 | + } catch (ModelNotFoundException $e) { |
|
| 140 | + |
|
| 141 | + return $this->respondNotFound(); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Returns a single item |
|
| 147 | + * |
|
| 148 | + * @param object $object |
|
| 149 | + * @return \Illuminate\Http\JsonResponse |
|
| 150 | + */ |
|
| 151 | + public function showByObject($object) |
|
| 152 | + { |
|
| 153 | + try { |
|
| 154 | + return $this->response->withItem($object, new $this->transformerName); |
|
| 155 | 155 | |
| 156 | - } catch (ModelNotFoundException $e) { |
|
| 157 | - |
|
| 158 | - return $this->respondNotFound(); |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Returns a paginated collection |
|
| 164 | - * |
|
| 165 | - * @return \Illuminate\Http\JsonResponse |
|
| 166 | - */ |
|
| 167 | - public function collection() |
|
| 168 | - { |
|
| 169 | - $limit = \Input::get('limit') ?: 10; |
|
| 170 | - $model = $this->model; |
|
| 156 | + } catch (ModelNotFoundException $e) { |
|
| 157 | + |
|
| 158 | + return $this->respondNotFound(); |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Returns a paginated collection |
|
| 164 | + * |
|
| 165 | + * @return \Illuminate\Http\JsonResponse |
|
| 166 | + */ |
|
| 167 | + public function collection() |
|
| 168 | + { |
|
| 169 | + $limit = \Input::get('limit') ?: 10; |
|
| 170 | + $model = $this->model; |
|
| 171 | 171 | |
| 172 | - if (\Request::has('order')) { |
|
| 173 | - list($orderCol, $orderBy) = explode('|', \Input::get('order')); |
|
| 174 | - $model = $model->orderBy($orderCol, $orderBy); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - return $this->response->withPaginator($model->paginate($limit), new $this->transformerName, $this->collectionName); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Handles 404 errors |
|
| 182 | - * |
|
| 183 | - * @param string $msg |
|
| 184 | - * @return \Illuminate\Http\JsonResponse |
|
| 185 | - */ |
|
| 186 | - public function respondNotFound($msg = 'Not found!') |
|
| 187 | - { |
|
| 188 | - return \Response::json([ |
|
| 189 | - 'error' => [ |
|
| 190 | - 'message' => $msg, |
|
| 191 | - 'status_code' => 404 |
|
| 192 | - ] |
|
| 193 | - ], 404); |
|
| 194 | - } |
|
| 172 | + if (\Request::has('order')) { |
|
| 173 | + list($orderCol, $orderBy) = explode('|', \Input::get('order')); |
|
| 174 | + $model = $model->orderBy($orderCol, $orderBy); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + return $this->response->withPaginator($model->paginate($limit), new $this->transformerName, $this->collectionName); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Handles 404 errors |
|
| 182 | + * |
|
| 183 | + * @param string $msg |
|
| 184 | + * @return \Illuminate\Http\JsonResponse |
|
| 185 | + */ |
|
| 186 | + public function respondNotFound($msg = 'Not found!') |
|
| 187 | + { |
|
| 188 | + return \Response::json([ |
|
| 189 | + 'error' => [ |
|
| 190 | + 'message' => $msg, |
|
| 191 | + 'status_code' => 404 |
|
| 192 | + ] |
|
| 193 | + ], 404); |
|
| 194 | + } |
|
| 195 | 195 | } |
| 196 | 196 | \ No newline at end of file |
@@ -103,7 +103,7 @@ |
||
| 103 | 103 | return $this->response->setStatusCode(422)->withError($e->errors()->all(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 104 | 104 | } catch (MassAssignmentException $e) { |
| 105 | 105 | return $this->response->setStatusCode(422)->withError("Cannot mass assign " . $e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 106 | - } catch (\Exception $e) { |
|
| 106 | + } catch (\Exception $e) { |
|
| 107 | 107 | return $this->response->setStatusCode(422)->withError($e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 108 | 108 | } |
| 109 | 109 | } |
@@ -103,7 +103,7 @@ |
||
| 103 | 103 | return $this->response->setStatusCode(422)->withError($e->errors()->all(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 104 | 104 | } catch (MassAssignmentException $e) { |
| 105 | 105 | return $this->response->setStatusCode(422)->withError("Cannot mass assign " . $e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 106 | - } catch (\Exception $e) { |
|
| 106 | + } catch (\Exception $e) { |
|
| 107 | 107 | return $this->response->setStatusCode(422)->withError($e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 108 | 108 | } |
| 109 | 109 | } |
@@ -5,7 +5,6 @@ |
||
| 5 | 5 | use Illuminate\Foundation\Validation\ValidatesRequests; |
| 6 | 6 | use Illuminate\Support\MessageBag; |
| 7 | 7 | use Illuminate\Support\Pluralizer; |
| 8 | -use Illuminate\Support\Singular; |
|
| 9 | 8 | |
| 10 | 9 | abstract class BaseModel extends Model { |
| 11 | 10 | |
@@ -74,21 +74,21 @@ discard block |
||
| 74 | 74 | public $attirbution = false; |
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | - * The "booting" method of the model. |
|
| 78 | - * |
|
| 79 | - * @return void |
|
| 80 | - */ |
|
| 81 | - protected static function boot() |
|
| 82 | - { |
|
| 83 | - if (!is_null(static::$updateRules) && !is_null(static::$updateRulesConcat) && sizeof(static::$updateRulesConcat)) { |
|
| 84 | - static::$updateRules = static::$createRules + static::$updateRulesConcat; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - parent::boot(); |
|
| 88 | - |
|
| 89 | - static::savedEvent(); |
|
| 90 | - static::deletingEvent(); |
|
| 91 | - } |
|
| 77 | + * The "booting" method of the model. |
|
| 78 | + * |
|
| 79 | + * @return void |
|
| 80 | + */ |
|
| 81 | + protected static function boot() |
|
| 82 | + { |
|
| 83 | + if (!is_null(static::$updateRules) && !is_null(static::$updateRulesConcat) && sizeof(static::$updateRulesConcat)) { |
|
| 84 | + static::$updateRules = static::$createRules + static::$updateRulesConcat; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + parent::boot(); |
|
| 88 | + |
|
| 89 | + static::savedEvent(); |
|
| 90 | + static::deletingEvent(); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | /** |
| 94 | 94 | * Gets the table name of the model. |
@@ -101,35 +101,35 @@ discard block |
||
| 101 | 101 | return $this->table; |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Hooks into the "saved" event |
|
| 106 | - * |
|
| 107 | - * @return void |
|
| 108 | - */ |
|
| 109 | - protected static function savedEvent() |
|
| 110 | - { |
|
| 111 | - static::saved(function($model) |
|
| 112 | - { |
|
| 113 | - static::addToCache($model); |
|
| 114 | - |
|
| 115 | - return true; |
|
| 116 | - }); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Hooks into the "deleting" event |
|
| 121 | - * |
|
| 122 | - * @return void |
|
| 123 | - */ |
|
| 124 | - protected static function deletingEvent() |
|
| 125 | - { |
|
| 126 | - static::deleting(function($model) |
|
| 127 | - { |
|
| 128 | - static::removeFromCache($model); |
|
| 129 | - |
|
| 130 | - return true; |
|
| 131 | - }); |
|
| 132 | - } |
|
| 104 | + /** |
|
| 105 | + * Hooks into the "saved" event |
|
| 106 | + * |
|
| 107 | + * @return void |
|
| 108 | + */ |
|
| 109 | + protected static function savedEvent() |
|
| 110 | + { |
|
| 111 | + static::saved(function($model) |
|
| 112 | + { |
|
| 113 | + static::addToCache($model); |
|
| 114 | + |
|
| 115 | + return true; |
|
| 116 | + }); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Hooks into the "deleting" event |
|
| 121 | + * |
|
| 122 | + * @return void |
|
| 123 | + */ |
|
| 124 | + protected static function deletingEvent() |
|
| 125 | + { |
|
| 126 | + static::deleting(function($model) |
|
| 127 | + { |
|
| 128 | + static::removeFromCache($model); |
|
| 129 | + |
|
| 130 | + return true; |
|
| 131 | + }); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | 134 | /** |
| 135 | 135 | * Retrieves the singular name of the table |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | /** |
| 33 | 33 | * Rules to validate when creating a model |
| 34 | 34 | * |
| 35 | - * @var array |
|
| 35 | + * @var array |
|
| 36 | 36 | */ |
| 37 | 37 | protected static $createRules = [ |
| 38 | 38 | 'parent_id' => 'integer', |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | /** |
| 48 | 48 | * Indicates if the model should be attributed with created_by and updated_by |
| 49 | 49 | * |
| 50 | - * @var bool |
|
| 50 | + * @var bool |
|
| 51 | 51 | */ |
| 52 | 52 | public $attirbution = true; |
| 53 | 53 | |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 58 | 58 | */ |
| 59 | 59 | public function detail() |
| 60 | - { |
|
| 61 | - return $this->hasMany(PageDetail::class, 'page_id', 'id'); |
|
| 62 | - } |
|
| 60 | + { |
|
| 61 | + return $this->hasMany(PageDetail::class, 'page_id', 'id'); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * Joins the templates table |
@@ -67,8 +67,8 @@ discard block |
||
| 67 | 67 | * @return \Illuminate\Database\Eloquent\Relations\hasOne |
| 68 | 68 | */ |
| 69 | 69 | public function template() |
| 70 | - { |
|
| 71 | - return $this->hasMany(Template::class, 'id', 'template_id'); |
|
| 72 | - } |
|
| 70 | + { |
|
| 71 | + return $this->hasMany(Template::class, 'id', 'template_id'); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | 74 | } |
@@ -30,7 +30,7 @@ |
||
| 30 | 30 | /** |
| 31 | 31 | * Rules to validate when creating a model |
| 32 | 32 | * |
| 33 | - * @var array |
|
| 33 | + * @var array |
|
| 34 | 34 | */ |
| 35 | 35 | protected static $createRules = [ |
| 36 | 36 | 'first_name' => 'required', |