Skip to content

Commit 7ad9d79

Browse files
author
Steeven Andrian
committed
restful bugs fixing
1 parent ecf2ec8 commit 7ad9d79

File tree

1 file changed

+125
-141
lines changed

1 file changed

+125
-141
lines changed

src/Http/Controllers/Restful.php

+125-141
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,6 @@ class Restful extends Controller
156156
*/
157157
public $model;
158158

159-
/**
160-
* Restful::$getParams
161-
*
162-
* @var array
163-
*/
164-
public $getParams = [];
165-
166159
/**
167160
* Restful::$getValidationRules
168161
*
@@ -387,7 +380,7 @@ public function index()
387380
unset($_GET[ 'search' ]);
388381
}
389382

390-
if (count($this->getParams)) {
383+
if (empty($this->getValidationRules)) {
391384
if ($get = input()->get()) {
392385
if (false !== ($result = $this->model->findWhere($get->getArrayCopy(), $limit))) {
393386
if ($result->count()) {
@@ -398,39 +391,25 @@ public function index()
398391
} else {
399392
$this->sendError(204);
400393
}
394+
} elseif (false !== ($result = $this->model->all())) {
395+
$this->sendPayload($result);
401396
} else {
402-
$this->sendError(400, 'Get parameters cannot be empty!');
397+
$this->sendError(204);
403398
}
404-
} elseif (count($this->getValidationRules)) {
405-
if ($get = input()->get()) {
406-
$get->validation($this->getValidationRules, $this->getValidationCustomErrors);
399+
} elseif ($get = input()->get()) {
400+
$get->validation($this->getValidationRules, $this->getValidationCustomErrors);
407401

408-
if ( ! $get->validate()) {
409-
$this->sendError(400, implode(', ', $get->validator->getErrors()));
410-
} else {
411-
$conditions = [];
402+
if ( ! $get->validate()) {
403+
$this->sendError(400, implode(', ', $get->validator->getErrors()));
404+
}
412405

413-
foreach ($this->getValidationRules as $field => $rule) {
414-
if ($get->offsetExists($field)) {
415-
$conditions[ $field ] = $get->offsetGet($field);
416-
}
417-
}
406+
$conditions = [];
418407

419-
if (false !== ($result = $this->model->findWhere($conditions, $limit))) {
420-
if ($result->count()) {
421-
$this->sendPayload($result);
422-
} else {
423-
$this->sendError(204);
424-
}
425-
} else {
426-
$this->sendError(204);
427-
}
428-
}
429-
} else {
430-
$this->sendError(400, 'Get parameters cannot be empty!');
408+
foreach ($this->getValidationRules as $field => $rule) {
409+
$conditions[ $field ] = $get->offsetGet($field);
431410
}
432-
} elseif ($get = input()->get()) {
433-
if (false !== ($result = $this->model->findWhere($get->getArrayCopy(), $limit))) {
411+
412+
if (false !== ($result = $this->model->findWhere($conditions, $limit))) {
434413
if ($result->count()) {
435414
$this->sendPayload($result);
436415
} else {
@@ -439,12 +418,10 @@ public function index()
439418
} else {
440419
$this->sendError(204);
441420
}
421+
} elseif (false !== ($result = $this->model->all())) {
422+
$this->sendPayload($result);
442423
} else {
443-
if (false !== ($result = $this->model->all())) {
444-
$this->sendPayload($result);
445-
} else {
446-
$this->sendError(204);
447-
}
424+
$this->sendError(204);
448425
}
449426
}
450427
}
@@ -463,10 +440,6 @@ public function datatable()
463440
$this->sendError(503, 'Model is not exists!');
464441
}
465442

466-
if ( ! $this->model instanceof Model) {
467-
$this->sendError(503, 'Model is not exists!');
468-
}
469-
470443
$hasAction = false;
471444
if ($request = input()->request()) {
472445
// Start as limit
@@ -744,7 +717,8 @@ public function delete($id = null)
744717
];
745718
} else {
746719
foreach ($this->model->primaryKeys as $primaryKey) {
747-
$conditions = [$primaryKey => $post->offsetGet($primaryKey)];
720+
$conditions[ $primaryKey ] = $post->offsetGet($primaryKey);
721+
748722
$this->actionValidationRules[ $primaryKey ] = 'required';
749723
$this->actionValidationCustomErrors[ $primaryKey ] = [
750724
'required' => language('LABEL_' . strtoupper($primaryKey)) . ' cannot be empty!',
@@ -774,7 +748,7 @@ public function delete($id = null)
774748
$this->sendError(501, 'Unavailable primary keys data');
775749
}
776750

777-
if ( ! $post->validate($conditions)) {
751+
if ( ! $post->validate()) {
778752
$this->sendError(400, implode(', ', $post->validator->getErrors()));
779753
}
780754

@@ -882,20 +856,17 @@ public function delete($id = null)
882856
*/
883857
private function updateRecordStatus(array $params, $method)
884858
{
885-
if ($post = input()->post()) {
886-
if (empty($this->actionValidationRules)) {
887-
if (empty($this->model->primaryKeys)) {
888-
$primaryKey = empty($this->model->primaryKey) ? 'id' : $this->model->primaryKey;
889-
if ($post->offsetExists($primaryKey)) {
890-
$conditions = [$primaryKey => $post->offsetGet($primaryKey)];
891-
}
859+
if (empty($this->model)) {
860+
output()->sendError(204);
861+
} else {
862+
if ( ! $this->model instanceof Model) {
863+
$this->sendError(503, 'Model is not ready');
864+
}
892865

893-
$this->actionValidationRules[ $primaryKey ] = 'required';
894-
$this->actionValidationCustomErrors[ $primaryKey ] = [
895-
'required' => language('LABEL_' . strtoupper($primaryKey)) . ' cannot be empty!',
896-
];
897-
} else {
898-
foreach ($this->model->primaryKeys as $primaryKey) {
866+
if ($post = input()->post()) {
867+
if (empty($this->actionValidationRules)) {
868+
if (empty($this->model->primaryKeys)) {
869+
$primaryKey = empty($this->model->primaryKey) ? 'id' : $this->model->primaryKey;
899870
if ($post->offsetExists($primaryKey)) {
900871
$conditions = [$primaryKey => $post->offsetGet($primaryKey)];
901872
}
@@ -904,115 +875,126 @@ private function updateRecordStatus(array $params, $method)
904875
$this->actionValidationCustomErrors[ $primaryKey ] = [
905876
'required' => language('LABEL_' . strtoupper($primaryKey)) . ' cannot be empty!',
906877
];
878+
} else {
879+
foreach ($this->model->primaryKeys as $primaryKey) {
880+
if ($post->offsetExists($primaryKey)) {
881+
$conditions[ $primaryKey ] = $post->offsetGet($primaryKey);
882+
}
883+
884+
$this->actionValidationRules[ $primaryKey ] = 'required';
885+
$this->actionValidationCustomErrors[ $primaryKey ] = [
886+
'required' => language('LABEL_' . strtoupper($primaryKey)) . ' cannot be empty!',
887+
];
888+
}
907889
}
908890
}
909-
}
910891

911-
if (count($this->actionValidationRules)) {
912-
$post->validation($this->actionValidationRules, $this->actionValidationCustomErrors);
913-
}
892+
if (count($this->actionValidationRules)) {
893+
$post->validation($this->actionValidationRules, $this->actionValidationCustomErrors);
894+
}
914895

915-
if (empty($conditions)) {
916-
$this->sendError(501, 'Unavailable primary keys data');
917-
}
896+
if (empty($conditions)) {
897+
$this->sendError(501, 'Unavailable primary keys data');
898+
}
918899

919-
if ( ! $post->validate($conditions)) {
920-
$this->sendError(400, implode(', ', $post->validator->getErrors()));
921-
}
900+
if ( ! $post->validate()) {
901+
$this->sendError(400, implode(', ', $post->validator->getErrors()));
902+
}
903+
904+
if ($result = $this->model->findWhere($conditions)) {
905+
if ($result instanceof Row) {
906+
if ($row->{$method}()) {
907+
$this->sendError(200);
908+
} else {
909+
$this->sendError(501);
910+
}
911+
} elseif ($result instanceof Result) {
912+
foreach ($result as $row) {
913+
$row->{$method}();
914+
}
922915

923-
if ($result = $this->model->findWhere($conditions)) {
924-
if ($result instanceof Row) {
925-
if ($row->{$method}()) {
926916
$this->sendError(200);
927-
} else {
928-
$this->sendError(501);
929-
}
930-
} elseif ($result instanceof Result) {
931-
foreach ($result as $row) {
932-
$row->{$method}();
933917
}
934-
935-
$this->sendError(200);
918+
} else {
919+
$this->sendError(404, 'Data not found!');
936920
}
937-
} else {
938-
$this->sendError(404, 'Data not found!');
939-
}
940-
} elseif (input()->server('REQUEST_METHOD') === 'PATCH') {
941-
$validator = new Validator();
942-
943-
if (empty($this->actionValidationRules)) {
944-
if (empty($this->model->primaryKeys)) {
945-
$primaryKey = empty($this->model->primaryKey) ? 'id' : $this->model->primaryKey;
946-
if (count($params)) {
947-
$conditions = [$primaryKey => reset($params)];
948-
}
921+
} elseif (input()->server('REQUEST_METHOD') === 'PATCH') {
922+
$validator = new Validator();
949923

950-
$this->actionValidationRules[ $primaryKey ] = 'required';
951-
$this->actionValidationCustomErrors[ $primaryKey ] = [
952-
'required' => language('LABEL_' . strtoupper($primaryKey)) . ' cannot be empty!',
953-
];
954-
} else {
955-
foreach ($this->model->primaryKeys as $key => $primaryKey) {
956-
if (isset($params[ $key ])) {
957-
$conditions[ $primaryKey ] = $params[ $key ];
924+
if (empty($this->actionValidationRules)) {
925+
if (empty($this->model->primaryKeys)) {
926+
$primaryKey = empty($this->model->primaryKey) ? 'id' : $this->model->primaryKey;
927+
if (count($params)) {
928+
$conditions = [$primaryKey => reset($params)];
958929
}
959930

960931
$this->actionValidationRules[ $primaryKey ] = 'required';
961932
$this->actionValidationCustomErrors[ $primaryKey ] = [
962933
'required' => language('LABEL_' . strtoupper($primaryKey)) . ' cannot be empty!',
963934
];
964-
}
965-
}
966-
} else {
967-
if (empty($this->model->primaryKeys)) {
968-
$primaryKey = empty($this->model->primaryKey) ? 'id' : $this->model->primaryKey;
969-
if (count($params)) {
970-
$conditions = [$primaryKey => reset($params)];
935+
} else {
936+
foreach ($this->model->primaryKeys as $key => $primaryKey) {
937+
if (isset($params[ $key ])) {
938+
$conditions[ $primaryKey ] = $params[ $key ];
939+
}
940+
941+
$this->actionValidationRules[ $primaryKey ] = 'required';
942+
$this->actionValidationCustomErrors[ $primaryKey ] = [
943+
'required' => language('LABEL_' . strtoupper($primaryKey)) . ' cannot be empty!',
944+
];
945+
}
971946
}
972947
} else {
973-
foreach ($this->model->primaryKeys as $key => $primaryKey) {
974-
if (isset($params[ $key ])) {
975-
$conditions[ $primaryKey ] = $params[ $key ];
948+
if (empty($this->model->primaryKeys)) {
949+
$primaryKey = empty($this->model->primaryKey) ? 'id' : $this->model->primaryKey;
950+
if (count($params)) {
951+
$conditions = [$primaryKey => reset($params)];
952+
}
953+
} else {
954+
foreach ($this->model->primaryKeys as $key => $primaryKey) {
955+
if (isset($params[ $key ])) {
956+
$conditions[ $primaryKey ] = $params[ $key ];
957+
}
976958
}
977959
}
978960
}
979-
}
980961

981-
if (count($this->actionValidationRules)) {
982-
$validator->setRules($this->actionValidationRules, $this->actionValidationCustomErrors);
983-
}
962+
if (count($this->actionValidationRules)) {
963+
$validator->setRules($this->actionValidationRules, $this->actionValidationCustomErrors);
964+
}
984965

985-
if (empty($conditions)) {
986-
$this->sendError(501, 'Unavailable primary keys data');
987-
}
966+
if (empty($conditions)) {
967+
$this->sendError(501, 'Unavailable primary keys data');
968+
}
988969

989-
if ( ! $validator->validate($conditions)) {
990-
$this->sendError(400, implode(', ', $validator->getErrors()));
991-
}
970+
if ( ! $validator->validate($conditions)) {
971+
$this->sendError(400, implode(', ', $validator->getErrors()));
972+
}
992973

993-
if ( ! $this->model instanceof Model) {
994-
$this->sendError(503, 'Model is not ready!');
995-
}
974+
if ( ! $this->model instanceof Model) {
975+
$this->sendError(503, 'Model is not ready!');
976+
}
977+
978+
if ($result = $this->model->findWhere($conditions)) {
979+
if ($result instanceof Row) {
980+
if ($row->{$method}()) {
981+
$this->sendError(200);
982+
} else {
983+
$this->sendError(501);
984+
}
985+
} elseif ($result instanceof Result) {
986+
foreach ($result as $row) {
987+
$row->{$method}();
988+
}
996989

997-
if ($result = $this->model->findWhere($conditions)) {
998-
if ($result instanceof Row) {
999-
if ($row->{$method}()) {
1000990
$this->sendError(200);
1001-
} else {
1002-
$this->sendError(501);
1003991
}
1004-
} elseif ($result instanceof Result) {
1005-
foreach ($result as $row) {
1006-
$row->{$method}();
1007-
}
1008-
1009-
$this->sendError(200);
992+
} else {
993+
$this->sendError(404, 'Data not found!');
1010994
}
1011995
} else {
1012-
$this->sendError(404, 'Data not found!');
996+
$this->sendError(400);
1013997
}
1014-
} else {
1015-
$this->sendError(400);
1016998
}
1017999
}
10181000

@@ -1030,7 +1012,7 @@ public function publish($id = null)
10301012
$this->updateRecordStatus(func_get_args(), 'publish');
10311013
}
10321014

1033-
// ------------------------------------------------------------------------
1015+
// ------------------------------------------------------------------------
10341016

10351017
/**
10361018
* Restful::unpublish
@@ -1044,7 +1026,7 @@ public function unpublish($id = null)
10441026
$this->updateRecordStatus(func_get_args(), 'unpublish');
10451027
}
10461028

1047-
// ------------------------------------------------------------------------
1029+
// ------------------------------------------------------------------------
10481030

10491031
/**
10501032
* Restful::archive
@@ -1053,8 +1035,10 @@ public function unpublish($id = null)
10531035
*
10541036
* @throws OutOfRangeException
10551037
*/
1056-
public function archive($id = null)
1057-
{
1038+
public
1039+
function archive(
1040+
$id = null
1041+
) {
10581042
$this->updateRecordStatus(func_get_args(), 'archive');
10591043
}
10601044

0 commit comments

Comments
 (0)