1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Movies\Repository; |
6
|
|
|
|
7
|
|
|
use App\Guests\Entity\GuestSession; |
8
|
|
|
use App\Movies\Entity\Movie; |
9
|
|
|
use App\Users\Entity\User; |
10
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
11
|
|
|
use Doctrine\ORM\Query; |
12
|
|
|
use Doctrine\ORM\QueryBuilder; |
13
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @method Movie|null find($id, $lockMode = null, $lockVersion = null) |
17
|
|
|
* @method Movie|null findOneBy(array $criteria, array $orderBy = null) |
18
|
|
|
* @method Movie[] findAll() |
19
|
|
|
* @method Movie[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
20
|
|
|
*/ |
21
|
|
|
class MovieRepository extends ServiceEntityRepository |
22
|
|
|
{ |
23
|
18 |
|
public function __construct(RegistryInterface $registry) |
24
|
|
|
{ |
25
|
18 |
|
parent::__construct($registry, Movie::class); |
26
|
18 |
|
} |
27
|
|
|
|
28
|
14 |
|
private function getBaseQuery(): QueryBuilder |
29
|
|
|
{ |
30
|
14 |
|
return $this->createQueryBuilder('m') |
31
|
14 |
|
->leftJoin('m.translations', 'mt') |
32
|
14 |
|
->addSelect('mt') |
33
|
14 |
|
->leftJoin('m.genres', 'mg') |
34
|
14 |
|
->addSelect('mg') |
35
|
14 |
|
->leftJoin('mg.translations', 'mgt') |
36
|
14 |
|
->addSelect('mgt'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param int $id |
41
|
|
|
* @param User|null $user |
42
|
|
|
* |
43
|
|
|
* @throws \Doctrine\ORM\NoResultException |
44
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
45
|
|
|
* |
46
|
|
|
* @return Movie|null |
47
|
|
|
*/ |
48
|
1 |
|
public function findOneForMoviePage(int $id, ?User $user = null): ?Movie |
49
|
|
|
{ |
50
|
1 |
|
if ($user === null) { |
51
|
1 |
|
return $this->getBaseQuery() |
52
|
1 |
|
->where('m.id = :id') |
53
|
1 |
|
->setParameter('id', $id) |
54
|
1 |
|
->getQuery() |
55
|
1 |
|
->getSingleResult(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$result = $this->getBaseQuery() |
59
|
|
|
->where('m.id = :id') |
60
|
|
|
->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') |
61
|
|
|
->addSelect('uwm') |
62
|
|
|
->leftJoin('m.userRecommendedMovie', 'urm', 'WITH', 'urm.user = :user_id AND urm.originalMovie = :id') |
63
|
|
|
->addSelect('urm') |
64
|
|
|
->setParameter('user_id', $user->getId()) |
65
|
|
|
->setParameter('id', $id) |
66
|
|
|
->getQuery() |
67
|
|
|
->getSingleResult(); |
68
|
|
|
|
69
|
|
|
return $result; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function findAllByIdsWithFlags(array $ids, int $userId, int $originalMovieId) |
73
|
|
|
{ |
74
|
|
|
$result = $this->getBaseQuery() |
75
|
|
|
->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') // if this relation exists then user has already watched this movie |
76
|
|
|
->addSelect('uwm') |
77
|
|
|
->leftJoin('m.userRecommendedMovie', 'urm', 'WITH', 'urm.user = :user_id AND urm.originalMovie = :original_movie_id') |
78
|
|
|
->addSelect('urm') |
79
|
|
|
->where('m.id IN (:ids)') |
80
|
|
|
->setParameter('user_id', $userId) |
81
|
|
|
->setParameter('original_movie_id', $originalMovieId) |
82
|
|
|
->setParameter('ids', $ids) |
83
|
|
|
->getQuery() |
84
|
|
|
->getResult(); |
85
|
|
|
|
86
|
|
|
// Sorting here because ORDER BY FIELD(m.id, ...$ids) not working in postgres, we need to use joins on sorted table and so on, but I dont want to |
87
|
|
|
// todo => add sorting to sql |
88
|
|
|
$reversedIds = array_flip($ids); |
89
|
|
|
usort($result, function (Movie $movie1, Movie $movie2) use ($reversedIds) { |
90
|
|
|
return $reversedIds[$movie1->getId()] <=> $reversedIds[$movie2->getId()]; |
91
|
|
|
}); |
92
|
|
|
|
93
|
|
|
return $result; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function findAllByIdsWithoutFlags(array $ids) |
97
|
|
|
{ |
98
|
|
|
$result = $this->findAllByIds($ids); |
99
|
|
|
|
100
|
|
|
// Sorting here because ORDER BY FIELD(m.id, ...$ids) not working in postgres, we need to use joins on sorted table and so on, but I dont want to |
101
|
|
|
// todo => add sorting to sql |
102
|
|
|
$reversedIds = array_flip($ids); |
103
|
|
|
usort($result, function (Movie $movie1, Movie $movie2) use ($reversedIds) { |
104
|
|
|
return $reversedIds[$movie1->getId()] <=> $reversedIds[$movie2->getId()]; |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
return $result; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function findAllWithIsUserWatchedFlag(User $user) |
111
|
|
|
{ |
112
|
|
|
$result = $this->getBaseQuery() |
113
|
|
|
->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') // if this relation exists then user has already watched this movie |
114
|
|
|
->addSelect('uwm') |
115
|
|
|
->setParameter('user_id', $user->getId()) |
116
|
|
|
->orderBy('m.id', 'DESC') |
117
|
|
|
->getQuery(); |
118
|
|
|
|
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
10 |
|
public function findAllWithIsGuestWatchedFlag(?GuestSession $guestSession) |
123
|
|
|
{ |
124
|
10 |
|
$guestSessionId = $guestSession ? $guestSession->getId() : 0; |
125
|
|
|
|
126
|
10 |
|
$result = $this->getBaseQuery() |
127
|
10 |
|
->leftJoin('m.guestWatchedMovie', 'gwm', 'WITH', 'gwm.guestSession = :guest_session_id') // if this relation exists then guest has already watched this movie |
128
|
10 |
|
->addSelect('gwm') |
129
|
10 |
|
->setParameter('guest_session_id', $guestSessionId) |
130
|
10 |
|
->orderBy('m.id', 'DESC') |
131
|
10 |
|
->getQuery(); |
132
|
|
|
|
133
|
10 |
|
return $result; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $ids |
138
|
|
|
* |
139
|
|
|
* @return array|Movie[] |
140
|
|
|
*/ |
141
|
|
|
public function findAllByIds(array $ids) |
142
|
|
|
{ |
143
|
|
|
$result = $this->getBaseQuery() |
144
|
|
|
->where('m.id IN (:ids)') |
145
|
|
|
->setParameter('ids', $ids) |
146
|
|
|
->getQuery() |
147
|
|
|
->getResult(); |
148
|
|
|
|
149
|
|
|
return $result; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param array $ids |
154
|
|
|
* |
155
|
|
|
* @return array|Movie[] |
156
|
|
|
*/ |
157
|
|
|
public function findAllByIdsWithSimilarMovies(array $ids): array |
158
|
|
|
{ |
159
|
|
|
$result = $this->createQueryBuilder('m') |
160
|
|
|
->leftJoin('m.similarMovies', 'sm') |
161
|
|
|
->addSelect('sm') |
162
|
|
|
->where('m.id IN (:ids)') |
163
|
|
|
->setParameter('ids', $ids) |
164
|
|
|
->getQuery() |
165
|
|
|
->getScalarResult(); |
166
|
|
|
|
167
|
|
|
return $result; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param array $ids |
172
|
|
|
* |
173
|
|
|
* @return array|Movie[] |
174
|
|
|
*/ |
175
|
1 |
|
public function findAllByTmdbIds(array $ids) |
176
|
|
|
{ |
177
|
1 |
|
$result = $this->getBaseQuery() |
178
|
1 |
|
->where('m.tmdb.id IN (:ids)') |
179
|
1 |
|
->setParameter('ids', $ids) |
180
|
1 |
|
->getQuery() |
181
|
1 |
|
->getResult(); |
182
|
|
|
|
183
|
1 |
|
return $result; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array $ids |
188
|
|
|
* |
189
|
|
|
* @return array of int |
190
|
|
|
*/ |
191
|
|
|
public function findAllIdsByTmdbIds(array $ids) |
192
|
|
|
{ |
193
|
|
|
$result = $this->createQueryBuilder('m') |
194
|
|
|
->select('m.id, m.tmdb.voteAverage') |
195
|
|
|
->where('m.tmdb.id IN (:ids)') |
196
|
|
|
->setParameter('ids', $ids) |
197
|
|
|
->getQuery() |
198
|
|
|
->getScalarResult(); |
199
|
|
|
|
200
|
|
|
return $result; |
201
|
|
|
} |
202
|
|
|
|
203
|
2 |
|
public function getAllWatchedMoviesByUserId(int $userId): Query |
204
|
|
|
{ |
205
|
2 |
|
$result = $this->getBaseQuery() |
206
|
2 |
|
->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') |
207
|
2 |
|
->addSelect('uwm') |
208
|
2 |
|
->setParameter('user_id', $userId) |
209
|
2 |
|
->andWhere('uwm.id != 0') |
210
|
2 |
|
->orderBy('uwm.id', 'DESC') |
211
|
2 |
|
->getQuery(); |
212
|
|
|
|
213
|
2 |
|
return $result; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function findAllQuery() |
217
|
|
|
{ |
218
|
|
|
$result = $this->getBaseQuery() |
219
|
|
|
->orderBy('m.id', 'DESC') |
220
|
|
|
->getQuery(); |
221
|
|
|
|
222
|
|
|
return $result; |
223
|
|
|
} |
224
|
|
|
|
225
|
2 |
|
public function findByTitleQuery(string $query) |
226
|
|
|
{ |
227
|
2 |
|
$query = mb_strtolower($query); |
228
|
2 |
|
$result = $this->getBaseQuery() |
229
|
2 |
|
->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(mt.title) LIKE :title') |
230
|
2 |
|
->setParameter('title', "%{$query}%") |
231
|
2 |
|
->getQuery(); |
232
|
|
|
|
233
|
2 |
|
return $result; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function findByTitleWithUserRecommendedMovieQuery(string $query, int $userId, int $originalMovieId) |
237
|
|
|
{ |
238
|
|
|
$query = mb_strtolower($query); |
239
|
|
|
$result = $this->getBaseQuery() |
240
|
|
|
->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(mt.title) LIKE :title') |
241
|
|
|
->setParameter('title', "%{$query}%") |
242
|
|
|
->leftJoin('m.userRecommendedMovie', 'urm', 'WITH', 'urm.user = :user_id AND urm.originalMovie = :movie_id') |
243
|
|
|
->addSelect('urm') |
244
|
|
|
->setParameter('user_id', $userId) |
245
|
|
|
->setParameter('movie_id', $originalMovieId) |
246
|
|
|
->getQuery(); |
247
|
|
|
|
248
|
|
|
return $result; |
249
|
|
|
} |
250
|
|
|
|
251
|
7 |
|
public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null) |
252
|
|
|
{ |
253
|
7 |
|
if ($id === null && $tmdb_id === null) { |
254
|
|
|
throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided'); |
255
|
|
|
} |
256
|
|
|
|
257
|
7 |
|
return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|