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. 51
      src/Service/TorrentService.php
  5. 27
      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()
;
}
}

51
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
->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 @@ -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(

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

@ -27,17 +27,22 @@ @@ -27,17 +27,22 @@
<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 }}">
{% 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"/>
</svg>
{% else %}
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M2.866 14.85c-.078.444.36.791.746.593l4.39-2.256 4.389 2.256c.386.198.824-.149.746-.592l-.83-4.73 3.522-3.356c.33-.314.16-.888-.282-.95l-4.898-.696L8.465.792a.513.513 0 0 0-.927 0L5.354 5.12l-4.898.696c-.441.062-.612.636-.283.95l3.523 3.356-.83 4.73zm4.905-2.767-3.686 1.894.694-3.957a.565.565 0 0 0-.163-.505L1.71 6.745l4.052-.576a.525.525 0 0 0 .393-.288L8 2.223l1.847 3.658a.525.525 0 0 0 .393.288l4.052.575-2.906 2.77a.565.565 0 0 0-.163.506l.694 3.957-3.686-1.894a.503.503 0 0 0-.461 0z"/>
</svg>
{% endif %}
</a>
<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"/>
</svg>
{% else %}
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M2.866 14.85c-.078.444.36.791.746.593l4.39-2.256 4.389 2.256c.386.198.824-.149.746-.592l-.83-4.73 3.522-3.356c.33-.314.16-.888-.282-.95l-4.898-.696L8.465.792a.513.513 0 0 0-.927 0L5.354 5.12l-4.898.696c-.441.062-.612.636-.283.95l3.523 3.356-.83 4.73zm4.905-2.767-3.686 1.894.694-3.957a.565.565 0 0 0-.163-.505L1.71 6.745l4.052-.576a.525.525 0 0 0 .393-.288L8 2.223l1.847 3.658a.525.525 0 0 0 .393.288l4.052.575-2.906 2.77a.565.565 0 0 0-.163.506l.694 3.957-3.686-1.894a.503.503 0 0 0-.461 0z"/>
</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