79 lines
2.4 KiB
PHP
79 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Entity;
|
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
|
|
use ApiPlatform\Doctrine\Orm\Filter\NumericFilter;
|
|
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
|
|
use ApiPlatform\Doctrine\Orm\Filter\RangeFilter;
|
|
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
|
|
use ApiPlatform\Metadata\ApiFilter;
|
|
use ApiPlatform\Metadata\ApiProperty;
|
|
use ApiPlatform\Metadata\ApiResource;
|
|
use ApiPlatform\Metadata\Get;
|
|
use ApiPlatform\Metadata\GetCollection;
|
|
use ApiPlatform\Metadata\Link;
|
|
use ApiPlatform\Metadata\Post;
|
|
use Doctrine\DBAL\Types\Types;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Symfony\Component\Serializer\Attribute\Groups;
|
|
|
|
#[ORM\Entity]
|
|
#[ApiResource(
|
|
shortName: 'Livre',
|
|
operations: [
|
|
new GetCollection(),
|
|
new Get(),
|
|
new Post(security: "is_granted('ROLE_ADMIN')"),
|
|
],
|
|
normalizationContext: ['groups' => ['book:read']],
|
|
denormalizationContext: ['groups' => ['book:write']],
|
|
paginationItemsPerPage: 10,
|
|
)]
|
|
#[ApiResource(
|
|
uriTemplate: '/authors/{authorId}/books',
|
|
uriVariables: ['authorId' => new Link(fromClass: Author::class, toProperty: 'author')],
|
|
operations: [new GetCollection()],
|
|
)]
|
|
#[ApiFilter(OrderFilter::class, properties: ['title', 'pages'])]
|
|
class Book
|
|
{
|
|
#[ORM\Id]
|
|
#[ORM\GeneratedValue]
|
|
#[ORM\Column]
|
|
private ?int $id = null;
|
|
|
|
#[ORM\Column(length: 255)]
|
|
#[Groups(['book:read', 'book:write', 'search'])]
|
|
#[ApiFilter(SearchFilter::class, strategy: 'start')]
|
|
#[ApiFilter(OrderFilter::class)]
|
|
private ?string $title = null;
|
|
|
|
#[ORM\Column]
|
|
#[Groups(['book:read'])]
|
|
#[ApiFilter(NumericFilter::class)]
|
|
#[ApiProperty(description: 'Number of pages', example: '300', writable: false)]
|
|
private ?int $pages = null;
|
|
|
|
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2)]
|
|
#[Groups(['book:read'])]
|
|
private ?string $price = null;
|
|
|
|
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
|
|
#[Groups(['book:read', 'book:write'])]
|
|
#[ApiFilter(DateFilter::class)]
|
|
private ?\DateTimeImmutable $publishedAt = null;
|
|
|
|
#[ORM\Column(enumType: Genre::class)]
|
|
#[Groups(['book:read', 'book:write'])]
|
|
private ?Genre $genre = null;
|
|
|
|
#[ORM\ManyToOne(inversedBy: 'books')]
|
|
#[Groups(['book:read', 'book:write'])]
|
|
private ?Author $author = null;
|
|
|
|
#[ORM\Column(length: 255, nullable: true)]
|
|
#[Groups(['book:write'])]
|
|
#[ApiProperty(readable: false)]
|
|
private ?string $internalNote = null;
|
|
}
|