Skip to content

Commit 590ba9c

Browse files
authored
[Storybook] Improve Storybook examples (#600)
* storybook: add Book and Review entities * storybook: improve entities attributes * storybook: add story using Field and Input Guessers * storybook: add Advanced Customization story * storybook: add custom menu icons to the guessers story
1 parent 7904b32 commit 590ba9c

File tree

6 files changed

+549
-53
lines changed

6 files changed

+549
-53
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20250306152729 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('CREATE TABLE book (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, isbn VARCHAR(255) DEFAULT NULL, title VARCHAR(255) NOT NULL, description TEXT NOT NULL, author VARCHAR(255) NOT NULL, publication_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))');
24+
$this->addSql('CREATE TABLE review (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, rating SMALLINT NOT NULL, body TEXT NOT NULL, author VARCHAR(255) NOT NULL, publication_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, book_id INT DEFAULT NULL, PRIMARY KEY(id))');
25+
$this->addSql('CREATE INDEX IDX_794381C616A2B381 ON review (book_id)');
26+
$this->addSql('ALTER TABLE review ADD CONSTRAINT FK_794381C616A2B381 FOREIGN KEY (book_id) REFERENCES book (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
27+
}
28+
29+
public function down(Schema $schema): void
30+
{
31+
// this down() migration is auto-generated, please modify it to your needs
32+
$this->addSql('CREATE SCHEMA public');
33+
$this->addSql('ALTER TABLE review DROP CONSTRAINT FK_794381C616A2B381');
34+
$this->addSql('DROP TABLE book');
35+
$this->addSql('DROP TABLE review');
36+
}
37+
}

api/src/Entity/Book.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use ApiPlatform\Metadata\ApiResource;
6+
use ApiPlatform\Metadata\ApiProperty;
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Symfony\Component\Validator\Constraints as Assert;
9+
use Doctrine\Common\Collections\ArrayCollection;
10+
use ApiPlatform\Metadata\ApiFilter;
11+
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
12+
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
13+
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
14+
15+
16+
#[ApiResource(mercure: true)]
17+
#[ORM\Entity]
18+
#[ApiFilter(OrderFilter::class, properties: [
19+
'id' => 'ASC',
20+
'isbn' => 'ASC',
21+
'title' => 'ASC',
22+
'author' => 'ASC',
23+
'publicationDate' => 'DESC'
24+
])]
25+
#[ApiFilter(SearchFilter::class, properties: [
26+
'id' => 'exact',
27+
'title' => 'ipartial',
28+
'author' => 'ipartial'
29+
])]
30+
#[ApiFilter(DateFilter::class, properties: ['publicationDate'])]
31+
class Book
32+
{
33+
/** The ID of this book */
34+
#[ORM\Id]
35+
#[ORM\Column]
36+
#[ORM\GeneratedValue]
37+
private ?int $id = null;
38+
39+
/** The ISBN of this book (or null if doesn't have one) */
40+
#[ORM\Column(nullable: true)]
41+
public ?string $isbn = null;
42+
43+
/** The title of this book */
44+
#[ORM\Column]
45+
#[Assert\NotBlank]
46+
#[ApiProperty(iris: ['http://schema.org/name'])]
47+
public string $title = '';
48+
49+
/** The description of this book */
50+
#[ORM\Column(type: 'text')]
51+
#[Assert\NotBlank]
52+
public string $description = '';
53+
54+
/** The author of this book */
55+
#[ORM\Column]
56+
#[Assert\NotBlank]
57+
public string $author = '';
58+
59+
/** The publication date of this book */
60+
#[ORM\Column]
61+
#[Assert\NotNull]
62+
public ?\DateTimeImmutable $publicationDate = null;
63+
64+
/** @var Review[] Available reviews for this book */
65+
#[ORM\OneToMany(mappedBy: 'book', targetEntity: Review::class, cascade: ['persist', 'remove'])]
66+
public iterable $reviews;
67+
68+
public function __construct()
69+
{
70+
$this->reviews = new ArrayCollection();
71+
}
72+
73+
public function getId(): ?int
74+
{
75+
return $this->id;
76+
}
77+
}

api/src/Entity/Review.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use ApiPlatform\Metadata\ApiResource;
6+
use ApiPlatform\Metadata\ApiProperty;
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Symfony\Component\Validator\Constraints as Assert;
9+
use ApiPlatform\Metadata\ApiFilter;
10+
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
11+
use ApiPlatform\Doctrine\Orm\Filter\NumericFilter;
12+
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
13+
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
14+
15+
16+
#[ApiResource(mercure: true)]
17+
#[ORM\Entity]
18+
#[ApiFilter(OrderFilter::class, properties: [
19+
'id' => 'ASC',
20+
'rating' => 'ASC',
21+
'author' => 'ASC',
22+
'publicationDate' => 'DESC'
23+
])]
24+
#[ApiFilter(SearchFilter::class, properties: [
25+
'id' => 'exact',
26+
'body' => 'ipartial',
27+
'author' => 'ipartial'
28+
])]
29+
#[ApiFilter(NumericFilter::class, properties: ['rating'])]
30+
#[ApiFilter(DateFilter::class, properties: ['publicationDate'])]
31+
class Review
32+
{
33+
/** The ID of this review */
34+
#[ORM\Id]
35+
#[ORM\Column]
36+
#[ORM\GeneratedValue]
37+
private ?int $id = null;
38+
39+
/** The rating of this review (between 0 and 5) */
40+
#[ORM\Column(type: 'smallint')]
41+
#[Assert\Range(min: 0, max: 5)]
42+
public int $rating = 0;
43+
44+
/** The body of this review */
45+
#[ORM\Column(type: 'text')]
46+
#[Assert\NotBlank]
47+
public string $body = '';
48+
49+
/** The author of this review */
50+
#[ORM\Column]
51+
#[Assert\NotBlank]
52+
public string $author = '';
53+
54+
/** The publication date of this review */
55+
#[ORM\Column]
56+
#[Assert\NotNull]
57+
#[ApiProperty(iris: ['http://schema.org/name'])]
58+
public ?\DateTimeImmutable $publicationDate = null;
59+
60+
/** The book this review is about */
61+
#[ORM\ManyToOne(inversedBy: 'reviews')]
62+
#[Assert\NotNull]
63+
public ?Book $book = null;
64+
65+
public function getId(): ?int
66+
{
67+
return $this->id;
68+
}
69+
}

src/stories/Custom.stories.tsx

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)