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 |
|
if ($this->getUser()) { |
30
|
|
|
$userId = $this->getUser()->getId(); |
31
|
|
|
$movies = $this->getDoctrine()->getRepository(Movie::class)->findAllWithIsWatchedFlag($userId); |
|
|
|
|
32
|
|
|
} else { |
33
|
3 |
|
$movies = $this->getDoctrine()->getRepository(Movie::class)->findAll(); |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
return $this->response($movies, 200, [], [ |
37
|
3 |
|
'groups' => ['list'], |
38
|
|
|
]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get movie resource |
43
|
|
|
* |
44
|
|
|
* @Route("/api/movies/{id}", methods={"GET"}) |
45
|
|
|
* @param Movie $movie |
46
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
47
|
|
|
*/ |
48
|
|
|
public function getMovies(Movie $movie) |
49
|
|
|
{ |
50
|
|
|
return $this->response($movie, 200, [], [ |
51
|
|
|
'groups' => ['view'], |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get movies by title |
57
|
|
|
* |
58
|
|
|
* @Route("/api/movies/search", methods={"POST"}) |
59
|
|
|
* @param SearchRequest $request |
60
|
|
|
* @param SearchService $searchService |
61
|
|
|
* @param Request $currentRequest |
62
|
|
|
* @throws \Exception |
63
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
64
|
|
|
*/ |
65
|
2 |
|
public function getSearch(SearchRequest $request, SearchService $searchService, Request $currentRequest) |
66
|
|
|
{ |
67
|
2 |
|
$query = $request->get('query'); |
68
|
2 |
|
$movies = $searchService->findByQuery($query, $currentRequest->getLocale()); |
69
|
|
|
|
70
|
2 |
|
return $this->response($movies, 200, [], [ |
71
|
2 |
|
'groups' => ['list'] |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create new movie |
77
|
|
|
* |
78
|
|
|
* @Route("/api/movies", methods={"POST"}) |
79
|
|
|
* |
80
|
|
|
* @param CreateMovieRequest $request |
81
|
|
|
* @param MovieManageService $service |
82
|
|
|
* @param ValidatorInterface $validator |
83
|
|
|
* @throws \Exception |
84
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
85
|
|
|
*/ |
86
|
2 |
|
public function postMovies(CreateMovieRequest $request, MovieManageService $service, ValidatorInterface $validator) |
87
|
|
|
{ |
88
|
2 |
|
$this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN); |
89
|
|
|
|
90
|
1 |
|
$movie = $service->createMovieByRequest($request); |
91
|
1 |
|
$errors = $validator->validate($movie); |
92
|
|
|
|
93
|
|
|
if (count($errors)) { |
94
|
|
|
return $request->getErrorResponse($errors); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
98
|
|
|
$entityManager->persist($movie); |
99
|
|
|
$entityManager->flush(); |
100
|
|
|
|
101
|
|
|
return $this->response($movie, 200, [], [ |
102
|
|
|
'groups' => ['view'], |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.