Describe entities as API Platform resources
This commit is contained in:
@@ -2,38 +2,60 @@
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use ApiPlatform\Metadata;
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use App\Repository\PostRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ORM\Entity(repositoryClass: PostRepository::class)]
|
||||
#[ORM\HasLifecycleCallbacks]
|
||||
#[ApiResource(
|
||||
operations: [new Metadata\Post(), new Metadata\Get(), new Metadata\Put(), new Metadata\Delete(), new Metadata\Patch()],
|
||||
normalizationContext: ['groups' => ['post:collection:read', 'post:read']],
|
||||
)]
|
||||
#[GetCollection(normalizationContext: ['groups' => ['post:collection:read']])]
|
||||
class Post
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
#[Groups(['post:collection:read'])]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column]
|
||||
#[Groups(['post:collection:read'])]
|
||||
private ?\DateTimeImmutable $foundDate = null;
|
||||
|
||||
#[ORM\Column]
|
||||
#[ApiProperty(writable: false)]
|
||||
#[Groups(['post:collection:read'])]
|
||||
private ?\DateTimeImmutable $publicationDate = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
#[Groups(['post:collection:read'])]
|
||||
private ?float $latitude = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
#[Groups(['post:collection:read'])]
|
||||
private ?float $longitude = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
#[Groups(['post:collection:read'])]
|
||||
private ?float $altitude = null;
|
||||
|
||||
#[ORM\Column(type: Types::TEXT)]
|
||||
#[Groups(['post:read'])]
|
||||
private ?string $commentary = null;
|
||||
|
||||
#[ORM\ManyToOne(inversedBy: 'posts')]
|
||||
#[ApiProperty(readableLink: false)]
|
||||
#[Groups(['post:collection:read'])]
|
||||
private ?Species $species = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
@@ -122,4 +144,11 @@ class Post
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
#[ORM\PrePersist]
|
||||
#[ORM\PreUpdate]
|
||||
public function setPublicationDateValue(): void
|
||||
{
|
||||
$this->publicationDate = new \DateTimeImmutable();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user