From 285a5104e2f982bfea28818a2a8812dcc9ceb9bd Mon Sep 17 00:00:00 2001 From: ghost Date: Mon, 9 Oct 2023 20:04:52 +0300 Subject: [PATCH] make common methods optimization --- .../TorrentDownloadFileRepository.php | 17 ---- .../TorrentDownloadMagnetRepository.php | 17 ---- src/Repository/TorrentLocalesRepository.php | 33 ------- src/Repository/TorrentRepository.php | 20 ---- src/Repository/TorrentSensitiveRepository.php | 33 ------- src/Repository/TorrentStarRepository.php | 23 +---- src/Repository/UserRepository.php | 20 ---- src/Repository/UserStarRepository.php | 16 --- src/Service/TorrentService.php | 98 ++++++++++++++----- src/Service/UserService.php | 15 ++- 10 files changed, 91 insertions(+), 201 deletions(-) diff --git a/src/Repository/TorrentDownloadFileRepository.php b/src/Repository/TorrentDownloadFileRepository.php index 69b6140..4052176 100644 --- a/src/Repository/TorrentDownloadFileRepository.php +++ b/src/Repository/TorrentDownloadFileRepository.php @@ -21,23 +21,6 @@ class TorrentDownloadFileRepository extends ServiceEntityRepository parent::__construct($registry, TorrentDownloadFile::class); } - public function findTorrentDownloadFile( - int $torrentId, - int $userId - ): ?TorrentDownloadFile - { - return $this->createQueryBuilder('tdf') - ->where('tdf.torrentId = :torrentId') - ->andWhere('tdf.userId = :userId') - ->setParameter('torrentId', $torrentId) - ->setParameter('userId', $userId) - ->orderBy('tdf.id', 'DESC') // same to ts.added - ->setMaxResults(1) - ->getQuery() - ->getOneOrNullResult() - ; - } - public function findTorrentDownloadFilesTotalByTorrentId( int $torrentId ): int diff --git a/src/Repository/TorrentDownloadMagnetRepository.php b/src/Repository/TorrentDownloadMagnetRepository.php index 42612d0..ef4e706 100644 --- a/src/Repository/TorrentDownloadMagnetRepository.php +++ b/src/Repository/TorrentDownloadMagnetRepository.php @@ -21,23 +21,6 @@ class TorrentDownloadMagnetRepository extends ServiceEntityRepository parent::__construct($registry, TorrentDownloadMagnet::class); } - public function findTorrentDownloadMagnet( - int $torrentId, - int $userId - ): ?TorrentDownloadMagnet - { - return $this->createQueryBuilder('tdm') - ->where('tdm.torrentId = :torrentId') - ->andWhere('tdm.userId = :userId') - ->setParameter('torrentId', $torrentId) - ->setParameter('userId', $userId) - ->orderBy('tdm.id', 'DESC') // same to ts.added - ->setMaxResults(1) - ->getQuery() - ->getOneOrNullResult() - ; - } - public function findTorrentDownloadMagnetsTotalByTorrentId( int $torrentId ): int diff --git a/src/Repository/TorrentLocalesRepository.php b/src/Repository/TorrentLocalesRepository.php index 4901596..8ba7b73 100644 --- a/src/Repository/TorrentLocalesRepository.php +++ b/src/Repository/TorrentLocalesRepository.php @@ -20,37 +20,4 @@ class TorrentLocalesRepository extends ServiceEntityRepository { parent::__construct($registry, TorrentLocales::class); } - - public function getTorrentLocales(int $id): ?TorrentLocales - { - return $this->createQueryBuilder('tl') - ->where('tl.id = :id') - ->setParameter('id', $id) - ->getQuery() - ->getOneOrNullResult() - ; - } - - public function findLastTorrentLocalesByTorrentId(int $torrentId): ?TorrentLocales - { - return $this->createQueryBuilder('tl') - ->where('tl.torrentId = :torrentId') - ->setParameter('torrentId', $torrentId) - ->orderBy('tl.id', 'DESC') // same to tl.added - ->setMaxResults(1) - ->getQuery() - ->getOneOrNullResult() - ; - } - - public function findTorrentLocalesByTorrentId(int $torrentId): array - { - return $this->createQueryBuilder('tl') - ->where('tl.torrentId = :torrentId') - ->setParameter('torrentId', $torrentId) - ->orderBy('tl.id', 'DESC') // same to tl.added - ->getQuery() - ->getResult() - ; - } } diff --git a/src/Repository/TorrentRepository.php b/src/Repository/TorrentRepository.php index d40022b..53b6358 100644 --- a/src/Repository/TorrentRepository.php +++ b/src/Repository/TorrentRepository.php @@ -20,24 +20,4 @@ class TorrentRepository extends ServiceEntityRepository { parent::__construct($registry, Torrent::class); } - - public function getTorrent(int $id): ?Torrent - { - return $this->createQueryBuilder('t') - ->where('t.id = :id') - ->setParameter('id', $id) - ->getQuery() - ->getOneOrNullResult() - ; - } - - public function getTorrentScrapeQueue(): ?Torrent - { - return $this->createQueryBuilder('t') - ->orderBy('t.scraped', 'ASC') // same to ts.added - ->setMaxResults(1) - ->getQuery() - ->getOneOrNullResult() - ; - } } diff --git a/src/Repository/TorrentSensitiveRepository.php b/src/Repository/TorrentSensitiveRepository.php index 8e02e99..e21c7e6 100644 --- a/src/Repository/TorrentSensitiveRepository.php +++ b/src/Repository/TorrentSensitiveRepository.php @@ -20,37 +20,4 @@ class TorrentSensitiveRepository extends ServiceEntityRepository { parent::__construct($registry, TorrentSensitive::class); } - - public function getTorrentSensitive(int $id): ?TorrentSensitive - { - return $this->createQueryBuilder('ts') - ->where('ts.id = :id') - ->setParameter('id', $id) - ->getQuery() - ->getOneOrNullResult() - ; - } - - public function findLastTorrentSensitiveByTorrentId(int $torrentId): ?TorrentSensitive - { - return $this->createQueryBuilder('ts') - ->where('ts.torrentId = :torrentId') - ->setParameter('torrentId', $torrentId) - ->orderBy('ts.id', 'DESC') // same to ts.added - ->setMaxResults(1) - ->getQuery() - ->getOneOrNullResult() - ; - } - - public function findTorrentSensitiveByTorrentId(int $torrentId): array - { - return $this->createQueryBuilder('ts') - ->where('ts.torrentId = :torrentId') - ->setParameter('torrentId', $torrentId) - ->orderBy('ts.id', 'DESC') // same to ts.added - ->getQuery() - ->getResult() - ; - } } diff --git a/src/Repository/TorrentStarRepository.php b/src/Repository/TorrentStarRepository.php index bbb6b17..978aa95 100644 --- a/src/Repository/TorrentStarRepository.php +++ b/src/Repository/TorrentStarRepository.php @@ -21,30 +21,13 @@ class TorrentStarRepository extends ServiceEntityRepository parent::__construct($registry, TorrentStar::class); } - public function findTorrentStar( - int $torrentId, - int $userId - ): ?TorrentStar - { - return $this->createQueryBuilder('tb') - ->where('tb.torrentId = :torrentId') - ->andWhere('tb.userId = :userId') - ->setParameter('torrentId', $torrentId) - ->setParameter('userId', $userId) - ->orderBy('tb.id', 'DESC') // same to ts.added - ->setMaxResults(1) - ->getQuery() - ->getOneOrNullResult() - ; - } - public function findTorrentStarsTotalByTorrentId( int $torrentId ): int { - return $this->createQueryBuilder('tb') - ->select('count(tb.id)') - ->where('tb.torrentId = :torrentId') + return $this->createQueryBuilder('ts') + ->select('count(ts.id)') + ->where('ts.torrentId = :torrentId') ->setParameter('torrentId', $torrentId) ->getQuery() ->getSingleScalarResult() diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index c3fbb67..86a987a 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -20,24 +20,4 @@ class UserRepository extends ServiceEntityRepository { parent::__construct($registry, User::class); } - - public function getUser(int $id): ?User - { - return $this->createQueryBuilder('u') - ->where('u.id = :id') - ->setParameter('id', $id) - ->getQuery() - ->getOneOrNullResult() - ; - } - - public function findOneByAddressField(string $address): ?User - { - return $this->createQueryBuilder('u') - ->where('u.address = :address') - ->setParameter('address', $address) - ->getQuery() - ->getOneOrNullResult() - ; - } } diff --git a/src/Repository/UserStarRepository.php b/src/Repository/UserStarRepository.php index fa6bae7..2a4f295 100644 --- a/src/Repository/UserStarRepository.php +++ b/src/Repository/UserStarRepository.php @@ -21,22 +21,6 @@ class UserStarRepository extends ServiceEntityRepository parent::__construct($registry, UserStar::class); } - public function findUserStar( - int $userId, - int $userIdTarget - ): ?UserStar - { - return $this->createQueryBuilder('us') - ->where('us.userId = :userId') - ->andWhere('us.userIdTarget = :userIdTarget') - ->setParameter('userId', $userId) - ->setParameter('userIdTarget', $userIdTarget) - ->setMaxResults(1) - ->getQuery() - ->getOneOrNullResult() - ; - } - public function findUserStarsTotalByUserIdTarget( int $userIdTarget ): int diff --git a/src/Service/TorrentService.php b/src/Service/TorrentService.php index 5bb5b32..32f300c 100644 --- a/src/Service/TorrentService.php +++ b/src/Service/TorrentService.php @@ -250,7 +250,7 @@ class TorrentService { return $this->entityManagerInterface ->getRepository(Torrent::class) - ->getTorrent($id); + ->find($id); } public function addTorrent( @@ -277,7 +277,12 @@ class TorrentService { return $this->entityManagerInterface ->getRepository(Torrent::class) - ->getTorrentScrapeQueue(); + ->findOneBy( + [], + [ + 'scraped' => 'ASC' + ] + ); } public function setTorrentsApprovedByUserId( @@ -302,32 +307,44 @@ class TorrentService } // Torrent locale - public function getTorrentLocales(int $id): ?TorrentLocales + public function getTorrentLocales( + int $torrentLocaleId + ): ?TorrentLocales { return $this->entityManagerInterface ->getRepository(TorrentLocales::class) - ->getTorrentLocales($id); + ->find($torrentLocaleId); } - public function findLastTorrentLocalesByTorrentId(int $torrentId): ?TorrentLocales + public function findLastTorrentLocalesByTorrentId( + int $torrentId + ): ?TorrentLocales { return $this->entityManagerInterface ->getRepository(TorrentLocales::class) - ->findLastTorrentLocalesByTorrentId($torrentId); + ->findOneBy( + [ + 'torrentId' => $torrentId + ] + ); } public function findTorrentLocalesByTorrentId(int $torrentId): array { return $this->entityManagerInterface ->getRepository(TorrentLocales::class) - ->findTorrentLocalesByTorrentId($torrentId); + ->findBy( + [ + 'torrentId' => $torrentId + ] + ); } public function toggleTorrentLocalesApproved( - int $id + int $torrentLocalesId ): ?TorrentLocales { - $torrentLocales = $this->getTorrentLocales($id); + $torrentLocales = $this->getTorrentLocales($torrentLocalesId); $torrentLocales->setApproved( !$torrentLocales->isApproved() // toggle current value @@ -340,10 +357,10 @@ class TorrentService } public function deleteTorrentLocales( - int $id + int $torrentLocalesId ): ?TorrentLocales { - $torrentLocales = $this->getTorrentLocales($id); + $torrentLocales = $this->getTorrentLocales($torrentLocalesId); $this->entityManagerInterface->remove($torrentLocales); $this->entityManagerInterface->flush(); @@ -395,32 +412,52 @@ class TorrentService } // Torrent sensitive - public function getTorrentSensitive(int $id): ?TorrentSensitive + public function getTorrentSensitive( + int $torrentSensitiveId + ): ?TorrentSensitive { return $this->entityManagerInterface ->getRepository(TorrentSensitive::class) - ->getTorrentSensitive($id); + ->find( + $torrentSensitiveId + ); } public function findLastTorrentSensitiveByTorrentId(int $torrentId): ?TorrentSensitive { return $this->entityManagerInterface ->getRepository(TorrentSensitive::class) - ->findLastTorrentSensitiveByTorrentId($torrentId); + ->findOneBy( + [ + 'torrentId' => $torrentId + ], + [ + 'id' => 'DESC' + ] + ); } public function findTorrentSensitiveByTorrentId(int $torrentId): array { return $this->entityManagerInterface ->getRepository(TorrentSensitive::class) - ->findTorrentSensitiveByTorrentId($torrentId); + ->findBy( + [ + 'torrentId' => $torrentId + ], + [ + 'id' => 'DESC' + ] + ); } public function toggleTorrentSensitiveApproved( - int $id + int $torrentSensitiveId ): ?TorrentSensitive { - $torrentSensitive = $this->getTorrentSensitive($id); + $torrentSensitive = $this->find( + $torrentSensitiveId + ); $torrentSensitive->setApproved( !$torrentSensitive->isApproved() // toggle current value @@ -433,10 +470,12 @@ class TorrentService } public function deleteTorrentSensitive( - int $id + int $torrentSensitiveId ): ?TorrentSensitive { - $torrentSensitive = $this->getTorrentSensitive($id); + $torrentSensitive = $this->getTorrentSensitive( + $torrentSensitiveId + ); $this->entityManagerInterface->remove($torrentSensitive); $this->entityManagerInterface->flush(); @@ -495,7 +534,12 @@ class TorrentService { return $this->entityManagerInterface ->getRepository(TorrentStar::class) - ->findTorrentStar($torrentId, $userId); + ->findOneBy( + [ + 'torrentId' => $torrentId, + 'userId' => $userId, + ] + ); } public function findTorrentStarsTotalByTorrentId(int $torrentId): int @@ -538,7 +582,12 @@ class TorrentService { return $this->entityManagerInterface ->getRepository(TorrentDownloadFile::class) - ->findTorrentDownloadFile($torrentId, $userId); + ->findOneBy( + [ + 'torrentId' => $torrentId, + 'userId' => $userId + ] + ); } public function findTorrentDownloadFilesTotalByTorrentId(int $torrentId): int @@ -575,7 +624,12 @@ class TorrentService { return $this->entityManagerInterface ->getRepository(TorrentDownloadMagnet::class) - ->findTorrentDownloadMagnet($torrentId, $userId); + ->findOneBy( + [ + 'torrentId' => $torrentId, + 'userId' => $userId + ] + ); } public function findTorrentDownloadMagnetsTotalByTorrentId(int $torrentId): int diff --git a/src/Service/UserService.php b/src/Service/UserService.php index 2752fa2..3774a1c 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -28,7 +28,11 @@ class UserService // Return existing user if ($result = $this->entityManagerInterface ->getRepository(User::class) - ->findOneByAddressField($address)) + ->findOneBy( + [ + 'address' => $address + ] + )) { return $result; } @@ -91,7 +95,7 @@ class UserService { return $this->entityManagerInterface ->getRepository(User::class) - ->getUser($userId); + ->find($userId); } public function identicon( @@ -128,7 +132,12 @@ class UserService { return $this->entityManagerInterface ->getRepository(UserStar::class) - ->findUserStar($userId, $userIdTarget); + ->findOneBy( + [ + 'userId' => $userId, + 'userIdTarget' => $userIdTarget + ] + ); } public function findUserStarsTotalByUserIdTarget(int $torrentId): int