Add project custom api resource
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\ApiResource\Project;
|
||||
|
||||
use ApiPlatform\Doctrine\Orm\State\Options;
|
||||
use ApiPlatform\Metadata as API;
|
||||
use App\ApiResource\Accounting\Accounting;
|
||||
use App\Entity\Project as Entity;
|
||||
use App\Entity\Project\ProjectStatus;
|
||||
use App\Entity\User;
|
||||
use App\State\Project\ProjectStateProvider;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
/**
|
||||
* A Project represents a User-led crowdfunding event that is to be discovered and supported by the wider community.
|
||||
*/
|
||||
#[API\ApiResource(
|
||||
stateOptions: new Options(entityClass: Entity\Project::class),
|
||||
provider: ProjectStateProvider::class
|
||||
)]
|
||||
class Project
|
||||
{
|
||||
#[API\ApiProperty(identifier: true, writable: false)]
|
||||
public int $id;
|
||||
|
||||
/**
|
||||
* The User who launched this Project.
|
||||
*/
|
||||
#[API\ApiProperty(writable: false)]
|
||||
public User $owner;
|
||||
|
||||
/**
|
||||
* The Accounting processing the funds involved in this Project.
|
||||
*/
|
||||
#[API\ApiProperty(writable: false)]
|
||||
public Accounting $accounting;
|
||||
|
||||
/**
|
||||
* Main title for this Project.
|
||||
*/
|
||||
#[Assert\NotBlank()]
|
||||
public string $title;
|
||||
|
||||
/**
|
||||
* The current status of this Project.\
|
||||
* Admin-only property.
|
||||
*/
|
||||
#[API\ApiProperty(security: 'is_granted("ROLE_ADMIN")')]
|
||||
public ProjectStatus $status = ProjectStatus::InEditing;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\State\Project;
|
||||
|
||||
use ApiPlatform\Doctrine\Orm\State\CollectionProvider;
|
||||
use ApiPlatform\Metadata\CollectionOperationInterface;
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\Pagination\TraversablePaginator;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\Project as Resource;
|
||||
use AutoMapper\AutoMapperInterface;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
|
||||
class ProjectStateProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private AutoMapperInterface $autoMapper,
|
||||
#[Autowire(service: CollectionProvider::class)]
|
||||
private ProviderInterface $collectionProvider
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
||||
{
|
||||
if ($operation instanceof CollectionOperationInterface) {
|
||||
$collection = $this->collectionProvider->provide($operation, $uriVariables, $context);
|
||||
|
||||
$resources = [];
|
||||
foreach ($collection as $entity) {
|
||||
$project = $this->autoMapper->map($entity, Resource\Project::class);
|
||||
|
||||
var_dump($project);
|
||||
exit;
|
||||
}
|
||||
|
||||
return new TraversablePaginator(
|
||||
new ArrayCollection($resources),
|
||||
$collection->getCurrentPage(),
|
||||
$collection->getItemsPerPage(),
|
||||
$collection->getTotalItems()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user