diff --git a/api/migrations/Version20230920163933.php b/api/migrations/Version20230920163933.php new file mode 100644 index 0000000..8f3c281 --- /dev/null +++ b/api/migrations/Version20230920163933.php @@ -0,0 +1,47 @@ +addSql('DROP SEQUENCE greeting_id_seq CASCADE'); + $this->addSql('CREATE SEQUENCE book_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE SEQUENCE review_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE book (id INT 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))'); + $this->addSql('COMMENT ON COLUMN book.publication_date IS \'(DC2Type:datetime_immutable)\''); + $this->addSql('CREATE TABLE review (id INT NOT NULL, book_id INT DEFAULT NULL, rating SMALLINT NOT NULL, body TEXT NOT NULL, author VARCHAR(255) NOT NULL, publication_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_794381C616A2B381 ON review (book_id)'); + $this->addSql('COMMENT ON COLUMN review.publication_date IS \'(DC2Type:datetime_immutable)\''); + $this->addSql('ALTER TABLE review ADD CONSTRAINT FK_794381C616A2B381 FOREIGN KEY (book_id) REFERENCES book (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('DROP TABLE greeting'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('DROP SEQUENCE book_id_seq CASCADE'); + $this->addSql('DROP SEQUENCE review_id_seq CASCADE'); + $this->addSql('CREATE SEQUENCE greeting_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE greeting (id INT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id))'); + $this->addSql('ALTER TABLE review DROP CONSTRAINT FK_794381C616A2B381'); + $this->addSql('DROP TABLE book'); + $this->addSql('DROP TABLE review'); + } +} diff --git a/api/src/Entity/Book.php b/api/src/Entity/Book.php index 155982c..850b654 100644 --- a/api/src/Entity/Book.php +++ b/api/src/Entity/Book.php @@ -3,28 +3,44 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiResource; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\ORM\Mapping as ORM; /** A book. */ +#[ORM\Entity] #[ApiResource] class Book { - /** The ID of this book. */ - private ?int $id = null; - /** The ISBN of this book (or null if doesn't have one). */ - public ?string $isbn = null; - /** The title of this book. */ - public string $title = ''; - /** The description of this book. */ - public string $description = ''; - /** The author of this book. */ - public string $author = ''; - /** The publication date of this book. */ - public ?\DateTimeImmutable $publicationDate = null; - /** @var Review[] Available reviews for this book. */ + /** The ID of this book. */ + #[ORM\Id, ORM\Column, ORM\GeneratedValue] + private ?int $id = null; + + /** The ISBN of this book (or null if doesn't have one). */ + #[ORM\Column(nullable: true)] + public ?string $isbn = null; + + /** The title of this book. */ + #[ORM\Column] + public string $title = ''; + + /** The description of this book. */ + #[ORM\Column(type: 'text')] + public string $description = ''; + + /** The author of this book. */ + #[ORM\Column] + public string $author = ''; + + /** The publication date of this book. */ + #[ORM\Column] + public ?\DateTimeImmutable $publicationDate = null; + + /** @var Review[] Available reviews for this book. */ + #[ORM\OneToMany(targetEntity: Review::class, mappedBy: 'book', cascade: ['persist', 'remove'])] public iterable $reviews; public function __construct() { $this->reviews = new ArrayCollection(); } + public function getId(): ?int { return $this->id; diff --git a/api/src/Entity/Review.php b/api/src/Entity/Review.php index bd39a04..d263947 100644 --- a/api/src/Entity/Review.php +++ b/api/src/Entity/Review.php @@ -2,22 +2,36 @@ // api/src/Entity/Review.php namespace App\Entity; use ApiPlatform\Metadata\ApiResource; +use Doctrine\ORM\Mapping as ORM; /** A review of a book. */ +#[ORM\Entity] #[ApiResource] class Review { /** The ID of this review. */ + #[ORM\Id, ORM\Column, ORM\GeneratedValue] private ?int $id = null; + /** The rating of this review (between 0 and 5). */ + #[ORM\Column(type: 'smallint')] public int $rating = 0; + /** The body of the review. */ + #[ORM\Column(type: 'text')] public string $body = ''; + /** The author of the review. */ + #[ORM\Column] public string $author = ''; + /** The date of publication of this review.*/ + #[ORM\Column] public ?\DateTimeImmutable $publicationDate = null; + /** The book this review is about. */ + #[ORM\ManyToOne(inversedBy: 'reviews')] public ?Book $book = null; + public function getId(): ?int { return $this->id; diff --git a/docs/openapi.json b/docs/openapi.json index 8399c09..ad094e7 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -892,8 +892,7 @@ "properties": { "id": { "readOnly": true, - "type": "integer", - "nullable": true + "type": "integer" }, "isbn": { "type": "string", @@ -910,11 +909,14 @@ }, "publicationDate": { "type": "string", - "format": "date-time", - "nullable": true + "format": "date-time" }, "reviews": { - "type": "string" + "type": "array", + "items": { + "type": "string", + "format": "iri-reference" + } } } }, @@ -960,8 +962,7 @@ }, "id": { "readOnly": true, - "type": "integer", - "nullable": true + "type": "integer" }, "isbn": { "type": "string", @@ -978,11 +979,14 @@ }, "publicationDate": { "type": "string", - "format": "date-time", - "nullable": true + "format": "date-time" }, "reviews": { - "type": "string" + "type": "array", + "items": { + "type": "string", + "format": "iri-reference" + } } } }, @@ -993,8 +997,7 @@ "properties": { "id": { "readOnly": true, - "type": "integer", - "nullable": true + "type": "integer" }, "rating": { "type": "integer" @@ -1007,8 +1010,7 @@ }, "publicationDate": { "type": "string", - "format": "date-time", - "nullable": true + "format": "date-time" }, "book": { "type": "string", @@ -1059,8 +1061,7 @@ }, "id": { "readOnly": true, - "type": "integer", - "nullable": true + "type": "integer" }, "rating": { "type": "integer" @@ -1073,8 +1074,7 @@ }, "publicationDate": { "type": "string", - "format": "date-time", - "nullable": true + "format": "date-time" }, "book": { "type": "string",