1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Genres\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
6
|
|
|
use App\Genres\Entity\Genre; |
7
|
|
|
use App\Genres\Repository\GenreRepository; |
8
|
|
|
use App\Genres\Request\CreateGenreRequest; |
9
|
|
|
use App\Genres\Request\UpdateGenreRequest; |
10
|
|
|
use App\Genres\Service\GenreManageService; |
11
|
|
|
use App\Pagination\PaginatedCollection; |
12
|
|
|
use App\Users\Entity\UserRoles; |
13
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
15
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class GenreController. |
19
|
|
|
*/ |
20
|
|
|
class GenreController extends BaseController |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Get all genres. |
24
|
|
|
* |
25
|
|
|
* @Route("/api/genres", methods={"GET"}) |
26
|
|
|
* |
27
|
|
|
* @return JsonResponse |
28
|
|
|
*/ |
29
|
5 |
|
public function getAll(GenreRepository $genreRepository) |
30
|
|
|
{ |
31
|
5 |
|
$genres = $genreRepository->findAllWithTranslations(); |
32
|
5 |
|
$genres = new PaginatedCollection($genres->getQuery(), 0, 20); |
33
|
|
|
|
34
|
5 |
|
return $this->response($genres, 200, [], [ |
35
|
5 |
|
'groups' => ['list'], |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create new genre. |
41
|
|
|
* |
42
|
|
|
* @Route("/api/genres", methods={"POST"}) |
43
|
|
|
* |
44
|
|
|
* @param \App\Genres\Request\CreateGenreRequest $request |
45
|
|
|
* @param \App\Genres\Service\GenreManageService $genreManageService |
46
|
|
|
* @param ValidatorInterface $validator |
47
|
|
|
* |
48
|
|
|
* @return Genre|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response |
49
|
|
|
*/ |
50
|
2 |
|
public function postGenres(CreateGenreRequest $request, GenreManageService $genreManageService, ValidatorInterface $validator) |
51
|
|
|
{ |
52
|
2 |
|
$this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN); |
53
|
|
|
|
54
|
1 |
|
$genre = $genreManageService->createGenreByRequest($request); |
55
|
1 |
|
$errors = $validator->validate($genre); |
56
|
|
|
|
57
|
1 |
|
if (\count($errors)) { |
58
|
|
|
return $request->getErrorResponse($errors); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$this->getDoctrine()->getManager()->flush(); |
62
|
|
|
|
63
|
1 |
|
return $this->response($genre, 200, [], [ |
64
|
1 |
|
'groups' => ['view'], |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Update genre. |
70
|
|
|
* |
71
|
|
|
* @Route("/api/genres/{id}", methods={"POST"}) |
72
|
|
|
* |
73
|
|
|
* @param UpdateGenreRequest $request |
74
|
|
|
* @param Genre $genre |
75
|
|
|
* @param \App\Genres\Service\GenreManageService $genreManageService |
76
|
|
|
* @param ValidatorInterface $validator |
77
|
|
|
* |
78
|
|
|
* @return Genre|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response |
79
|
|
|
*/ |
80
|
1 |
|
public function putGenres(UpdateGenreRequest $request, Genre $genre, GenreManageService $genreManageService, ValidatorInterface $validator) |
81
|
|
|
{ |
82
|
1 |
|
$this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN); |
83
|
|
|
|
84
|
1 |
|
$genre = $genreManageService->updateGenreByRequest($request, $genre); |
85
|
1 |
|
$errors = $validator->validate($genre); |
86
|
|
|
|
87
|
1 |
|
if (\count($errors)) { |
88
|
|
|
return $request->getErrorResponse($errors); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
$this->getDoctrine()->getManager()->flush(); |
92
|
|
|
|
93
|
1 |
|
return $this->response($genre, 200, [], [ |
94
|
1 |
|
'groups' => ['view'], |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|