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