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
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
1 |
|
public function getAll() |
30
|
|
|
{ |
31
|
1 |
|
$movies = $this->getDoctrine()->getRepository(Movie::class)->findAll(); |
32
|
1 |
|
return $this->response($movies, 200, [], [ |
33
|
1 |
|
'groups' => ['list'], |
34
|
|
|
]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get movies by title |
39
|
|
|
* |
40
|
|
|
* @Route("/api/movies/search", methods={"POST"}) |
41
|
|
|
* @param SearchRequest $request |
42
|
|
|
* @param SearchService $searchService |
43
|
|
|
* @throws \Exception |
44
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
45
|
|
|
*/ |
46
|
2 |
|
public function getSearch(SearchRequest $request, SearchService $searchService) |
47
|
|
|
{ |
48
|
2 |
|
$query = $request->get('query'); |
49
|
2 |
|
$movies = $searchService->findByQuery($query, $this->currentRequest->getLocale()); |
50
|
|
|
|
51
|
2 |
|
return $this->response($movies, 200, [], [ |
52
|
2 |
|
'groups' => ['list'] |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create new movie |
58
|
|
|
* |
59
|
|
|
* @Route("/api/movies", methods={"POST"}) |
60
|
|
|
* |
61
|
|
|
* @param CreateMovieRequest $request |
62
|
|
|
* @param MovieManageService $service |
63
|
|
|
* @param ValidatorInterface $validator |
64
|
|
|
* @throws \Exception |
65
|
|
|
* @return Movie|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response |
66
|
|
|
*/ |
67
|
2 |
|
public function postMovies(CreateMovieRequest $request, MovieManageService $service, ValidatorInterface $validator) |
68
|
|
|
{ |
69
|
2 |
|
$this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN); |
70
|
|
|
|
71
|
1 |
|
$movie = $service->createMovieByRequest($request); |
72
|
1 |
|
$errors = $validator->validate($movie); |
73
|
|
|
|
74
|
1 |
|
if (count($errors)) { |
75
|
|
|
return $request->getErrorResponse($errors); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$entityManager = $this->getDoctrine()->getManager(); |
79
|
1 |
|
$entityManager->persist($movie); |
80
|
1 |
|
$entityManager->flush(); |
81
|
|
|
|
82
|
1 |
|
return $this->response($movie, 200, [], [ |
83
|
1 |
|
'groups' => ['view'], |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
} |