From 8ab6acf62013d2d7f072a77d42f73099825f26d5 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Thu, 21 Jun 2018 02:37:35 +0300 Subject: [PATCH] Torrent search and torrent show implemented. Simple imdex page implemented. --- config/packages/pagefanta.yaml | 2 ++ config/routes.yaml | 21 +++++++++---- public/css/style.css | 3 ++ src/Controller/MainController.php | 16 ++++++++++ src/Controller/TestController.php | 18 ----------- src/Controller/TorrentController.php | 40 +++++++++++++++++++++++++ src/Entity/Torrent.php | 4 +-- src/Repository/Torrent.php | 24 --------------- src/Repository/TorrentRepository.php | 45 ++++++++++++++++++++++++++++ templates/base.html.twig | 40 ++++++------------------- templates/index.html.twig | 7 +++++ templates/search_results.html.twig | 5 ++++ templates/torrent_list.html.twig | 31 ++++++++++--------- templates/torrent_show.html.twig | 43 ++++++++++++++++++++++++++ 14 files changed, 202 insertions(+), 97 deletions(-) create mode 100644 config/packages/pagefanta.yaml create mode 100644 src/Controller/MainController.php delete mode 100644 src/Controller/TestController.php create mode 100644 src/Controller/TorrentController.php delete mode 100644 src/Repository/Torrent.php create mode 100644 src/Repository/TorrentRepository.php create mode 100644 templates/index.html.twig create mode 100644 templates/search_results.html.twig create mode 100644 templates/torrent_show.html.twig diff --git a/config/packages/pagefanta.yaml b/config/packages/pagefanta.yaml new file mode 100644 index 0000000..e9d6dd9 --- /dev/null +++ b/config/packages/pagefanta.yaml @@ -0,0 +1,2 @@ +white_october_pagerfanta: + default_view: twitter_bootstrap4 diff --git a/config/routes.yaml b/config/routes.yaml index a808cf9..a82b509 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -1,7 +1,16 @@ -#index: -# path: / -# controller: App\Controller\DefaultController::index - -test: +index: path: / - controller: App\Controller\TestController::test \ No newline at end of file + controller: App\Controller\MainController::index + +torrent_show: + path: /torrents/{id} + controller: App\Controller\TorrentController::showTorrent + requirements: + method: GET + id: '\d+' + +torrent_search: + path: /torrents/search + controller: App\Controller\TorrentController::searchTorrent + requirements: + method: GET \ No newline at end of file diff --git a/public/css/style.css b/public/css/style.css index e69de29..93e308f 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -0,0 +1,3 @@ +#content { + margin-top: 80px; +} \ No newline at end of file diff --git a/src/Controller/MainController.php b/src/Controller/MainController.php new file mode 100644 index 0000000..096e20f --- /dev/null +++ b/src/Controller/MainController.php @@ -0,0 +1,16 @@ +render('index.html.twig', [ + 'torrentsCount' => $repo->getTorrentsTotalCount(), + ]); + } +} \ No newline at end of file diff --git a/src/Controller/TestController.php b/src/Controller/TestController.php deleted file mode 100644 index 0afd011..0000000 --- a/src/Controller/TestController.php +++ /dev/null @@ -1,18 +0,0 @@ -getLastTorrents(10); - - return $this->render('torrent_list.html.twig', [ - 'torrents' => $torrents, - ]); - } -} \ No newline at end of file diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php new file mode 100644 index 0000000..bd55e21 --- /dev/null +++ b/src/Controller/TorrentController.php @@ -0,0 +1,40 @@ +render('torrent_show.html.twig', [ + 'torrent' => $torrent, + ]); + } + + public function searchTorrent(Request $request, TorrentRepository $repo) + { + $query = $request->query->get('query', ''); + $page = (int) $request->query->get('page', '1'); + + $pagerAdapter = new DoctrineORMAdapter($repo->createFindLikeQueryBuilder($query)); + $pager = new Pagerfanta($pagerAdapter); + $pager + ->setCurrentPage($page) + ->setMaxPerPage(self::PER_PAGE) + ; + + return $this->render('search_results.html.twig', [ + 'torrents' => $pager, + 'searchQuery' => $query, + ]); + } +} \ No newline at end of file diff --git a/src/Entity/Torrent.php b/src/Entity/Torrent.php index 4c29697..054edb9 100644 --- a/src/Entity/Torrent.php +++ b/src/Entity/Torrent.php @@ -10,7 +10,7 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Index(name="discovered_on_index", columns={"discovered_on"}), * @ORM\Index(name="info_hash_index", columns={"info_hash"}) * }) - * @ORM\Entity(readOnly=true) + * @ORM\Entity(readOnly=true, repositoryClass="App\Repository\TorrentRepository") */ class Torrent { @@ -67,7 +67,7 @@ class Torrent return $this->infoHash; } - public function getInfoHashAsHex() + public function getInfoHashAsHex(): string { return bin2hex(stream_get_contents($this->infoHash)); } diff --git a/src/Repository/Torrent.php b/src/Repository/Torrent.php deleted file mode 100644 index fcec593..0000000 --- a/src/Repository/Torrent.php +++ /dev/null @@ -1,24 +0,0 @@ -createQueryBuilder('t') - ->orderBy('t.discoveredOn', 'DESC') - ->setMaxResults($number) - ; - - return $qb->getQuery()->getResult(); - } -} \ No newline at end of file diff --git a/src/Repository/TorrentRepository.php b/src/Repository/TorrentRepository.php new file mode 100644 index 0000000..04b8c02 --- /dev/null +++ b/src/Repository/TorrentRepository.php @@ -0,0 +1,45 @@ +createQueryBuilder('t') + ->select('COUNT(t.id)') + ; + + return (int) $qb->getQuery()->getSingleScalarResult(); + } + + public function createFindLikeQueryBuilder(string $query): QueryBuilder + { + $qb = $this->createQueryBuilder('t'); + + $where = $qb->expr()->andX(); + + $query = trim($query); + $query = preg_replace('/\s+/', ' ', $query); + + $parts = explode(' ', $query); + + foreach ($parts as $idx => $part) { + $where->add($qb->expr()->like('LOWER(t.name)', ':part_'.$idx)); + $qb->setParameter('part_'.$idx, '%'.strtolower($part).'%'); + } + + $qb->where($where); + + return $qb; + } +} \ No newline at end of file diff --git a/templates/base.html.twig b/templates/base.html.twig index e4511ee..16805fb 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -7,63 +7,41 @@ {% endblock %} - + {% block title %}Magnetoco Web{% endblock %} {% block css %} - - - + {% endblock %} -
- {% block content %} -
-

Bootstrap starter template

-

Use this document as a way to quickly start any new project.
All you get is this text and a mostly barebones HTML document.

-
- {% endblock %} +
+ {% block content %}{% endblock %}
{% block javascript %} diff --git a/templates/index.html.twig b/templates/index.html.twig new file mode 100644 index 0000000..24e45fb --- /dev/null +++ b/templates/index.html.twig @@ -0,0 +1,7 @@ +{% extends 'base.html.twig' %} + +{% block content %} +
+

Torrents indexed: {{ torrentsCount }}

+
+{% endblock %} \ No newline at end of file diff --git a/templates/search_results.html.twig b/templates/search_results.html.twig new file mode 100644 index 0000000..4c3de3b --- /dev/null +++ b/templates/search_results.html.twig @@ -0,0 +1,5 @@ +{% extends 'base.html.twig' %} + +{% block content %} + {% include 'torrent_list.html.twig' with {'torrents': torrents} %} +{% endblock %} diff --git a/templates/torrent_list.html.twig b/templates/torrent_list.html.twig index bc34c51..414f0d8 100644 --- a/templates/torrent_list.html.twig +++ b/templates/torrent_list.html.twig @@ -1,33 +1,32 @@ -{% extends 'base.html.twig' %} - {% block content %} + + + - - + - {# @var torrent \App\Entity\\App\Entity\Torrent #} + {# @var torrent \App\Entity\Torrent #} {% for torrent in torrents %} - - - + + {% endfor %}
ID Name Size DiscoveredFilesLink
{{ torrent.id }} - {{ torrent.name }} + {{ torrent.name }} {{ torrent.totalSize }}{{ torrent.discoveredOn }}{{ (torrent.totalSize / 1024 / 1024) | round(2, 'ceil')}} MB{{ torrent.discoveredOn | date('Y-m-d H:i:s')}} - {# -
    - {% for file in torrent.files %} -
  • {{ file.path }}
  • - {% endfor %} -
- #} + 🔗
+ + {% endblock %} \ No newline at end of file diff --git a/templates/torrent_show.html.twig b/templates/torrent_show.html.twig new file mode 100644 index 0000000..a781251 --- /dev/null +++ b/templates/torrent_show.html.twig @@ -0,0 +1,43 @@ +{% extends 'base.html.twig' %} + +{% block content %} + {# @var torrent \App\Entity\Torrent #} + + + + + + + + + + + + + + + + + +
Name{{ torrent.name }}
Hash + {{ torrent.infoHashAsHex }} +
Size{{ (torrent.totalSize / 1024 / 1024) | round(2, 'ceil')}} MB
Discovered{{ torrent.discoveredOn | date('Y-m-d H:i:s')}}
+ + + + + + + + + + {# @var file \App\Entity\File #} + {% for file in torrent.files | sort %} + + + + + {% endfor %} + +
FileSize
{{ file.path }}{{ (file.size / 1024 / 1024) | round(2, 'ceil')}} MB
+{% endblock %} \ No newline at end of file