diff --git a/src/ApiResource/Project/RewardClaimApiResource.php b/src/ApiResource/Project/RewardClaimApiResource.php index 153eabb..f9c1d38 100644 --- a/src/ApiResource/Project/RewardClaimApiResource.php +++ b/src/ApiResource/Project/RewardClaimApiResource.php @@ -6,8 +6,8 @@ use ApiPlatform\Metadata as API; use App\ApiResource\User\UserApiResource; use App\Entity\Project\RewardClaim; -use App\State\ApiResourceStateProcessor; use App\State\ApiResourceStateProvider; +use App\State\Project\RewardClaimStateProcessor; use Symfony\Component\Validator\Constraints as Assert; /** @@ -17,7 +17,7 @@ shortName: 'ProjectRewardClaim', stateOptions: new Options(entityClass: RewardClaim::class), provider: ApiResourceStateProvider::class, - processor: ApiResourceStateProcessor::class + processor: RewardClaimStateProcessor::class )] class RewardClaimApiResource { diff --git a/src/Entity/Project/RewardClaim.php b/src/Entity/Project/RewardClaim.php index 4dc14fd..1da5454 100644 --- a/src/Entity/Project/RewardClaim.php +++ b/src/Entity/Project/RewardClaim.php @@ -4,9 +4,12 @@ use App\Entity\Interface\UserOwnedInterface; use App\Entity\User\User; +use App\Mapping\Provider\EntityMapProvider; use App\Repository\Project\RewardClaimRepository; +use AutoMapper\Attribute\MapProvider; use Doctrine\ORM\Mapping as ORM; +#[MapProvider(EntityMapProvider::class)] #[ORM\Entity(repositoryClass: RewardClaimRepository::class)] class RewardClaim implements UserOwnedInterface { diff --git a/src/Service/Project/RewardService.php b/src/Service/Project/RewardService.php new file mode 100644 index 0000000..1db9240 --- /dev/null +++ b/src/Service/Project/RewardService.php @@ -0,0 +1,36 @@ +getReward(); + + if (!$reward->hasUnits()) { + return $claim; + } + + $available = $reward->getUnitsAvailable(); + + if ($available < 1) { + throw new \Exception("The claimed Reward has no units available"); + } + + $reward->addClaim($claim); + $reward->setUnitsAvailable($available - 1); + + return $claim->setReward($reward); + } +} diff --git a/src/State/Project/RewardClaimStateProcessor.php b/src/State/Project/RewardClaimStateProcessor.php new file mode 100644 index 0000000..d3c5a94 --- /dev/null +++ b/src/State/Project/RewardClaimStateProcessor.php @@ -0,0 +1,49 @@ +autoMapper->map($data, RewardClaim::class); + + if (!$claim->getId()) { + $owner = $this->authService->getUser(); + + if (!$owner) { + throw new AuthenticationException(); + } + + $claim->setOwner($owner); + } + + $claim = $this->rewardService->processClaim($claim); + $claim = $this->entityStateProcessor->process($claim, $operation, $uriVariables, $context); + + return $this->autoMapper->map($claim, $data); + } +}