Skip to content

Commit b171e82

Browse files
author
Lupacescu Eduard
authored
Plain custom CRUD (#104)
* Plain custom CRUD * Apply fixes from StyleCI (#103) * Docs * Apply fixes from StyleCI (#105) * Call plain actions from controller actions in terms of avoiding too many calls in the stack * Apply fixes from StyleCI (#106) * Rename debug into dump * Apply fixes from StyleCI (#107) * Actions hooks * Apply fixes from StyleCI (#108) * Docs * Apply fixes from StyleCI (#109)
1 parent 5185ffc commit b171e82

File tree

8 files changed

+214
-61
lines changed

8 files changed

+214
-61
lines changed

src/Controllers/RestResponse.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Database\Eloquent\Model;
1111
use Illuminate\Http\JsonResponse;
1212
use Illuminate\Support\Arr;
13+
use Illuminate\Support\Facades\App;
1314

1415
/**
1516
* Class RestResponse.
@@ -580,7 +581,7 @@ public function getErrors()
580581
return $this->errors instanceof Arrayable ? $this->errors->toArray() : $this->errors;
581582
}
582583

583-
public function debug(\Exception $exception, $condition)
584+
public function dump(\Exception $exception, $condition)
584585
{
585586
if ($condition) {
586587
$this->debug = true;
@@ -595,6 +596,17 @@ public function debug(\Exception $exception, $condition)
595596
return $this;
596597
}
597598

599+
/**
600+
* Debug the log if the environment is local.
601+
*
602+
* @param \Exception $exception
603+
* @return $this
604+
*/
605+
public function dumpLocal(\Exception $exception)
606+
{
607+
return $this->dump($exception, App::environment('production') === false);
608+
}
609+
598610
/**
599611
* Set the JSON:API format for a single resource.
600612
*

src/Exceptions/RestifyHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function render($request, Exception $exception)
109109
if (App::environment('production') === true) {
110110
$response->addError(__('messages.something_went_wrong'));
111111
} else {
112-
$response->debug($exception, true);
112+
$response->dump($exception, true);
113113
}
114114
$response->error();
115115
}

src/Fields/Field.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public function fillAttribute(RestifyRequest $request, $model)
118118
*/
119119
protected function fillAttributeFromRequest(RestifyRequest $request, $model, $attribute)
120120
{
121-
if ($request->exists($attribute)) {
122-
$value = $request[$attribute];
121+
if ($request->exists($attribute) || $request->get($attribute)) {
122+
$value = $request[$attribute] ?? $request->get($attribute);
123123

124124
$model->{$attribute} = is_callable($this->storeCallback) ? call_user_func($this->storeCallback, $value, $request, $model) : $value;
125125
}

src/Http/Controllers/RepositoryIndexController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public function handle(RestifyRequest $request)
2828
} catch (EntityNotFoundException $e) {
2929
return $this->response()->notFound()
3030
->addError($e->getMessage())
31-
->debug($e, $request->isDev());
31+
->dump($e, $request->isDev());
3232
} catch (UnauthorizedException $e) {
33-
return $this->response()->forbidden()->addError($e->getMessage())->debug($e, $request->isDev());
33+
return $this->response()->forbidden()->addError($e->getMessage())->dump($e, $request->isDev());
3434
} catch (InstanceOfException | \Throwable $e) {
35-
return $this->response()->error()->debug($e, $request->isDev());
35+
return $this->response()->error()->dump($e, $request->isDev());
3636
}
3737
}
3838
}

src/Http/Requests/InteractWithRepositories.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ public function authorize()
3131
/**
3232
* Get the class name of the repository being requested.
3333
*
34+
* @param null $key
3435
* @return Repository
35-
* @throws EntityNotFoundException
36-
* @throws UnauthorizedException
3736
*/
38-
public function repository()
37+
public function repository($key = null)
3938
{
40-
return tap(Restify::repositoryForKey($this->route('repository')), function ($repository) {
39+
return tap(Restify::repositoryForKey($key ?? $this->route('repository')), function ($repository) {
4140
if (is_null($repository)) {
4241
throw new EntityNotFoundException(__('Repository :name not found.', [
4342
'name' => $repository,
@@ -109,50 +108,56 @@ public function isResolvedByRestify()
109108
* As a model it could accept either a model instance, a collection or even paginated collection.
110109
*
111110
* @param $model
111+
* @param null $uriKey
112112
* @return Repository
113113
*/
114-
public function newRepositoryWith($model)
114+
public function newRepositoryWith($model, $uriKey = null)
115115
{
116-
$repository = $this->repository();
116+
$repository = $this->repository($uriKey);
117117

118118
return $repository::resolveWith($model);
119119
}
120120

121121
/**
122122
* Get a new, scopeless query builder for the underlying model.
123123
*
124+
* @param null $uriKey
124125
* @return \Illuminate\Database\Eloquent\Builder
125126
* @throws EntityNotFoundException
126127
* @throws UnauthorizedException
127128
*/
128-
public function newQueryWithoutScopes()
129+
public function newQueryWithoutScopes($uriKey = null)
129130
{
130-
return $this->model()->newQueryWithoutScopes();
131+
return $this->model($uriKey)->newQueryWithoutScopes();
131132
}
132133

133134
/**
134135
* Get a new instance of the underlying model.
135136
*
137+
* @param null $uriKey
136138
* @return \Illuminate\Database\Eloquent\Model
137139
* @throws EntityNotFoundException
138140
* @throws UnauthorizedException
139141
*/
140-
public function model()
142+
public function model($uriKey = null)
141143
{
142-
$repository = $this->repository();
144+
$repository = $this->repository($uriKey);
143145

144146
return $repository::newModel();
145147
}
146148

147149
/**
148150
* Get the query to find the model instance for the request.
149151
*
150-
* @param mixed|null $repositoryId
152+
* @param mixed|null $repositoryId
153+
* @param null $uriKey
151154
* @return \Illuminate\Database\Eloquent\Builder
155+
* @throws EntityNotFoundException
156+
* @throws UnauthorizedException
152157
*/
153-
public function findModelQuery($repositoryId = null)
158+
public function findModelQuery($repositoryId = null, $uriKey = null)
154159
{
155-
return $this->newQueryWithoutScopes()->whereKey(
160+
return $this->newQueryWithoutScopes($uriKey)->whereKey(
156161
$repositoryId ?? request('repositoryId')
157162
);
158163
}

0 commit comments

Comments
 (0)