diff --git a/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantLogic.php b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantLogic.php index dac04ec7..b7186064 100644 --- a/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantLogic.php +++ b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantLogic.php @@ -76,7 +76,7 @@ class UpdateConstantLogic extends AbstractControllerLogic public function logic(Request $request) : JsonResponse { - $object = new ConstantObject($request->input('name'), $request->input('reference'), $request->input('detail')); + $object = new ConstantObject($request->input('reference'), $request->input('value')); $segment = $this->fetchesSegment->execute(['id' => $request->route('id')]); diff --git a/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantPostcodeLogic.php b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantPostcodeLogic.php new file mode 100644 index 00000000..82edc1a2 --- /dev/null +++ b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantPostcodeLogic.php @@ -0,0 +1,99 @@ + 'Updated Segment Constant', + 'message' => 'You have successfully updated the Segment Constant' + ]; + } + + /** @var CanUpdateConstant */ + private $canUpdateConstant; + + /** @var UpdatesConstant */ + private $updatesConstant; + + /** @var FetchesSegment */ + private $fetchesSegment; + + /** @var FetchesConstant */ + private $fetchesConstant; + + /** @var AddItemToConstantValueArray */ + private $addItemToConstantValueArray; + + /** @var RemoveItemFromConstantValueArray */ + private $removeItemFromConstantValueArray; + + + /** + * UpdateConstantLogic constructor. + * @param CanUpdateConstant $canUpdateConstant + * @param UpdatesConstant $updatesConstant + * @param FetchesSegment $fetchesSegment + * @param FetchesConstant $fetchesConstant + * @param AddItemToConstantValueArray $addItemToConstantValueArray + * @param RemoveItemFromConstantValueArray $removeItemFromConstantValueArray + */ + public function __construct(CanUpdateConstant $canUpdateConstant, UpdatesConstant $updatesConstant, FetchesSegment $fetchesSegment, FetchesConstant $fetchesConstant,AddItemToConstantValueArray $addItemToConstantValueArray, RemoveItemFromConstantValueArray $removeItemFromConstantValueArray) + { + $this->canUpdateConstant = $canUpdateConstant; + $this->updatesConstant = $updatesConstant; + $this->fetchesSegment = $fetchesSegment; + $this->fetchesConstant = $fetchesConstant; + $this->addItemToConstantValueArray = $addItemToConstantValueArray; + $this->removeItemFromConstantValueArray = $removeItemFromConstantValueArray; + } + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\MalformedRequestException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + public function logic(Request $request) : JsonResponse + { + $segment = $this->fetchesSegment->execute(['id' => $request->route('id')]); + + $constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $request->input('reference')]); + + $object = new ConstantObject($constant->reference, $this->addItemToConstantValueArray->execute($constant->value, $request->input('postcode'))); + $this->canUpdateConstant->passes($object); + + $constant = $this->updatesConstant->execute($constant, $object); + + $oppositeConstant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $request->input('reference') == SegmentConstants::CENTER_POSTCODE ? SegmentConstants::OUTSTATION_POSTCODE : SegmentConstants::CENTER_POSTCODE]); + + $oppositeObject = new ConstantObject($request->input('reference') == SegmentConstants::CENTER_POSTCODE ? SegmentConstants::OUTSTATION_POSTCODE : SegmentConstants::CENTER_POSTCODE, $this->removeItemFromConstantValueArray->execute($oppositeConstant->value, $request->input('postcode'))); + + $oppositeConstant = $this->updatesConstant->execute($oppositeConstant, $oppositeObject); + + + return $this->response([ + 'CENTER_POSTCODE' => $constant->reference == SegmentConstants::CENTER_POSTCODE ? new ConstantResource($constant) : New ConstantResource($oppositeConstant), + 'OUTSTATION_POSTCODE' => $constant->reference == SegmentConstants::OUTSTATION_POSTCODE ? new ConstantResource($constant) : New ConstantResource($oppositeConstant), + ]); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantStateLogic.php b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantStateLogic.php new file mode 100644 index 00000000..6dca08a6 --- /dev/null +++ b/app/Classes/Modules/Segments/ControllersLogic/UpdateConstantStateLogic.php @@ -0,0 +1,87 @@ + 'Updated Segment Constant', + 'message' => 'You have successfully updated the Segment Constant' + ]; + } + + /** @var CanUpdateConstant */ + private $canUpdateConstant; + + /** @var UpdatesConstant */ + private $updatesConstant; + + /** @var FetchesSegment */ + private $fetchesSegment; + + /** @var FetchesConstant */ + private $fetchesConstant; + + /** @var UpdatesConstantValueByState */ + private $updatesConstantValueByState; + + + /** + * UpdateConstantLogic constructor. + * @param CanUpdateConstant $canUpdateConstant + * @param UpdatesConstant $updatesConstant + * @param FetchesSegment $fetchesSegment + * @param FetchesConstant $fetchesConstant + * @param UpdatesConstantValueByState $updatesConstantValueByState + */ + public function __construct(CanUpdateConstant $canUpdateConstant, UpdatesConstant $updatesConstant, FetchesSegment $fetchesSegment, FetchesConstant $fetchesConstant,UpdatesConstantValueByState $updatesConstantValueByState) + { + $this->canUpdateConstant = $canUpdateConstant; + $this->updatesConstant = $updatesConstant; + $this->fetchesSegment = $fetchesSegment; + $this->fetchesConstant = $fetchesConstant; + $this->updatesConstantValueByState = $updatesConstantValueByState; + } + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\MalformedRequestException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + public function logic(Request $request) : JsonResponse + { + $segment = $this->fetchesSegment->execute(['id' => $request->route('id')]); + + $constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => SegmentConstants::STATE_RATE]); + + $constantValue = $this->updatesConstantValueByState->execute($constant->value, $request->input('status_id'), $request->input('rate')); + + $object = new ConstantObject(SegmentConstants::STATE_RATE, (array) $constantValue); + $this->canUpdateConstant->passes($object); + + $constant = $this->updatesConstant->execute($constant, $object); + + + return $this->resourceResponse(new ConstantResource($constant)); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Segments/DataTransferObjects/ConstantObject.php b/app/Classes/Modules/Segments/DataTransferObjects/ConstantObject.php index d5373a68..ebd5d2e4 100644 --- a/app/Classes/Modules/Segments/DataTransferObjects/ConstantObject.php +++ b/app/Classes/Modules/Segments/DataTransferObjects/ConstantObject.php @@ -7,34 +7,22 @@ use App\Classes\General\Interfaces\DataTransferObject; class ConstantObject implements DataTransferObject { - /** @var string */ - private $name; - /** @var string */ private $reference; /** @var array */ - private $detail; + private $value; /** * ConstantObject constructor. * @param string $name * @param string $reference - * @param array $detail + * @param array $value */ - public function __construct(string $name, string $reference, array $detail) + public function __construct(string $reference, array $value) { - $this->name = $name; $this->reference = $reference; - $this->detail = $detail; - } - - /** - * @return string - */ - public function getName(): string - { - return $this->name; + $this->value = $value; } /** @@ -48,9 +36,9 @@ class ConstantObject implements DataTransferObject /** * @return array */ - public function getDetail(): array + public function getValue(): array { - return $this->detail; + return $this->value; } diff --git a/app/Classes/Modules/Segments/Services/AddItemToConstantValueArray.php b/app/Classes/Modules/Segments/Services/AddItemToConstantValueArray.php new file mode 100644 index 00000000..af6d053b --- /dev/null +++ b/app/Classes/Modules/Segments/Services/AddItemToConstantValueArray.php @@ -0,0 +1,19 @@ +reference === SegmentConstants::SUPPLIER_CURRENCIES) { - return property_exists($constant->detail, 'id') ? new CurrencyResource($this->fetchesCurrency->execute(['id' => $constant->detail->id])) : ''; - } + // if($constant->reference === SegmentConstants::SUPPLIER_CURRENCIES) { + // return property_exists($constant->detail, 'id') ? new CurrencyResource($this->fetchesCurrency->execute(['id' => $constant->detail->id])) : ''; + // } - return $constant->detail; + return $constant->value; } diff --git a/app/Classes/Modules/Segments/Services/CreatesConstant.php b/app/Classes/Modules/Segments/Services/CreatesConstant.php index b2addae8..48c1d018 100644 --- a/app/Classes/Modules/Segments/Services/CreatesConstant.php +++ b/app/Classes/Modules/Segments/Services/CreatesConstant.php @@ -18,9 +18,8 @@ class CreatesConstant extends AbstractUpdateRelationshipRecord public function execute(Segment $segment, ConstantObject $object) { $model = new SegmentConstant(); - $model->name = $object->getName(); $model->reference = $object->getReference(); - $model->detail = json_encode($object->getDetail()); + $model->value = json_encode($object->getValue()); return $this->handler($segment->constants(), $model); } diff --git a/app/Classes/Modules/Segments/Services/RemoveItemFromConstantValueArray.php b/app/Classes/Modules/Segments/Services/RemoveItemFromConstantValueArray.php new file mode 100644 index 00000000..9eec46e5 --- /dev/null +++ b/app/Classes/Modules/Segments/Services/RemoveItemFromConstantValueArray.php @@ -0,0 +1,22 @@ + $value){ + if($array[$key] == $item){ + unset($array[$key]); + } + } + return array_values($array); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Segments/Services/UpdatesConstant.php b/app/Classes/Modules/Segments/Services/UpdatesConstant.php index 13fb22c1..b8e8ec18 100644 --- a/app/Classes/Modules/Segments/Services/UpdatesConstant.php +++ b/app/Classes/Modules/Segments/Services/UpdatesConstant.php @@ -17,9 +17,8 @@ class UpdatesConstant extends AbstractUpdateRecord */ public function execute(SegmentConstant $model, ConstantObject $object) { - $model->name = $object->getName(); $model->reference = $object->getReference(); - $model->detail = json_encode($object->getDetail()); + $model->value = json_encode($object->getValue()); return $this->handler($model); } diff --git a/app/Classes/Modules/Segments/Services/UpdatesConstantValueByState.php b/app/Classes/Modules/Segments/Services/UpdatesConstantValueByState.php new file mode 100644 index 00000000..ae8728fa --- /dev/null +++ b/app/Classes/Modules/Segments/Services/UpdatesConstantValueByState.php @@ -0,0 +1,32 @@ +config as $key => $object){ + if($object->status_id == $status_id){ + $newArray->config[$key] = (object) [ + 'rate' => $rate, + 'status_id' => $object->status_id, + 'outstation_rate' => $object->outstation_rate, + ]; + }else{ + $newArray->config[$key] = $object; + } + } + return $newArray; + + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Segments/Standards/Validators/ConstantValidation.php b/app/Classes/Modules/Segments/Standards/Validators/ConstantValidation.php index d5ac8819..eb66adb5 100644 --- a/app/Classes/Modules/Segments/Standards/Validators/ConstantValidation.php +++ b/app/Classes/Modules/Segments/Standards/Validators/ConstantValidation.php @@ -15,9 +15,8 @@ class ConstantValidation extends AbstractValidation protected function data($object): array { $data = [ - 'name' => $object->getName(), 'reference' => $object->getReference(), - 'detail' => $object->getDetail() + 'value' => $object->getValue() ]; return $data; @@ -28,9 +27,8 @@ class ConstantValidation extends AbstractValidation */ protected function rules(): array { return [ - 'name' => 'required', 'reference' => 'required', - 'detail' => 'required' + 'value' => 'required' ]; } diff --git a/app/Http/Controllers/Segments/UpdateConstantPostcodeController.php b/app/Http/Controllers/Segments/UpdateConstantPostcodeController.php new file mode 100644 index 00000000..5d0098ed --- /dev/null +++ b/app/Http/Controllers/Segments/UpdateConstantPostcodeController.php @@ -0,0 +1,20 @@ +execute($request); + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/Segments/UpdateConstantStateController.php b/app/Http/Controllers/Segments/UpdateConstantStateController.php new file mode 100644 index 00000000..33e52100 --- /dev/null +++ b/app/Http/Controllers/Segments/UpdateConstantStateController.php @@ -0,0 +1,20 @@ +execute($request); + } + +} \ No newline at end of file diff --git a/app/Http/Resources/ConstantResource.php b/app/Http/Resources/ConstantResource.php index c6bcf344..aba82526 100644 --- a/app/Http/Resources/ConstantResource.php +++ b/app/Http/Resources/ConstantResource.php @@ -19,9 +19,8 @@ class ConstantResource extends JsonResource { return [ 'id' => $this->id, - 'name' => $this->name, 'reference' => $this->reference, - 'detail' => (App()->make(ConvertsConstantDetailsToResource::class))->execute($this->resource) + 'value' => (App()->make(ConvertsConstantDetailsToResource::class))->execute($this->resource) ]; } } diff --git a/routes/segment.php b/routes/segment.php index ada2b84d..d4406d71 100644 --- a/routes/segment.php +++ b/routes/segment.php @@ -13,5 +13,7 @@ Route::group(['prefix' => 'segment', 'as' => 'segment.', 'namespace' => 'Segment Route::put('/service/update', 'UpdateCustomServiceConstantController@update')->name('service.update'); Route::put('/update', 'UpdateConstantController@update')->name('update'); Route::get('/show/{reference}', 'FetchConstantController@fetch')->name('show'); + Route::put('/update/state', 'UpdateConstantStateController@update')->name('update.state'); + Route::put('/update/postcode', 'UpdateConstantPostcodeController@update')->name('update.postcode'); }); }); \ No newline at end of file