From a9675e607fd762bc26f23adf63dcecbc40b43ce4 Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Mon, 9 Dec 2024 10:20:58 +0100 Subject: [PATCH] Update Accounting api resource --- .../Accounting/AccountingApiResource.php | 6 +-- .../Accounting/AccountingStateProvider.php | 40 ++++++++++++++----- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/ApiResource/Accounting/AccountingApiResource.php b/src/ApiResource/Accounting/AccountingApiResource.php index ef9ed39..0e84bee 100644 --- a/src/ApiResource/Accounting/AccountingApiResource.php +++ b/src/ApiResource/Accounting/AccountingApiResource.php @@ -37,11 +37,9 @@ class AccountingApiResource public string $currency; /** - * Accountings can be owned by different resources.\ - * Full-body owner data is not directly embedded, - * instead a middle object with context about the owner is given. + * The resource owning this Accounting.\ */ - public AccountingOwner $owner; + public mixed $owner; /** * The money currently held by the Accounting. diff --git a/src/State/Accounting/AccountingStateProvider.php b/src/State/Accounting/AccountingStateProvider.php index 3982a52..f81b62c 100644 --- a/src/State/Accounting/AccountingStateProvider.php +++ b/src/State/Accounting/AccountingStateProvider.php @@ -2,15 +2,21 @@ namespace App\State\Accounting; +use ApiPlatform\Api\IriConverterInterface; use ApiPlatform\Doctrine\Orm\State\CollectionProvider; use ApiPlatform\Doctrine\Orm\State\ItemProvider; use ApiPlatform\Metadata\CollectionOperationInterface; use ApiPlatform\Metadata\Operation; use ApiPlatform\State\Pagination\TraversablePaginator; use ApiPlatform\State\ProviderInterface; -use App\ApiResource\Accounting as ApiResource; -use App\Entity\Accounting as Entity; -use App\Mapping\Accounting\AccountingMapper; +use App\ApiResource\Accounting\AccountingApiResource; +use App\ApiResource\Accounting\AccountingOwner; +use App\ApiResource\Project\ProjectApiResource; +use App\Entity\Accounting\Accounting; +use App\Entity\Project\Project; +use App\Entity\User\User; +use App\Mapping\AutoMapper; +use App\Service\AccountingService; use Symfony\Component\DependencyInjection\Attribute\Autowire; class AccountingStateProvider implements ProviderInterface @@ -20,7 +26,9 @@ class AccountingStateProvider implements ProviderInterface private ProviderInterface $itemProvider, #[Autowire(service: CollectionProvider::class)] private ProviderInterface $collectionProvider, - private AccountingMapper $accountingMapper, + private AutoMapper $autoMapper, + private AccountingService $accountingService, + private IriConverterInterface $iriConverter, ) {} public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null @@ -43,15 +51,27 @@ class AccountingStateProvider implements ProviderInterface $accounting = $this->itemProvider->provide($operation, $uriVariables, $context); - return $this->toResource($accounting); - } - - private function toResource(?Entity\Accounting $accounting): ?ApiResource\Accounting - { if ($accounting === null) { return null; } - return $this->accountingMapper->toResource($accounting); + return $this->toResource($accounting); + } + + private function toResource(Accounting $accounting): AccountingApiResource + { + /** @var AccountingApiResource */ + $resource = $this->autoMapper->map($accounting, AccountingApiResource::class); + $resource->balance = $this->accountingService->calcBalance($accounting); + + $owner = $accounting->getOwner(); + + $resource->owner = $owner; + + if ($owner instanceof Project) { + $resource->owner = $this->autoMapper->map($owner, ProjectApiResource::class); + } + + return $resource; } }