1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Guests\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
6
|
|
|
use App\Guests\Entity\GuestSession; |
7
|
|
|
use App\Guests\Entity\GuestWatchedMovie; |
8
|
|
|
use App\Guests\Repository\WatchedMovieRepository; |
9
|
|
|
use App\Movies\Request\AddWatchedMovieRequest; |
10
|
|
|
use App\Movies\Service\WatchedMovieService; |
11
|
|
|
use App\Pagination\PaginatedCollection; |
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
15
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
16
|
|
|
|
17
|
|
|
class WatchedMovieController extends BaseController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @Route("/api/guests/{token}/watchedMovies", methods={"POST"}); |
21
|
|
|
* |
22
|
|
|
* @param $token |
23
|
|
|
* @param AddWatchedMovieRequest $addWatchedMovieRequest |
24
|
|
|
* @param Request $request |
25
|
|
|
* @param WatchedMovieService $watchedMovieService |
26
|
|
|
* |
27
|
|
|
* @throws \Exception |
28
|
|
|
* |
29
|
|
|
* @return JsonResponse |
30
|
|
|
*/ |
31
|
3 |
|
public function postWatchedMovies($token, AddWatchedMovieRequest $addWatchedMovieRequest, Request $request, WatchedMovieService $watchedMovieService) |
32
|
|
|
{ |
33
|
3 |
|
$em = $this->getDoctrine()->getManager(); |
34
|
3 |
|
$guestSessionRepository = $em->getRepository(GuestSession::class); |
35
|
|
|
|
36
|
|
|
/** @var $guestSession GuestSession|null */ |
37
|
3 |
|
$guestSession = $guestSessionRepository->findOneBy([ |
38
|
3 |
|
'token' => $token, |
39
|
|
|
]); |
40
|
|
|
|
41
|
3 |
|
if ($guestSession === null) { |
42
|
|
|
throw new NotFoundHttpException('Guest session not found by provided token'); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
$watchedMovieDTO = $addWatchedMovieRequest->getWatchedMovieDTO(); |
46
|
3 |
|
$isMovieAdded = $watchedMovieService->addGuestWatchedMovie($guestSession, $watchedMovieDTO, $request->getLocale()); |
47
|
|
|
|
48
|
3 |
|
if ($isMovieAdded === false) { |
49
|
|
|
throw new NotFoundHttpException('Movie not found by provided ID / TMDB ID'); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
return new JsonResponse(null, 202); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @Route("/api/guests/{id<\d+>}/watchedMovies", methods={"GET"}); |
57
|
|
|
* |
58
|
|
|
* @param Request $request |
59
|
|
|
* @param GuestSession $guestSession |
60
|
|
|
* |
61
|
|
|
* @return JsonResponse |
62
|
|
|
*/ |
63
|
|
|
public function getAll(Request $request, GuestSession $guestSession) |
64
|
|
|
{ |
65
|
|
|
$em = $this->getDoctrine()->getManager(); |
66
|
|
|
/** @var $watchedMovieRepository WatchedMovieRepository */ |
67
|
|
|
$watchedMovieRepository = $em->getRepository(GuestWatchedMovie::class); |
68
|
|
|
|
69
|
|
|
$offset = (int) $request->get('offset', 0); |
70
|
|
|
$limit = $request->get('limit', null); |
71
|
|
|
|
72
|
|
|
$watchedMovies = new PaginatedCollection( |
73
|
|
|
$watchedMovieRepository->getAllWatchedMoviesByGuestSessionId($guestSession->getId()), |
74
|
|
|
$offset, |
75
|
|
|
$limit ? (int) $limit : null |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return $this->response($watchedMovies, 200, [], [ |
79
|
|
|
'groups' => ['list'], |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|