1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Genres\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller\ControllerInterface; |
6
|
|
|
use App\Genres\Entity\Genre; |
7
|
|
|
use App\Users\Entity\User; |
8
|
|
|
use App\Genres\Request\CreateGenreRequest; |
9
|
|
|
use App\Genres\Request\UpdateGenreRequest; |
10
|
|
|
use App\Genres\Service\GenreManageService; |
11
|
|
|
use FOS\RestBundle\Controller\FOSRestController; |
12
|
|
|
use Swagger\Annotations as SWG; |
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
14
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
15
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class GenreController |
19
|
|
|
* @package App\Genres\Controller |
20
|
|
|
*/ |
21
|
|
|
class GenreController extends FOSRestController implements ControllerInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Get all genres |
25
|
|
|
* |
26
|
|
|
* @Route("/api/genres", methods={"GET"}) |
27
|
|
|
* @SWG\Response( |
28
|
|
|
* description="REST action which returns all genres.", |
29
|
|
|
* response=200, |
30
|
|
|
* @SWG\Schema( |
31
|
|
|
* type="array", |
32
|
|
|
* @SWG\Items(ref=@Model(type=Genre::class, groups={"full"})) |
33
|
|
|
* ) |
34
|
|
|
* ) |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
3 |
|
public function getAll() |
39
|
|
|
{ |
40
|
3 |
|
return $this->getDoctrine()->getRepository(Genre::class)->findAll(); |
41
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create new genre |
46
|
|
|
* |
47
|
|
|
* @Route("/api/genres", methods={"POST"}) |
48
|
|
|
* @SWG\Parameter(name="genre[translations][0][locale]", in="formData", type="string") |
49
|
|
|
* @SWG\Parameter(name="genre[translations][0][name]", in="formData", type="string") |
50
|
|
|
* @SWG\Response( |
51
|
|
|
* description="New genre action.", |
52
|
|
|
* response=202, |
53
|
|
|
* @Model(type=Genre::class) |
54
|
|
|
* ) |
55
|
|
|
* @param \App\Genres\Request\CreateGenreRequest $request |
56
|
|
|
* @param \App\Genres\Service\GenreManageService $genreManageService |
57
|
|
|
* @param ValidatorInterface $validator |
58
|
|
|
* @return Genre|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response |
59
|
|
|
*/ |
60
|
3 |
|
public function postGenres(CreateGenreRequest $request, GenreManageService $genreManageService, ValidatorInterface $validator) |
61
|
|
|
{ |
62
|
3 |
|
$this->denyAccessUnlessGranted(User::ROLE_ADMIN); |
63
|
|
|
|
64
|
2 |
|
$genre = $genreManageService->createGenreByRequest($request); |
65
|
2 |
|
$errors = $validator->validate($genre); |
66
|
|
|
|
67
|
2 |
|
if (count($errors)) { |
68
|
|
|
return $request->getErrorResponse($errors); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
$this->getDoctrine()->getManager()->flush(); |
72
|
|
|
|
73
|
2 |
|
return $genre; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Update genre |
78
|
|
|
* |
79
|
|
|
* @Route("/api/genres/{id}", methods={"POST"}) |
80
|
|
|
* @SWG\Parameter(name="genre[translations][0][locale]", in="formData", type="string") |
81
|
|
|
* @SWG\Parameter(name="genre[translations][0][name]", in="formData", type="string") |
82
|
|
|
* @SWG\Response( |
83
|
|
|
* description="New genre action.", |
84
|
|
|
* response=202, |
85
|
|
|
* @Model(type=Genre::class) |
86
|
|
|
* ) |
87
|
|
|
* @param UpdateGenreRequest $request |
88
|
|
|
* @param Genre $genre |
89
|
|
|
* @param \App\Genres\Service\GenreManageService $genreManageService |
90
|
|
|
* @param ValidatorInterface $validator |
91
|
|
|
* @return Genre|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response |
92
|
|
|
*/ |
93
|
1 |
|
public function putGenres(UpdateGenreRequest $request, Genre $genre, GenreManageService $genreManageService, ValidatorInterface $validator) |
94
|
|
|
{ |
95
|
1 |
|
$this->denyAccessUnlessGranted(User::ROLE_ADMIN); |
96
|
|
|
|
97
|
1 |
|
$genre = $genreManageService->updateGenreByRequest($request, $genre); |
98
|
1 |
|
$errors = $validator->validate($genre); |
99
|
|
|
|
100
|
1 |
|
if (count($errors)) { |
101
|
|
|
return $request->getErrorResponse($errors); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
$this->getDoctrine()->getManager()->flush(); |
105
|
|
|
|
106
|
1 |
|
return $genre; |
107
|
|
|
} |
108
|
|
|
} |