|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use App\Application\Command\CreateBook; |
|
8
|
|
|
use App\Application\Dto\BookDto; |
|
9
|
|
|
use App\Application\Dto\NewBookDto; |
|
10
|
|
|
use App\Application\Exception\BookNotFound; |
|
11
|
|
|
use App\Application\Service\BookService; |
|
12
|
|
|
use Damax\Common\Bridge\Symfony\Bundle\Annotation\Deserialize; |
|
13
|
|
|
use Damax\Common\Bridge\Symfony\Bundle\Annotation\Serialize; |
|
14
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
|
15
|
|
|
use Pagerfanta\Pagerfanta; |
|
16
|
|
|
use Swagger\Annotations as OpenApi; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
19
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @Route("/books") |
|
23
|
|
|
*/ |
|
24
|
|
|
final class BookController |
|
25
|
|
|
{ |
|
26
|
|
|
private $service; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(BookService $service) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->service = $service; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @OpenApi\Get( |
|
35
|
|
|
* tags={"book"}, |
|
36
|
|
|
* summary="List books.", |
|
37
|
|
|
* @OpenApi\Parameter( |
|
38
|
|
|
* name="authorId", |
|
39
|
|
|
* type="string", |
|
40
|
|
|
* in="query", |
|
41
|
|
|
* description="Author ID." |
|
42
|
|
|
* ), |
|
43
|
|
|
* @OpenApi\Parameter( |
|
44
|
|
|
* name="page", |
|
45
|
|
|
* type="integer", |
|
46
|
|
|
* in="query", |
|
47
|
|
|
* description="Page number.", |
|
48
|
|
|
* default=1 |
|
49
|
|
|
* ), |
|
50
|
|
|
* @OpenApi\Response( |
|
51
|
|
|
* response=200, |
|
52
|
|
|
* description="Books list.", |
|
53
|
|
|
* @OpenApi\Schema(type="array", @OpenApi\Items(ref=@Model(type=BookDto::class))) |
|
54
|
|
|
* ) |
|
55
|
|
|
* ) |
|
56
|
|
|
* |
|
57
|
|
|
* @Route("", methods={"GET"}) |
|
58
|
|
|
* @Serialize() |
|
59
|
|
|
*/ |
|
60
|
|
|
public function index(Request $request): Pagerfanta |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->service |
|
63
|
|
|
->fetchRange($request->query->get('authorId')) |
|
64
|
|
|
->setAllowOutOfRangePages(true) |
|
65
|
|
|
->setMaxPerPage(10) |
|
66
|
|
|
->setCurrentPage($request->query->getInt('page', 1)) |
|
67
|
|
|
; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @OpenApi\Get( |
|
72
|
|
|
* tags={"book"}, |
|
73
|
|
|
* summary="Get book.", |
|
74
|
|
|
* @OpenApi\Response( |
|
75
|
|
|
* response=200, |
|
76
|
|
|
* description="Book info.", |
|
77
|
|
|
* @OpenApi\Schema(ref=@Model(type=BookDto::class)) |
|
78
|
|
|
* ), |
|
79
|
|
|
* @OpenApi\Response( |
|
80
|
|
|
* response=404, |
|
81
|
|
|
* description="Book not found." |
|
82
|
|
|
* ) |
|
83
|
|
|
* ) |
|
84
|
|
|
* |
|
85
|
|
|
* @Route("/{id}", methods={"GET"}) |
|
86
|
|
|
* @Serialize() |
|
87
|
|
|
* |
|
88
|
|
|
* @throws NotFoundHttpException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function view(string $id): BookDto |
|
91
|
|
|
{ |
|
92
|
|
|
try { |
|
93
|
|
|
return $this->service->fetch($id); |
|
94
|
|
|
} catch (BookNotFound $e) { |
|
95
|
|
|
throw new NotFoundHttpException(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @OpenApi\Post( |
|
101
|
|
|
* tags={"book"}, |
|
102
|
|
|
* summary="Create book.", |
|
103
|
|
|
* @OpenApi\Parameter( |
|
104
|
|
|
* name="body", |
|
105
|
|
|
* in="body", |
|
106
|
|
|
* required=true, |
|
107
|
|
|
* @OpenApi\Schema(ref=@Model(type=NewBookDto::class)) |
|
108
|
|
|
* ), |
|
109
|
|
|
* @OpenApi\Response( |
|
110
|
|
|
* response=201, |
|
111
|
|
|
* description="Book info.", |
|
112
|
|
|
* @OpenApi\Schema(ref=@Model(type=BookDto::class)) |
|
113
|
|
|
* ) |
|
114
|
|
|
* ) |
|
115
|
|
|
* |
|
116
|
|
|
* @Route("", methods={"POST"}) |
|
117
|
|
|
* @Deserialize(NewBookDto::class, validate=true, param="book") |
|
118
|
|
|
* @Serialize() |
|
119
|
|
|
*/ |
|
120
|
|
|
public function create(NewBookDto $book): BookDto |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->service->create(new CreateBook($book)); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|