Update Accounting api resource

This commit is contained in:
2024-12-09 10:20:58 +01:00
parent 68e3dd3e28
commit a9675e607f
2 changed files with 32 additions and 14 deletions
@@ -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.
@@ -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;
}
}