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