Browse Source

implement bookmarks total counter

main
ghost 1 year ago
parent
commit
5b0a7bcb69
  1. 6
      src/Controller/TorrentController.php
  2. 15
      src/Entity/TorrentBookmark.php
  3. 15
      src/Repository/TorrentBookmarkRepository.php
  4. 37
      src/Service/TorrentService.php
  5. 7
      templates/default/torrent/info.html.twig

6
src/Controller/TorrentController.php

@ -61,11 +61,13 @@ class TorrentController extends AbstractController @@ -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' => []
],

15
src/Entity/TorrentBookmark.php

@ -22,9 +22,6 @@ class TorrentBookmark @@ -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 @@ -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;
}
}

15
src/Repository/TorrentBookmarkRepository.php

@ -21,7 +21,7 @@ class TorrentBookmarkRepository extends ServiceEntityRepository @@ -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 @@ -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()
;
}
}

37
src/Service/TorrentService.php

@ -161,16 +161,21 @@ class TorrentService @@ -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
return $this->entityManagerInterface
->getRepository(TorrentBookmark::class)
->findUserLastTorrentBookmark($torrentId, $userId))
{
return $torrentBookmark->isValue();
->findTorrentBookmark($torrentId, $userId);
}
return false;
public function findTorrentBookmarksTotalByTorrentId(int $torrentId): int
{
return $this->entityManagerInterface
->getRepository(TorrentBookmark::class)
->findTorrentBookmarksTotalByTorrentId($torrentId);
}
// Update
@ -223,7 +228,15 @@ class TorrentService @@ -223,7 +228,15 @@ class TorrentService
int $torrentId,
int $userId,
int $added
): ?TorrentBookmark
): void
{
if ($torrentBookmark = $this->findTorrentBookmark($torrentId, $userId))
{
$this->entityManagerInterface->remove($torrentBookmark);
$this->entityManagerInterface->flush();
}
else
{
$torrentBookmark = new TorrentBookmark();
@ -231,17 +244,9 @@ class TorrentService @@ -231,17 +244,9 @@ class TorrentService
$torrentBookmark->setUserId($userId);
$torrentBookmark->setAdded($added);
$torrentBookmark->setValue(
!$this->findUserLastTorrentBookmarkValue(
$torrentId,
$userId
)
);
$this->entityManagerInterface->persist($torrentBookmark);
$this->entityManagerInterface->flush();
return $torrentBookmark;
}
}
public function deleteTorrentSensitive(

7
templates/default/torrent/info.html.twig

@ -27,7 +27,8 @@ @@ -27,7 +27,8 @@
<h1>
{{ 'Torrent'|trans }} #{{ torrent.id }}
</h1>
<a class="float-right margin-l-8-px" href="{{ path('torrent_bookmark_toggle', {torrentId : torrent.id}) }}" title="{{ 'Bookmark'|trans }}">
<div class="float-right ">
<a class="margin-x-4-px" href="{{ path('torrent_bookmark_toggle', {torrentId : torrent.id}) }}" title="{{ 'Bookmark'|trans }}">
{% if torrent.bookmark.active %}
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z"/>
@ -38,6 +39,10 @@ @@ -38,6 +39,10 @@
</svg>
{% endif %}
</a>
<sup>
{{ torrent.bookmark.total }}
</sup>
</div>
{#
<a class="float-right margin-l-8-px" href="#" title="{{ 'Torrent'|trans }}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">

Loading…
Cancel
Save