Skip to content

Commit

Permalink
Finalize installation - enable persistence
Browse files Browse the repository at this point in the history
With this PR, it's now possible to create Books or Reviews with API
for example via admin, or docs 'try it out'

Consequently, OpenAPI schema has some minor changes
(but maybe related version should not be visible on Bump.sh changelog).
  • Loading branch information
Polo2 committed Sep 20, 2023
1 parent aba4611 commit 3722128
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 31 deletions.
47 changes: 47 additions & 0 deletions api/migrations/Version20230920163933.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230920163933 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}
42 changes: 29 additions & 13 deletions api/src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions api/src/Entity/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 18 additions & 18 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -892,8 +892,7 @@
"properties": {
"id": {
"readOnly": true,
"type": "integer",
"nullable": true
"type": "integer"
},
"isbn": {
"type": "string",
Expand All @@ -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"
}
}
}
},
Expand Down Expand Up @@ -960,8 +962,7 @@
},
"id": {
"readOnly": true,
"type": "integer",
"nullable": true
"type": "integer"
},
"isbn": {
"type": "string",
Expand All @@ -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"
}
}
}
},
Expand All @@ -993,8 +997,7 @@
"properties": {
"id": {
"readOnly": true,
"type": "integer",
"nullable": true
"type": "integer"
},
"rating": {
"type": "integer"
Expand All @@ -1007,8 +1010,7 @@
},
"publicationDate": {
"type": "string",
"format": "date-time",
"nullable": true
"format": "date-time"
},
"book": {
"type": "string",
Expand Down Expand Up @@ -1059,8 +1061,7 @@
},
"id": {
"readOnly": true,
"type": "integer",
"nullable": true
"type": "integer"
},
"rating": {
"type": "integer"
Expand All @@ -1073,8 +1074,7 @@
},
"publicationDate": {
"type": "string",
"format": "date-time",
"nullable": true
"format": "date-time"
},
"book": {
"type": "string",
Expand Down

0 comments on commit 3722128

Please sign in to comment.