Skip to content

Commit

Permalink
fix: pint
Browse files Browse the repository at this point in the history
  • Loading branch information
yajra committed Mar 14, 2024
1 parent 0b46483 commit 52671f9
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 117 deletions.
104 changes: 44 additions & 60 deletions src/DataTablesEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ abstract class DataTablesEditor
/**
* Process dataTables editor action request.
*
* @param Request $request
* @return JsonResponse|mixed
*
* @throws DataTablesEditorException
Expand All @@ -113,7 +112,7 @@ public function process(Request $request)
return $this->{$this->action}($request);
} catch (Exception $exception) {
$error = config('app.debug')
? '<strong>Server Error:</strong> ' . $exception->getMessage()
? '<strong>Server Error:</strong> '.$exception->getMessage()
: $this->getUseFriendlyErrorMessage();

app('log')->error($exception);
Expand All @@ -133,8 +132,6 @@ protected function getUseFriendlyErrorMessage()
/**
* Display success data in dataTables editor format.
*
* @param array $data
* @param array $errors
* @param string $error
* @return JsonResponse
*/
Expand All @@ -144,16 +141,16 @@ protected function toJson(array $data, array $errors = [], $error = '')

$response = [
'action' => $this->action,
'data' => $data,
'data' => $data,
];

if ($error) {
$code = 422;
$code = 422;
$response['error'] = $error;
}

if ($errors) {
$code = 422;
$code = 422;
$response['fieldErrors'] = $errors;
}

Expand All @@ -163,29 +160,28 @@ protected function toJson(array $data, array $errors = [], $error = '')
/**
* Process create action request.
*
* @param Request $request
* @return JsonResponse
*
* @throws \Exception
*/
public function create(Request $request)
{
$model = $this->resolveModel();
$model = $this->resolveModel();
$connection = $model->getConnection();
$affected = [];
$errors = [];
$affected = [];
$errors = [];

$connection->beginTransaction();
foreach ($request->get('data') as $data) {
$this->currentData = $data;

$instance = $model->newInstance();
$instance = $model->newInstance();
$validator = $this->getValidationFactory()
->make(
$data,
$this->createRules(), $this->messages() + $this->createMessages(),
$this->attributes()
);
->make(
$data,
$this->createRules(), $this->messages() + $this->createMessages(),
$this->attributes()
);
if ($validator->fails()) {
foreach ($this->formatErrors($validator) as $error) {
$errors[] = $error;
Expand Down Expand Up @@ -284,7 +280,6 @@ public function attributes()
}

/**
* @param Validator $validator
* @return array
*/
protected function formatErrors(Validator $validator)
Expand All @@ -293,7 +288,7 @@ protected function formatErrors(Validator $validator)

collect($validator->errors())->each(function ($error, $key) use (&$errors) {
$errors[] = [
'name' => $key,
'name' => $key,
'status' => $error[0],
];
});
Expand All @@ -304,7 +299,6 @@ protected function formatErrors(Validator $validator)
/**
* Process restore action request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function restore(Request $request)
Expand All @@ -317,26 +311,25 @@ public function restore(Request $request)
/**
* Process edit action request.
*
* @param Request $request
* @return JsonResponse
*/
public function edit(Request $request)
{
$connection = $this->getBuilder()->getConnection();
$affected = [];
$errors = [];
$affected = [];
$errors = [];

$connection->beginTransaction();
foreach ($request->get('data') as $key => $data) {
$this->currentData = $data;

$model = $this->getBuilder()->findOrFail($key);
$model = $this->getBuilder()->findOrFail($key);
$validator = $this->getValidationFactory()
->make(
$data,
$this->editRules($model), $this->messages() + $this->editMessages(),
$this->attributes()
);
->make(
$data,
$this->editRules($model), $this->messages() + $this->editMessages(),
$this->attributes()
);
if ($validator->fails()) {
foreach ($this->formatErrors($validator) as $error) {
$errors[] = $error;
Expand Down Expand Up @@ -395,7 +388,6 @@ protected function getBuilder()
/**
* Get edit action validation rules.
*
* @param Model $model
* @return array
*/
public function editRules(Model $model)
Expand All @@ -418,7 +410,6 @@ protected function editMessages()
/**
* Process force delete action request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*
* @throws \Exception
Expand All @@ -433,28 +424,27 @@ public function forceDelete(Request $request)
/**
* Process remove action request.
*
* @param Request $request
* @return JsonResponse
*
* @throws \Exception
*/
public function remove(Request $request)
{
$connection = $this->getBuilder()->getConnection();
$affected = [];
$errors = [];
$affected = [];
$errors = [];

$connection->beginTransaction();
foreach ($request->get('data') as $key => $data) {
$this->currentData = $data;

$model = $this->getBuilder()->findOrFail($key);
$model = $this->getBuilder()->findOrFail($key);
$validator = $this->getValidationFactory()
->make(
$data,
$this->removeRules($model), $this->messages() + $this->removeMessages(),
$this->attributes()
);
->make(
$data,
$this->removeRules($model), $this->messages() + $this->removeMessages(),
$this->attributes()
);
if ($validator->fails()) {
foreach ($this->formatErrors($validator) as $error) {
$errors[] = $error['status'];
Expand Down Expand Up @@ -502,7 +492,6 @@ public function remove(Request $request)
/**
* Get remove action validation rules.
*
* @param Model $model
* @return array
*/
public function removeRules(Model $model)
Expand All @@ -525,8 +514,6 @@ protected function removeMessages()
/**
* Get remove query exception message.
*
* @param QueryException $exception
* @param Model $model
* @return string
*/
protected function removeExceptionMessage(QueryException $exception, Model $model)
Expand Down Expand Up @@ -573,39 +560,38 @@ public function unguard($state = true)
/**
* Handle uploading of file.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function upload(Request $request)
{
$field = $request->input('uploadField');
$field = $request->input('uploadField');
$storage = $this->getDisk();

try {
$rules = $this->uploadRules();
$rules = $this->uploadRules();
$fieldRules = ['upload' => $rules[$field] ?? []];

$this->validate($request, $fieldRules, $this->messages(), $this->attributes());

$uploadedFile = $request->file('upload');
$id = $this->storeUploadedFile($field, $uploadedFile);
$id = $this->storeUploadedFile($field, $uploadedFile);

if (method_exists($this, 'uploaded')) {
$id = $this->uploaded($id);
}

return response()->json([
'action' => $this->action,
'data' => [],
'files' => [
'data' => [],
'files' => [
'files' => [
$id => [
'filename' => $id,
'filename' => $id,
'original_name' => $uploadedFile->getClientOriginalName(),
'size' => $uploadedFile->getSize(),
'directory' => $this->getUploadDirectory(),
'disk' => $this->disk,
'url' => $storage->url($id),
'size' => $uploadedFile->getSize(),
'directory' => $this->getUploadDirectory(),
'disk' => $this->disk,
'url' => $storage->url($id),
],
],
],
Expand All @@ -615,11 +601,11 @@ public function upload(Request $request)
]);
} catch (ValidationException $exception) {
return response()->json([
'action' => $this->action,
'data' => [],
'action' => $this->action,
'data' => [],
'fieldErrors' => [
[
'name' => $field,
'name' => $field,
'status' => str_replace('upload', $field, $exception->errors()['upload'][0]),
],
],
Expand All @@ -639,12 +625,11 @@ public function uploadRules()

/**
* @param string $field
* @param UploadedFile $uploadedFile
* @return string
*/
protected function getUploadedFilename($field, UploadedFile $uploadedFile)
{
return date('Ymd_His') . '_' . $uploadedFile->getClientOriginalName();
return date('Ymd_His').'_'.$uploadedFile->getClientOriginalName();
}

/**
Expand All @@ -665,7 +650,6 @@ protected function getDisk()

/**
* @param string $field
* @param UploadedFile $uploadedFile
* @return false|string
*/
protected function storeUploadedFile($field, UploadedFile $uploadedFile)
Expand Down
Loading

0 comments on commit 52671f9

Please sign in to comment.