Skip to content

Commit

Permalink
feat(core): repositories (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzarbn committed Oct 11, 2023
1 parent e23589f commit 7ea91b9
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 62 deletions.
30 changes: 23 additions & 7 deletions src/Concerns/HandlesRelationStandardBatchOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ protected function batchStoreWithTransaction(Request $request, int|string $paren
/** @var Model $entity */
$entity = new $resourceModelClass;

$this->beforeStore($request, $parentEntity, $entity);
$this->beforeSave($request, $parentEntity, $entity);
$this->beforeStore($request, $parentEntity, $entity, $resource);
$this->repositoryInstance->beforeStore($entity, $resource);

$this->beforeSave($request, $parentEntity, $entity, $resource);
$this->repositoryInstance->beforeSave($entity, $resource);

$this->performStore(
$request,
Expand All @@ -94,7 +97,10 @@ protected function batchStoreWithTransaction(Request $request, int|string $paren
$entity = $this->castPivotJsonFields($entity);
}

$this->repositoryInstance->afterSave($entity);
$this->afterSave($request, $parentEntity, $entity);

$this->repositoryInstance->afterStore($entity);
$this->afterStore($request, $parentEntity, $entity);

$entities->push($entity);
Expand Down Expand Up @@ -210,17 +216,20 @@ protected function batchUpdateWithTransaction(Request $request, int|string $pare
/** @var Model $entity */
$this->authorize($this->resolveAbility('update'), [$entity, $parentEntity]);

$resource = $this->retrieve($request, "resources.{$entity->{$this->keyName()}}");
$attributes = $this->retrieve($request, "resources.{$entity->{$this->keyName()}}");

$this->beforeUpdate($request, $parentEntity, $entity, $attributes);
$this->repositoryInstance->beforeUpdate($entity, $attributes);

$this->beforeUpdate($request, $parentEntity, $entity);
$this->beforeSave($request, $parentEntity, $entity);
$this->beforeSave($request, $parentEntity, $entity, $attributes);
$this->repositoryInstance->beforeSave($entity, $attributes);

$this->performUpdate(
$request,
$parentEntity,
$entity,
$resource,
Arr::get($resource, 'pivot', [])
$attributes,
Arr::get($attributes, 'pivot', [])
);

$entity = $this->refreshUpdatedEntity(
Expand All @@ -235,7 +244,10 @@ protected function batchUpdateWithTransaction(Request $request, int|string $pare
$entity = $this->castPivotJsonFields($entity);
}

$this->repositoryInstance->afterSave($entity);
$this->afterSave($request, $parentEntity, $entity);

$this->repositoryInstance->afterUpdate($entity);
$this->afterUpdate($request, $parentEntity, $entity);
}

Expand Down Expand Up @@ -411,6 +423,7 @@ protected function batchDestroyWithTransaction(Request $request, int|string $par
$this->authorize($this->resolveAbility($forceDeletes ? 'forceDelete' : 'delete'), [$entity, $parentEntity]);

$this->beforeDestroy($request, $parentEntity, $entity);
$this->repositoryInstance->beforeDestroy($entity, $forceDeletes);

if (!$forceDeletes) {
$this->performDestroy($entity);
Expand All @@ -434,6 +447,7 @@ protected function batchDestroyWithTransaction(Request $request, int|string $par
$entity = $this->castPivotJsonFields($entity);
}

$this->repositoryInstance->afterDestroy($entity);
$this->afterDestroy($request, $parentEntity, $entity);
}

Expand Down Expand Up @@ -579,6 +593,7 @@ protected function batchRestoreWithTransaction(Request $request, int|string $par
$this->authorize($this->resolveAbility('restore'), [$entity, $parentEntity]);

$this->beforeRestore($request, $parentEntity, $entity);
$this->repositoryInstance->beforeRestore($entity);

$this->performRestore($entity);

Expand All @@ -596,6 +611,7 @@ protected function batchRestoreWithTransaction(Request $request, int|string $par
$entity = $this->castPivotJsonFields($entity);
}

$this->repositoryInstance->afterRestore($entity);
$this->afterRestore($request, $parentEntity, $entity);
}

Expand Down
Loading

0 comments on commit 7ea91b9

Please sign in to comment.