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
'sensitive' => $torrentService->findLastTorrentSensitive($torrent->getId())->isValue(), 'sensitive' => $torrentService->findLastTorrentSensitive($torrent->getId())->isValue(),
'bookmark' => 'bookmark' =>
[ [
'active' => $torrentService->findUserLastTorrentBookmarkValue( 'active' => (bool) $torrentService->findTorrentBookmark(
$torrent->getId(), $torrent->getId(),
$user->getId() $user->getId()
), ),
'total' => 0, 'total' => $torrentService->findTorrentBookmarksTotalByTorrentId(
$torrent->getId()
),
], ],
'pages' => [] 'pages' => []
], ],

15
src/Entity/TorrentBookmark.php

@ -22,9 +22,6 @@ class TorrentBookmark
#[ORM\Column] #[ORM\Column]
private ?int $added = null; private ?int $added = null;
#[ORM\Column]
private ?bool $value = null;
public function getId(): ?int public function getId(): ?int
{ {
return $this->id; return $this->id;
@ -65,16 +62,4 @@ class TorrentBookmark
return $this; 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
parent::__construct($registry, TorrentBookmark::class); parent::__construct($registry, TorrentBookmark::class);
} }
public function findUserLastTorrentBookmark( public function findTorrentBookmark(
int $torrentId, int $torrentId,
int $userId int $userId
): ?TorrentBookmark ): ?TorrentBookmark
@ -37,4 +37,17 @@ class TorrentBookmarkRepository extends ServiceEntityRepository
->getOneOrNullResult() ->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
} }
/// Bookmark /// 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) ->getRepository(TorrentBookmark::class)
->findUserLastTorrentBookmark($torrentId, $userId)) ->findTorrentBookmark($torrentId, $userId);
{ }
return $torrentBookmark->isValue();
}
return false; public function findTorrentBookmarksTotalByTorrentId(int $torrentId): int
{
return $this->entityManagerInterface
->getRepository(TorrentBookmark::class)
->findTorrentBookmarksTotalByTorrentId($torrentId);
} }
// Update // Update
@ -223,25 +228,25 @@ class TorrentService
int $torrentId, int $torrentId,
int $userId, int $userId,
int $added int $added
): ?TorrentBookmark ): void
{ {
$torrentBookmark = new TorrentBookmark(); if ($torrentBookmark = $this->findTorrentBookmark($torrentId, $userId))
{
$torrentBookmark->setTorrentId($torrentId); $this->entityManagerInterface->remove($torrentBookmark);
$torrentBookmark->setUserId($userId); $this->entityManagerInterface->flush();
$torrentBookmark->setAdded($added); }
$torrentBookmark->setValue( else
!$this->findUserLastTorrentBookmarkValue( {
$torrentId, $torrentBookmark = new TorrentBookmark();
$userId
)
);
$this->entityManagerInterface->persist($torrentBookmark); $torrentBookmark->setTorrentId($torrentId);
$this->entityManagerInterface->flush(); $torrentBookmark->setUserId($userId);
$torrentBookmark->setAdded($added);
return $torrentBookmark; $this->entityManagerInterface->persist($torrentBookmark);
$this->entityManagerInterface->flush();
}
} }
public function deleteTorrentSensitive( public function deleteTorrentSensitive(

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

@ -27,17 +27,22 @@
<h1> <h1>
{{ 'Torrent'|trans }} #{{ torrent.id }} {{ 'Torrent'|trans }} #{{ torrent.id }}
</h1> </h1>
<a class="float-right margin-l-8-px" href="{{ path('torrent_bookmark_toggle', {torrentId : torrent.id}) }}" title="{{ 'Bookmark'|trans }}"> <div class="float-right ">
{% if torrent.bookmark.active %} <a class="margin-x-4-px" href="{{ path('torrent_bookmark_toggle', {torrentId : torrent.id}) }}" title="{{ 'Bookmark'|trans }}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16"> {% if torrent.bookmark.active %}
<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 xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
</svg> <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"/>
{% else %} </svg>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16"> {% else %}
<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 xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
</svg> <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"/>
{% endif %} </svg>
</a> {% endif %}
</a>
<sup>
{{ torrent.bookmark.total }}
</sup>
</div>
{# {#
<a class="float-right margin-l-8-px" href="#" title="{{ 'Torrent'|trans }}"> <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"> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">

Loading…
Cancel
Save