Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation before model deleting #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 deletions src/HZ/Illuminate/Organizer/Managers/AdminApiController.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php
namespace HZ\Illuminate\Organizer\Managers;

use Validator;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Validator;

abstract class AdminApiController extends ApiController
{
/**
* Repository object
*
*
* @var mixed
*/
protected $repository;

/**
* Controller repository
*
Expand Down Expand Up @@ -43,8 +43,8 @@ abstract class AdminApiController extends ApiController
public function __construct()
{
parent::__construct();
if (! empty($this->controllerInfo['repository'])) {

if (!empty($this->controllerInfo['repository'])) {
$this->repository = repo($this->controllerInfo['repository']);
}
}
Expand Down Expand Up @@ -72,7 +72,7 @@ protected function listOptions(Request $request): array
{
$listOptions = $this->controllerInfo('listOptions');

if (! empty($listOptions['filterBy'])) {
if (!empty($listOptions['filterBy'])) {
$filterByValues = $request->only($listOptions['filterBy']);
$listOptions = array_merge($listOptions, $filterByValues);
unset($listOptions['filterBy']);
Expand Down Expand Up @@ -105,8 +105,8 @@ public function show($id, Request $request)

/**
* Get value from controller info
*
* @param string $key
*
* @param string $key
* @return mixed
*/
protected function controllerInfo(string $key)
Expand Down Expand Up @@ -151,6 +151,18 @@ public function store(Request $request)
*/
public function destroy($id)
{
$deletingValidationErrors = [];

if ($this->repository->deleteHasDependence()) {
$deleteDependenceTables = $this->repository->deleteGetDependence();

$deletingValidationErrors = $this->validateBeforeDeleting($deleteDependenceTables, $id);
}

if (!empty($deletingValidationErrors)) {
return $this->badRequest($deletingValidationErrors);
}

$this->repository->delete((int) $id);

$response = [
Expand All @@ -160,6 +172,46 @@ public function destroy($id)
return $this->success($response);
}

/**
* Validate if this model has depended on another these tables .
*
* @param array $deleteDependenceTables
* @param int $modelId
* @return array $errors
*/
public function validateBeforeDeleting($deleteDependenceTables, $modelId): array
{
$errors = [];

$isSoftDelete = $this->repository->isSoftDeleteUsed();

foreach ($deleteDependenceTables as $table) {
$validationRules =
Rule::unique($table['tableName'], $table['key'])->where(function ($query) use ($isSoftDelete) {
if ($isSoftDelete) {
$query->whereNull('deleted_at');
}
});

$validator = Validator::make(
[
'id' => (int) $modelId,
],
[
'id' => $validationRules,
],
[
'unique' => $table['message'],
]
);

if ($validator->fails()) {
$errors[] = $validator->errors();
}
}
return $errors;
}

/**
* Update the specified resource in storage.
*
Expand Down Expand Up @@ -217,4 +269,4 @@ protected function storeValidation($request): array
{
return (array) $this->controllerInfo('rules.store');
}
}
}
Loading