| @@ 82-130 (lines=49) @@ | ||
| 79 | * @param Request $request |
|
| 80 | * @return JsonResponse |
|
| 81 | */ |
|
| 82 | public function create(Request $request) |
|
| 83 | { |
|
| 84 | $instance = $this->resolveModel(); |
|
| 85 | $connection = $instance->getConnection(); |
|
| 86 | $affected = []; |
|
| 87 | $errors = []; |
|
| 88 | ||
| 89 | $connection->beginTransaction(); |
|
| 90 | foreach ($request->get('data') as $data) { |
|
| 91 | $validator = $this->getValidationFactory() |
|
| 92 | ->make($data, $this->createRules(), $this->messages() + $this->createMessages(), $this->attributes()); |
|
| 93 | if ($validator->fails()) { |
|
| 94 | foreach ($this->formatErrors($validator) as $error) { |
|
| 95 | $errors[] = $error; |
|
| 96 | } |
|
| 97 | ||
| 98 | continue; |
|
| 99 | } |
|
| 100 | ||
| 101 | if (method_exists($this, 'creating')) { |
|
| 102 | $data = $this->creating($instance, $data); |
|
| 103 | } |
|
| 104 | ||
| 105 | if (method_exists($this, 'saving')) { |
|
| 106 | $data = $this->saving($instance, $data); |
|
| 107 | } |
|
| 108 | ||
| 109 | $instance->fill($data)->save(); |
|
| 110 | ||
| 111 | if (method_exists($this, 'created')) { |
|
| 112 | $instance = $this->created($instance, $data); |
|
| 113 | } |
|
| 114 | ||
| 115 | if (method_exists($this, 'saved')) { |
|
| 116 | $instance = $this->saved($instance, $data); |
|
| 117 | } |
|
| 118 | ||
| 119 | $instance->setAttribute('DT_RowId', $instance->getKey()); |
|
| 120 | $affected[] = $instance; |
|
| 121 | } |
|
| 122 | ||
| 123 | if (! $errors) { |
|
| 124 | $connection->commit(); |
|
| 125 | } else { |
|
| 126 | $connection->rollBack(); |
|
| 127 | } |
|
| 128 | ||
| 129 | return $this->toJson($affected, $errors); |
|
| 130 | } |
|
| 131 | ||
| 132 | /** |
|
| 133 | * Resolve model to used. |
|
| @@ 217-265 (lines=49) @@ | ||
| 214 | * @param Request $request |
|
| 215 | * @return JsonResponse |
|
| 216 | */ |
|
| 217 | public function edit(Request $request) |
|
| 218 | { |
|
| 219 | $connection = $this->getBuilder()->getConnection(); |
|
| 220 | $affected = []; |
|
| 221 | $errors = []; |
|
| 222 | ||
| 223 | $connection->beginTransaction(); |
|
| 224 | foreach ($request->get('data') as $key => $data) { |
|
| 225 | $model = $this->getBuilder()->findOrFail($key); |
|
| 226 | $validator = $this->getValidationFactory() |
|
| 227 | ->make($data, $this->editRules($model), $this->messages() + $this->editMessages(), $this->attributes()); |
|
| 228 | if ($validator->fails()) { |
|
| 229 | foreach ($this->formatErrors($validator) as $error) { |
|
| 230 | $errors[] = $error; |
|
| 231 | } |
|
| 232 | ||
| 233 | continue; |
|
| 234 | } |
|
| 235 | ||
| 236 | if (method_exists($this, 'updating')) { |
|
| 237 | $data = $this->updating($model, $data); |
|
| 238 | } |
|
| 239 | ||
| 240 | if (method_exists($this, 'saving')) { |
|
| 241 | $data = $this->saving($model, $data); |
|
| 242 | } |
|
| 243 | ||
| 244 | $model->fill($data)->save(); |
|
| 245 | ||
| 246 | if (method_exists($this, 'updated')) { |
|
| 247 | $model = $this->updated($model, $data); |
|
| 248 | } |
|
| 249 | ||
| 250 | if (method_exists($this, 'saved')) { |
|
| 251 | $model = $this->saved($model, $data); |
|
| 252 | } |
|
| 253 | ||
| 254 | $model->setAttribute('DT_RowId', $model->getKey()); |
|
| 255 | $affected[] = $model; |
|
| 256 | } |
|
| 257 | ||
| 258 | if (! $errors) { |
|
| 259 | $connection->commit(); |
|
| 260 | } else { |
|
| 261 | $connection->rollBack(); |
|
| 262 | } |
|
| 263 | ||
| 264 | return $this->toJson($affected, $errors); |
|
| 265 | } |
|
| 266 | ||
| 267 | /** |
|
| 268 | * Get elqouent builder of the model. |
|