Skip to content

Commit 89ebe02

Browse files
authored
Merge pull request #4 from sroze/feat/add-search-filter-on-book
Feat/add search filter on book
2 parents 6fa77cf + 5e916f6 commit 89ebe02

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

app/config/services.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ parameters:
44
# parameter_name: value
55

66
services:
7-
# service_name:
8-
# class: AppBundle\Directory\ClassName
9-
# arguments: ["@another_service_name", "plain_value", "%parameter_name%"]
7+
app.book_resource.search_filter:
8+
parent: 'api_platform.doctrine.orm.search_filter'
9+
arguments: [ { 'name': 'partial' } ]
10+
tags: [ { name: 'api_platform.filter', id: 'book.search' } ]

src/AppBundle/Entity/Book.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,31 @@
66
use Doctrine\Common\Collections\Collection;
77
use Doctrine\ORM\Mapping as ORM;
88
use ApiPlatform\Core\Annotation\ApiResource;
9+
use Symfony\Component\Serializer\Annotation\Groups;
10+
use Symfony\Component\Validator\Constraints as Assert;
11+
912

1013
/**
11-
* @ApiResource(itemOperations={
12-
* "get"={"method"="GET"},
13-
* "publish-reviews"={"route_name"="book_publish_reviews"}
14-
* })
14+
* @ApiResource(attributes={
15+
* "filters"={"book.search"},
16+
* "normalization_context"={"groups"={"book_default_out"}},
17+
* "denormalization_context"={"groups"={"book_default_in"}}
18+
* },
19+
* collectionOperations={
20+
* "post"={"method"="POST"},
21+
* "get"={
22+
* "method"="GET",
23+
* "normalization_context"={"groups"={"book_collection_out"}}
24+
* }
25+
* },
26+
* itemOperations={
27+
* "put"={"method"="PUT"},
28+
* "publish-reviews"={"route_name"="book_publish_reviews"},
29+
* "get"={
30+
* "method"="GET",
31+
* "normalization_context"={"groups"={"book_item_out", "review_default_out"}}
32+
* }
33+
* })
1534
*
1635
* @ORM\Entity
1736
*/
@@ -21,21 +40,24 @@ class Book
2140
* @ORM\Id
2241
* @ORM\Column(type="string")
2342
* @ORM\GeneratedValue(strategy="UUID")
43+
* @Groups({"book_collection_out"})
2444
*
2545
* @var string
2646
*/
2747
private $uuid;
2848

2949
/**
3050
* @ORM\Column(type="string")
51+
* @Groups({"book_collection_out", "book_default_in"})
52+
* @Assert\Type(type="string")
3153
*
3254
* @var string
3355
*/
3456
private $name;
3557

3658
/**
3759
* @ORM\OneToMany(targetEntity="Review", mappedBy="book", cascade={"persist"})
38-
*
60+
* @Groups({"book_item_out", "book_default_in"})
3961
* @var Collection
4062
*/
4163
private $reviews;

src/AppBundle/Entity/Review.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,41 @@
44

55
use Doctrine\ORM\Mapping as ORM;
66
use ApiPlatform\Core\Annotation\ApiResource;
7+
use Symfony\Component\Serializer\Annotation\Groups;
78

89
/**
910
* @ORM\Entity
1011
*
11-
* @ApiResource
12+
* @ApiResource(attributes={
13+
* "normalization_context"={"groups"={"review_default_out"}},
14+
* "denormalization_context"={"groups"={"review_default_in"}},
15+
* })
16+
*
1217
*/
1318
class Review
1419
{
1520
/**
1621
* @ORM\Id
1722
* @ORM\Column(type="string")
1823
* @ORM\GeneratedValue(strategy="UUID")
24+
* @Groups({"review_default_out"})
1925
*
2026
* @var string
2127
*/
2228
private $uuid;
2329

2430
/**
2531
* @ORM\Column(type="text")
32+
* @Groups({"review_default_out", "review_default_in"})
33+
* @Assert\Type(type="string")
2634
*
2735
* @var string
2836
*/
2937
private $contents;
3038

3139
/**
3240
* @ORM\Column(type="boolean")
41+
* @Groups({"review_default_out", "review_default_in"})
3342
*
3443
* @var boolean
3544
*/
@@ -38,6 +47,7 @@ class Review
3847
/**
3948
* @ORM\ManyToOne(targetEntity="Book", inversedBy="reviews")
4049
* @ORM\JoinColumn(name="book_uuid", referencedColumnName="uuid")
50+
* @Groups({"review_default_out", "review_default_in"})
4151
*
4252
* @var Book
4353
*/

0 commit comments

Comments
 (0)