1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Movies\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
6
|
|
|
use App\Movies\Entity\Movie; |
7
|
|
|
use App\Movies\Request\CreateMovieRequest; |
8
|
|
|
use App\Movies\Request\SearchRequest; |
9
|
|
|
use App\Movies\Service\MovieManageService; |
10
|
|
|
use App\Movies\Service\SearchService; |
11
|
|
|
use App\Users\Entity\UserRoles; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
14
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class MovieController |
18
|
|
|
* @package App\Movies\Controller |
19
|
|
|
*/ |
20
|
|
|
class MovieController extends BaseController |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Get all movies |
24
|
|
|
* |
25
|
|
|
* @Route("/api/movies", methods={"GET"}) |
26
|
|
|
*/ |
27
|
3 |
|
public function getAll() |
28
|
|
|
{ |
29
|
3 |
|
$movies = $this->getDoctrine()->getRepository(Movie::class)->findAll(); |
30
|
3 |
|
return $this->response($movies, 200, [], [ |
31
|
3 |
|
'groups' => ['list'], |
32
|
|
|
]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get movie resource |
37
|
|
|
* |
38
|
|
|
* @Route("/api/movies/{id}", methods={"GET"}) |
39
|
|
|
* @param Movie $movie |
40
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
41
|
|
|
*/ |
42
|
|
|
public function getMovies(Movie $movie) |
43
|
|
|
{ |
44
|
|
|
return $this->response($movie, 200, [], [ |
45
|
|
|
'groups' => ['view'], |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get movies by title |
51
|
|
|
* |
52
|
|
|
* @Route("/api/movies/search", methods={"POST"}) |
53
|
|
|
* @param SearchRequest $request |
54
|
|
|
* @param SearchService $searchService |
55
|
|
|
* @param Request $currentRequest |
56
|
|
|
* @throws \Exception |
57
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
58
|
|
|
*/ |
59
|
2 |
|
public function getSearch(SearchRequest $request, SearchService $searchService, Request $currentRequest) |
60
|
|
|
{ |
61
|
2 |
|
$query = $request->get('query'); |
62
|
2 |
|
$movies = $searchService->findByQuery($query, $currentRequest->getLocale()); |
63
|
|
|
|
64
|
2 |
|
return $this->response($movies, 200, [], [ |
65
|
2 |
|
'groups' => ['list'] |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create new movie |
71
|
|
|
* |
72
|
|
|
* @Route("/api/movies", methods={"POST"}) |
73
|
|
|
* |
74
|
|
|
* @param CreateMovieRequest $request |
75
|
|
|
* @param MovieManageService $service |
76
|
|
|
* @param ValidatorInterface $validator |
77
|
|
|
* @throws \Exception |
78
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
79
|
|
|
*/ |
80
|
2 |
|
public function postMovies(CreateMovieRequest $request, MovieManageService $service, ValidatorInterface $validator) |
81
|
|
|
{ |
82
|
2 |
|
$this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN); |
83
|
|
|
|
84
|
1 |
|
$movie = $service->createMovieByRequest($request); |
85
|
1 |
|
$errors = $validator->validate($movie); |
86
|
|
|
|
87
|
1 |
|
if (count($errors)) { |
88
|
|
|
return $request->getErrorResponse($errors); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
$entityManager = $this->getDoctrine()->getManager(); |
92
|
1 |
|
$entityManager->persist($movie); |
93
|
1 |
|
$entityManager->flush(); |
94
|
|
|
|
95
|
1 |
|
return $this->response($movie, 200, [], [ |
96
|
1 |
|
'groups' => ['view'], |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
} |