Skip to content

Commit 962a588

Browse files
committed
Add groups
1 parent 25d826c commit 962a588

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

api/src/Entity/Book.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\Common\Collections\ArrayCollection;
1010
use Doctrine\Common\Collections\Collection;
1111
use Doctrine\ORM\Mapping as ORM;
12+
use Symfony\Component\Serializer\Annotation\Groups;
1213
use Symfony\Component\Validator\Constraints as Assert;
1314

1415
/**
@@ -17,7 +18,10 @@
1718
* @see http://schema.org/Book Documentation on Schema.org
1819
*
1920
* @ORM\Entity
20-
* @ApiResource(iri="http://schema.org/Book")
21+
* @ApiResource(
22+
* iri="http://schema.org/Book",
23+
* normalizationContext={"groups": {"book:read"}}
24+
* )
2125
* @ApiFilter(PropertyFilter::class)
2226
*/
2327
class Book
@@ -36,6 +40,7 @@ class Book
3640
*
3741
* @Assert\Isbn
3842
* @ORM\Column(nullable=true)
43+
* @Groups("book:read")
3944
* @ApiProperty(iri="http://schema.org/isbn")
4045
*/
4146
public $isbn;
@@ -45,6 +50,7 @@ class Book
4550
*
4651
* @Assert\NotBlank
4752
* @ORM\Column
53+
* @Groups({"book:read", "review:read"})
4854
* @ApiProperty(iri="http://schema.org/name")
4955
*/
5056
public $title;
@@ -54,6 +60,7 @@ class Book
5460
*
5561
* @Assert\NotBlank
5662
* @ORM\Column(type="text")
63+
* @Groups("book:read")
5764
* @ApiProperty(iri="http://schema.org/description")
5865
*/
5966
public $description;
@@ -63,6 +70,7 @@ class Book
6370
*
6471
* @Assert\NotBlank
6572
* @ORM\Column
73+
* @Groups("book:read")
6674
* @ApiProperty(iri="http://schema.org/author")
6775
*/
6876
public $author;
@@ -73,14 +81,16 @@ class Book
7381
* @Assert\Date
7482
* @Assert\NotNull
7583
* @ORM\Column(type="date")
84+
* @Groups("book:read")
7685
* @ApiProperty(iri="http://schema.org/dateCreated")
7786
*/
7887
public $publicationDate;
7988

8089
/**
8190
* @var Review[] The book's reviews
8291
*
83-
* @ORM\OneToMany(targetEntity=Review::class, mappedBy="book", orphanRemoval=true, cascade={"all"})
92+
* @ORM\OneToMany(targetEntity=Review::class, mappedBy="book", orphanRemoval=true, cascade={"persist", "remove"})
93+
* @Groups("book:read")
8494
* @ApiProperty(iri="http://schema.org/reviews")
8595
*/
8696
private $reviews;
@@ -95,27 +105,31 @@ public function getId(): ?int
95105
return $this->id;
96106
}
97107

98-
/**
99-
* @return Collection|Review[]
100-
*/
101-
public function getReviews(): iterable
108+
public function addReview(Review $review, bool $updateRelation = true): void
102109
{
103-
return $this->reviews;
110+
if ($this->reviews->contains($review)) {
111+
return;
112+
}
113+
114+
$this->reviews->add($review);
115+
if ($updateRelation) {
116+
$review->setBook($this, false);
117+
}
104118
}
105119

106-
public function addReview(Review $review): void
120+
public function removeReview(Review $review, bool $updateRelation = true): void
107121
{
108-
if (!$this->reviews->contains($review)) {
109-
$this->reviews->add($review);
110-
$review->book = $this;
122+
$this->reviews->removeElement($review);
123+
if ($updateRelation) {
124+
$review->setBook(null, false);
111125
}
112126
}
113127

114-
public function removeReview(Review $review): void
128+
/**
129+
* @return Collection|Review[]
130+
*/
131+
public function getReviews(): iterable
115132
{
116-
if ($this->reviews->contains($review)) {
117-
$review->book = $this;
118-
$this->reviews->removeElement($review);
119-
}
133+
return $this->reviews;
120134
}
121135
}

api/src/Entity/Review.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ApiPlatform\Core\Annotation\ApiResource;
88
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
99
use Doctrine\ORM\Mapping as ORM;
10+
use Symfony\Component\Serializer\Annotation\Groups;
1011
use Symfony\Component\Validator\Constraints as Assert;
1112

1213
/**
@@ -15,7 +16,10 @@
1516
* @see http://schema.org/Review Documentation on Schema.org
1617
*
1718
* @ORM\Entity
18-
* @ApiResource(iri="http://schema.org/Review")
19+
* @ApiResource(
20+
* iri="http://schema.org/Review",
21+
* normalizationContext={"groups": {"review:read"}}
22+
* )
1923
* @ApiFilter(SearchFilter::class, properties={"book": "exact"})
2024
*/
2125
class Review
@@ -33,6 +37,7 @@ class Review
3337
* @var string The actual body of the review
3438
*
3539
* @ORM\Column(type="text", nullable=true)
40+
* @Groups({"book:read", "review:read"})
3641
* @ApiProperty(iri="http://schema.org/reviewBody")
3742
*/
3843
public $body;
@@ -42,6 +47,7 @@ class Review
4247
*
4348
* @Assert\Range(min=0, max=5)
4449
* @ORM\Column(type="smallint")
50+
* @Groups("review:read")
4551
*/
4652
public $rating;
4753

@@ -50,6 +56,7 @@ class Review
5056
*
5157
* @Assert\Choice({"a", "b", "c", "d"})
5258
* @ORM\Column(type="string", nullable=true)
59+
* @Groups("review:read")
5360
* @ApiProperty(deprecationReason="Use the rating property instead")
5461
*/
5562
public $letter;
@@ -59,22 +66,24 @@ class Review
5966
*
6067
* @Assert\NotNull
6168
* @ORM\ManyToOne(targetEntity=Book::class, inversedBy="reviews")
62-
* @ORM\JoinColumn(nullable=false)
69+
* @Groups("review:read")
6370
* @ApiProperty(iri="http://schema.org/itemReviewed")
6471
*/
65-
public $book;
72+
private $book;
6673

6774
/**
6875
* @var string Author the author of the review
6976
*
7077
* @ORM\Column(type="text", nullable=true)
78+
* @Groups("review:read")
7179
* @ApiProperty(iri="http://schema.org/author")
7280
*/
7381
public $author;
7482

7583
/**
7684
* @var \DateTimeInterface Author the author of the review
7785
*
86+
* @Groups("review:read")
7887
* @ORM\Column(nullable=true, type="datetime")
7988
*/
8089
public $publicationDate;
@@ -83,4 +92,17 @@ public function getId(): ?int
8392
{
8493
return $this->id;
8594
}
95+
96+
public function setBook(?Book $book, bool $updateRelation = true): void
97+
{
98+
$this->book = $book;
99+
if ($updateRelation) {
100+
$book->addReview($this, false);
101+
}
102+
}
103+
104+
public function getBook(): ?Book
105+
{
106+
return $this->book;
107+
}
86108
}

0 commit comments

Comments
 (0)