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