implement bookmarks total counter

This commit is contained in:
ghost 2023-10-08 20:37:50 +03:00
parent 3d74818303
commit 5b0a7bcb69
5 changed files with 62 additions and 52 deletions

View file

@ -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' => []
],

View file

@ -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;
}
}

View file

@ -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()
;
}
}

View file

@ -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();
if ($torrentBookmark = $this->findTorrentBookmark($torrentId, $userId))
{
$this->entityManagerInterface->remove($torrentBookmark);
$this->entityManagerInterface->flush();
}
$torrentBookmark->setTorrentId($torrentId);
$torrentBookmark->setUserId($userId);
$torrentBookmark->setAdded($added);
else
{
$torrentBookmark = new TorrentBookmark();
$torrentBookmark->setValue(
!$this->findUserLastTorrentBookmarkValue(
$torrentId,
$userId
)
);
$torrentBookmark->setTorrentId($torrentId);
$torrentBookmark->setUserId($userId);
$torrentBookmark->setAdded($added);
$this->entityManagerInterface->persist($torrentBookmark);
$this->entityManagerInterface->flush();
return $torrentBookmark;
$this->entityManagerInterface->persist($torrentBookmark);
$this->entityManagerInterface->flush();
}
}
public function deleteTorrentSensitive(