Skip to content

Commit 9251c36

Browse files
committed
Stored bulk return
1 parent b7e49c5 commit 9251c36

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
lines changed

src/Repositories/Repository.php

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public static function newModel(): Model
211211

212212
public static function query(RestifyRequest $request)
213213
{
214-
if (! $request->isViaRepository()) {
214+
if (!$request->isViaRepository()) {
215215
return static::newModel()->query();
216216
}
217217

@@ -276,8 +276,8 @@ public function collectFields(RestifyRequest $request)
276276

277277
if ($this instanceof Mergeable) {
278278
$fillable = collect($this->resource->getFillable())
279-
->filter(fn ($attribute) => $fields->contains('attribute', $attribute) === false)
280-
->map(fn ($attribute) => Field::new($attribute));
279+
->filter(fn($attribute) => $fields->contains('attribute', $attribute) === false)
280+
->map(fn($attribute) => Field::new($attribute));
281281

282282
$fields = $fields->merge($fillable);
283283
}
@@ -288,14 +288,14 @@ public function collectFields(RestifyRequest $request)
288288
private function indexFields(RestifyRequest $request): Collection
289289
{
290290
return $this->collectFields($request)
291-
->filter(fn (Field $field) => ! $field->isHiddenOnIndex($request, $this))
291+
->filter(fn(Field $field) => !$field->isHiddenOnIndex($request, $this))
292292
->values();
293293
}
294294

295295
private function showFields(RestifyRequest $request): Collection
296296
{
297297
return $this->collectFields($request)
298-
->filter(fn (Field $field) => ! $field->isHiddenOnShow($request, $this))
298+
->filter(fn(Field $field) => !$field->isHiddenOnShow($request, $this))
299299
->values();
300300
}
301301

@@ -409,10 +409,10 @@ public static function routes(Router $router, $attributes, $wrap = false)
409409
public function resolveShowAttributes(RestifyRequest $request)
410410
{
411411
$fields = $this->showFields($request)
412-
->filter(fn (Field $field) => $field->authorize($request))
413-
->each(fn (Field $field) => $field->resolveForShow($this))
414-
->map(fn (Field $field) => $field->serializeToValue($request))
415-
->mapWithKeys(fn ($value) => $value)
412+
->filter(fn(Field $field) => $field->authorize($request))
413+
->each(fn(Field $field) => $field->resolveForShow($this))
414+
->map(fn(Field $field) => $field->serializeToValue($request))
415+
->mapWithKeys(fn($value) => $value)
416416
->all();
417417

418418
if ($this instanceof Mergeable) {
@@ -430,7 +430,7 @@ public function resolveShowAttributes(RestifyRequest $request)
430430
return false;
431431
}
432432

433-
if (! $field->authorize($request)) {
433+
if (!$field->authorize($request)) {
434434
return false;
435435
}
436436

@@ -451,10 +451,10 @@ public function resolveIndexAttributes($request)
451451
{
452452
// Resolve the show method, and attach the value to the array
453453
$fields = $this->indexFields($request)
454-
->filter(fn (Field $field) => $field->authorize($request))
455-
->each(fn (Field $field) => $field->resolveForIndex($this))
456-
->map(fn (Field $field) => $field->serializeToValue($request))
457-
->mapWithKeys(fn ($value) => $value)
454+
->filter(fn(Field $field) => $field->authorize($request))
455+
->each(fn(Field $field) => $field->resolveForIndex($this))
456+
->map(fn(Field $field) => $field->serializeToValue($request))
457+
->mapWithKeys(fn($value) => $value)
458458
->all();
459459

460460
if ($this instanceof Mergeable) {
@@ -472,7 +472,7 @@ public function resolveIndexAttributes($request)
472472
return false;
473473
}
474474

475-
if (! $field->authorize($request)) {
475+
if (!$field->authorize($request)) {
476476
return false;
477477
}
478478

@@ -518,7 +518,7 @@ public function resolveRelationships($request): array
518518
/** * @var AbstractPaginator $paginator */
519519
$paginator = $this->resource->{$relation}()->take($request->input('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE))->get();
520520

521-
$withs[$relation] = $paginator->map(fn (Model $item) => [
521+
$withs[$relation] = $paginator->map(fn(Model $item) => [
522522
'attributes' => $item->toArray(),
523523
]);
524524
}
@@ -574,10 +574,10 @@ public function index(RestifyRequest $request)
574574

575575
return $this->response([
576576
'meta' => $this->resolveIndexMainMeta(
577-
$request, $items->map(fn (self $repository) => $repository->resource), RepositoryCollection::meta($paginator->toArray())
577+
$request, $items->map(fn(self $repository) => $repository->resource), RepositoryCollection::meta($paginator->toArray())
578578
) ?? RepositoryCollection::meta($paginator->toArray()),
579579
'links' => RepositoryCollection::paginationLinks($paginator->toArray()),
580-
'data' => $items->map(fn (self $repository) => $repository->serializeForIndex($request)),
580+
'data' => $items->map(fn(self $repository) => $repository->serializeForIndex($request)),
581581
]);
582582
}
583583

@@ -609,7 +609,7 @@ public function store(RestifyRequest $request)
609609
$this->resource->save();
610610
}
611611

612-
$this->storeFields($request)->each(fn (Field $field) => $field->invokeAfter($request, $this->resource));
612+
$this->storeFields($request)->each(fn(Field $field) => $field->invokeAfter($request, $this->resource));
613613
});
614614

615615
static::stored($this->resource, $request);
@@ -633,7 +633,7 @@ public function storeBulk(RepositoryStoreBulkRequest $request)
633633

634634
$this->resource->save();
635635

636-
$this->storeBulkFields($request)->each(fn (Field $field) => $field->invokeAfter($request, $this->resource));
636+
$this->storeBulkFields($request)->each(fn(Field $field) => $field->invokeAfter($request, $this->resource));
637637

638638
return $this->resource;
639639
});
@@ -642,7 +642,8 @@ public function storeBulk(RepositoryStoreBulkRequest $request)
642642
static::storedBulk($entities, $request);
643643

644644
return $this->response()
645-
->created();
645+
->data($entities)
646+
->success();
646647
}
647648

648649
public function update(RestifyRequest $request, $repositoryId)
@@ -670,15 +671,17 @@ public function updateBulk(RestifyRequest $request, $repositoryId, int $row)
670671

671672
$this->resource->save();
672673

674+
static::updatedBulk($this->resource, $request);
675+
673676
return $this->response()
674677
->success();
675678
}
676679

677680
public function attach(RestifyRequest $request, $repositoryId, Collection $pivots)
678681
{
679682
DB::transaction(function () use ($request, $pivots) {
680-
return $pivots->map(fn ($pivot) => $pivot->forceFill($request->except($request->relatedRepository)))
681-
->map(fn ($pivot) => $pivot->save());
683+
return $pivots->map(fn($pivot) => $pivot->forceFill($request->except($request->relatedRepository)))
684+
->map(fn($pivot) => $pivot->save());
682685
});
683686

684687
return $this->response()
@@ -689,7 +692,7 @@ public function attach(RestifyRequest $request, $repositoryId, Collection $pivot
689692
public function detach(RestifyRequest $request, $repositoryId, Collection $pivots)
690693
{
691694
$deleted = DB::transaction(function () use ($request, $pivots) {
692-
return $pivots->map(fn ($pivot) => $pivot->delete());
695+
return $pivots->map(fn($pivot) => $pivot->delete());
693696
});
694697

695698
return $this->response()
@@ -776,6 +779,11 @@ public static function storedBulk(Collection $repositories, $request)
776779
//
777780
}
778781

782+
public static function updatedBulk($model, $request)
783+
{
784+
//
785+
}
786+
779787
public static function updated($model, $request)
780788
{
781789
//
@@ -794,7 +802,7 @@ public function response($content = '', $status = 200, array $headers = []): Res
794802
public function serializeForShow(RestifyRequest $request): array
795803
{
796804
return $this->filter([
797-
'id' => $this->when($this->resource->id, fn () => $this->getShowId($request)),
805+
'id' => $this->when($this->resource->id, fn() => $this->getShowId($request)),
798806
'type' => $this->when($type = $this->getType($request), $type),
799807
'attributes' => $request->isShowRequest() ? $this->resolveShowAttributes($request) : $this->resolveIndexAttributes($request),
800808
'relationships' => $this->when(value($related = $this->resolveRelationships($request)), $related),
@@ -807,7 +815,7 @@ public function serializeForIndex(RestifyRequest $request): array
807815
return $this->filter([
808816
'id' => $this->when($id = $this->getShowId($request), $id),
809817
'type' => $this->when($type = $this->getType($request), $type),
810-
'attributes' => $this->when((bool) $attrs = $this->resolveIndexAttributes($request), $attrs),
818+
'attributes' => $this->when((bool)$attrs = $this->resolveIndexAttributes($request), $attrs),
811819
'relationships' => $this->when(value($related = $this->resolveRelationships($request)), $related),
812820
'meta' => $this->when(value($meta = $this->resolveIndexMeta($request)), $meta),
813821
]);
@@ -857,12 +865,12 @@ protected static function fillBulkFields(RestifyRequest $request, Model $model,
857865

858866
public static function uriTo(Model $model)
859867
{
860-
return Restify::path().'/'.static::uriKey().'/'.$model->getKey();
868+
return Restify::path() . '/' . static::uriKey() . '/' . $model->getKey();
861869
}
862870

863871
public function availableFilters(RestifyRequest $request)
864872
{
865-
return collect($this->filter($this->filters($request)))->each(fn (Filter $filter) => $filter->authorizedToSee($request))
873+
return collect($this->filter($this->filters($request)))->each(fn(Filter $filter) => $filter->authorizedToSee($request))
866874
->values();
867875
}
868876

tests/Controllers/RepositoryStoreBulkControllerTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function test_user_can_bulk_create_posts()
4444
{
4545
$user = $this->mockUsers()->first();
4646

47-
$this->postJson('/restify-api/posts/bulk', [
47+
$r = $this->postJson('/restify-api/posts/bulk', [
4848
[
4949
'user_id' => $user->id,
5050
'title' => 'First post.',
@@ -54,8 +54,7 @@ public function test_user_can_bulk_create_posts()
5454
'title' => 'Second post.',
5555
],
5656
])
57-
->dump()
58-
->assertStatus(201);
57+
->assertSuccessful();
5958

6059
$this->assertDatabaseCount('posts', 2);
6160
}

0 commit comments

Comments
 (0)