diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index b7ffdd8..2aba97a 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -61,11 +61,13 @@ class TorrentController extends AbstractController 'sensitive' => $torrentService->findLastTorrentSensitive($torrent->getId())->isValue(), 'bookmark' => [ - 'active' => $torrentService->findUserLastTorrentBookmarkValue( + 'active' => (bool) $torrentService->findTorrentBookmark( $torrent->getId(), $user->getId() ), - 'total' => 0, + 'total' => $torrentService->findTorrentBookmarksTotalByTorrentId( + $torrent->getId() + ), ], 'pages' => [] ], diff --git a/src/Entity/TorrentBookmark.php b/src/Entity/TorrentBookmark.php index 2447d0d..c3ed994 100644 --- a/src/Entity/TorrentBookmark.php +++ b/src/Entity/TorrentBookmark.php @@ -22,9 +22,6 @@ class TorrentBookmark #[ORM\Column] private ?int $added = null; - #[ORM\Column] - private ?bool $value = null; - public function getId(): ?int { return $this->id; @@ -65,16 +62,4 @@ class TorrentBookmark return $this; } - - public function isValue(): ?bool - { - return $this->value; - } - - public function setValue(bool $value): static - { - $this->value = $value; - - return $this; - } } diff --git a/src/Repository/TorrentBookmarkRepository.php b/src/Repository/TorrentBookmarkRepository.php index e4b502a..1c8e326 100644 --- a/src/Repository/TorrentBookmarkRepository.php +++ b/src/Repository/TorrentBookmarkRepository.php @@ -21,7 +21,7 @@ class TorrentBookmarkRepository extends ServiceEntityRepository parent::__construct($registry, TorrentBookmark::class); } - public function findUserLastTorrentBookmark( + public function findTorrentBookmark( int $torrentId, int $userId ): ?TorrentBookmark @@ -37,4 +37,17 @@ class TorrentBookmarkRepository extends ServiceEntityRepository ->getOneOrNullResult() ; } + + public function findTorrentBookmarksTotalByTorrentId( + int $torrentId + ): int + { + return $this->createQueryBuilder('tb') + ->select('count(tb.id)') + ->where('tb.torrentId = :torrentId') + ->setParameter('torrentId', $torrentId) + ->getQuery() + ->getSingleScalarResult() + ; + } } diff --git a/src/Service/TorrentService.php b/src/Service/TorrentService.php index ec08834..cc89430 100644 --- a/src/Service/TorrentService.php +++ b/src/Service/TorrentService.php @@ -161,16 +161,21 @@ class TorrentService } /// Bookmark - public function findUserLastTorrentBookmarkValue(int $torrentId, int $userId): bool + public function findTorrentBookmark( + int $torrentId, + int $userId + ): ?TorrentBookmark { - if ($torrentBookmark = $this->entityManagerInterface - ->getRepository(TorrentBookmark::class) - ->findUserLastTorrentBookmark($torrentId, $userId)) - { - return $torrentBookmark->isValue(); - } + return $this->entityManagerInterface + ->getRepository(TorrentBookmark::class) + ->findTorrentBookmark($torrentId, $userId); + } - return false; + public function findTorrentBookmarksTotalByTorrentId(int $torrentId): int + { + return $this->entityManagerInterface + ->getRepository(TorrentBookmark::class) + ->findTorrentBookmarksTotalByTorrentId($torrentId); } // Update @@ -223,25 +228,25 @@ class TorrentService int $torrentId, int $userId, int $added - ): ?TorrentBookmark + ): void { - $torrentBookmark = new TorrentBookmark(); - - $torrentBookmark->setTorrentId($torrentId); - $torrentBookmark->setUserId($userId); - $torrentBookmark->setAdded($added); + if ($torrentBookmark = $this->findTorrentBookmark($torrentId, $userId)) + { + $this->entityManagerInterface->remove($torrentBookmark); + $this->entityManagerInterface->flush(); + } - $torrentBookmark->setValue( - !$this->findUserLastTorrentBookmarkValue( - $torrentId, - $userId - ) - ); + else + { + $torrentBookmark = new TorrentBookmark(); - $this->entityManagerInterface->persist($torrentBookmark); - $this->entityManagerInterface->flush(); + $torrentBookmark->setTorrentId($torrentId); + $torrentBookmark->setUserId($userId); + $torrentBookmark->setAdded($added); - return $torrentBookmark; + $this->entityManagerInterface->persist($torrentBookmark); + $this->entityManagerInterface->flush(); + } } public function deleteTorrentSensitive( diff --git a/templates/default/torrent/info.html.twig b/templates/default/torrent/info.html.twig index e4ec236..e731e2a 100644 --- a/templates/default/torrent/info.html.twig +++ b/templates/default/torrent/info.html.twig @@ -27,17 +27,22 @@