update state charges

This commit is contained in:
edmondlang
2022-04-19 13:28:56 +08:00
parent 1814352780
commit 7b88fd5c3c
5 changed files with 65 additions and 38 deletions
@@ -11,9 +11,11 @@ use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Segments\Services\FetchesConstant;
use App\Classes\Modules\Segments\Services\UpdatesConstant;
use App\Classes\Modules\Segments\Services\CreatesConstant;
use App\Classes\Modules\Segments\Standards\Rules\CanUpdateConstant;
use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
use App\Classes\Modules\Segments\Services\UpdatesConstantValueByState;
use App\Classes\Exceptions\ResourceNotFoundException;
class UpdateConstantStateLogic extends AbstractControllerLogic
{
@@ -37,6 +39,9 @@ class UpdateConstantStateLogic extends AbstractControllerLogic
/** @var FetchesSegment */
private $fetchesSegment;
/** @var CreatesConstant */
private $createsConstant;
/** @var FetchesConstant */
private $fetchesConstant;
@@ -51,14 +56,23 @@ class UpdateConstantStateLogic extends AbstractControllerLogic
* @param FetchesSegment $fetchesSegment
* @param FetchesConstant $fetchesConstant
* @param UpdatesConstantValueByState $updatesConstantValueByState
* @param CreatesConstant $createsConstant
*/
public function __construct(CanUpdateConstant $canUpdateConstant, UpdatesConstant $updatesConstant, FetchesSegment $fetchesSegment, FetchesConstant $fetchesConstant,UpdatesConstantValueByState $updatesConstantValueByState)
public function __construct(
CanUpdateConstant $canUpdateConstant,
UpdatesConstant $updatesConstant,
FetchesSegment $fetchesSegment,
FetchesConstant $fetchesConstant,
UpdatesConstantValueByState $updatesConstantValueByState,
CreatesConstant $createsConstant
)
{
$this->canUpdateConstant = $canUpdateConstant;
$this->updatesConstant = $updatesConstant;
$this->fetchesSegment = $fetchesSegment;
$this->fetchesConstant = $fetchesConstant;
$this->updatesConstantValueByState = $updatesConstantValueByState;
$this->createsConstant = $createsConstant;
}
/**
* @param Request $request
@@ -69,18 +83,35 @@ class UpdateConstantStateLogic extends AbstractControllerLogic
*/
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'));
try {
$object = new ConstantObject(SegmentConstants::STATE_RATE, (array) $constantValue);
$this->canUpdateConstant->passes($object);
$constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => SegmentConstants::STATE_RATE]);
$constantValue = $this->updatesConstantValueByState->execute($constant->value, $request->input('id'), $request->input('rate'));
$object = new ConstantObject(SegmentConstants::STATE_RATE, $constantValue);
$this->canUpdateConstant->passes($object);
} catch (ResourceNotFoundException $exception){
$constantObject = [];
$constantObject[$request->input('id')] = $request->input('rate');
$object = new ConstantObject(
SegmentConstants::STATE_RATE,
$constantObject
);
$constant = $this->createsConstant->execute($segment, $object);
}
$constant = $this->updatesConstant->execute($constant, $object);
return $this->resourceResponse(new ConstantResource($constant));
}