implement events pagination

This commit is contained in:
ghost 2023-10-12 02:19:57 +03:00
parent cc6c68957c
commit 995d4bde54
10 changed files with 388 additions and 22 deletions

View file

@ -17,11 +17,16 @@ class TorrentController extends AbstractController
{
// Torrent
#[Route(
'/{_locale}/torrent/{torrentId}',
'/{_locale}/torrent/{torrentId}/{page}',
name: 'torrent_info',
requirements:
[
'torrentId' => '\d+'
'torrentId' => '\d+',
'page' => '\d+',
],
defaults:
[
'page' => 1,
],
methods:
[
@ -29,6 +34,7 @@ class TorrentController extends AbstractController
]
)]
public function info(
int $page,
Request $request,
TranslatorInterface $translator,
UserService $userService,
@ -82,6 +88,12 @@ class TorrentController extends AbstractController
);
}
// Get total activities
$total = $activityService->findActivitiesTotalByTorrentId(
$torrent->getId(),
$user->getEvents()
);
// Render template
return $this->render('default/torrent/info.html.twig', [
'torrent' =>
@ -166,6 +178,18 @@ class TorrentController extends AbstractController
// 'magnet' => $file->getMagnetLink()
],
'trackers' => explode('|', $this->getParameter('app.trackers')),
'activities' => $activityService->findLastActivitiesByTorrentId(
$torrent->getId(),
$user->getEvents(),
$this->getParameter('app.pagination'),
($page - 1) * $this->getParameter('app.pagination')
),
'pagination' =>
[
'page' => $page,
'pages' => ceil($total / $this->getParameter('app.pagination')),
'total' => $total
]
]);
}

View file

@ -39,10 +39,23 @@ class UserController extends AbstractController
}
#[Route(
'/{_locale}',
name: 'user_dashboard'
'/{_locale}/{page}',
name: 'user_dashboard',
requirements:
[
'page' => '\d+',
],
defaults:
[
'page' => 1,
],
methods:
[
'GET'
]
)]
public function index(
int $page,
Request $request,
UserService $userService,
ActivityService $activityService
@ -54,12 +67,24 @@ class UserController extends AbstractController
$activityService
);
$total = $activityService->findActivitiesTotal(
$user->getEvents()
);
return $this->render(
'default/user/dashboard.html.twig',
[
'activities' => $activityService->findLastActivities(
$user->getEvents()
)
$user->getEvents(),
$this->getParameter('app.pagination'),
($page - 1) * $this->getParameter('app.pagination')
),
'pagination' =>
[
'page' => $page,
'pages' => ceil($total / $this->getParameter('app.pagination')),
'total' => $total
]
]
);
}
@ -181,18 +206,22 @@ class UserController extends AbstractController
}
#[Route(
'/{_locale}/profile/{userId}',
'/{_locale}/profile/{userId}/{page}',
name: 'user_info',
defaults: [
'_locale' => '%app.locale%',
'userId' => null
'userId' => 0,
'page' => 1,
],
requirements: [
'_locale' => '%app.locales%',
'userId' => '\d+',
'userId' => '\d+',
'page' => '\d+',
],
)]
public function info(
int $userId,
int $page,
Request $request,
TranslatorInterface $translator,
UserService $userService,
@ -216,12 +245,18 @@ class UserController extends AbstractController
// Init target user
if (!$userTarget = $userService->getUser(
$request->get('userId') ? $request->get('userId') : $user->getId()
$userId ? $userId : $user->getId()
))
{
throw $this->createNotFoundException();
}
// Get total activities
$total = $activityService->findActivitiesTotalByUserId(
$userTarget->getId(),
$user->getEvents()
);
// Render template
return $this->render(
'default/user/info.html.twig',
@ -260,6 +295,18 @@ class UserController extends AbstractController
)
],
'events' => $activityService->getEventsTree(),
'activities' => $activityService->findLastActivitiesByUserId(
$userTarget->getId(),
$user->getEvents(),
$this->getParameter('app.pagination'),
($page - 1) * $this->getParameter('app.pagination')
),
'pagination' =>
[
'page' => $page,
'pages' => ceil($total / $this->getParameter('app.pagination')),
'total' => $total
]
]
);
}

View file

@ -20,4 +20,65 @@ class ActivityRepository extends ServiceEntityRepository
{
parent::__construct($registry, Activity::class);
}
public function findActivitiesTotal(
array $whitelist
): int
{
return $this->createQueryBuilder('a')
->select('count(a.id)')
->where('a.event IN (:event)')
->setParameter(':event', $whitelist)
->getQuery()
->getSingleScalarResult()
;
}
public function findActivitiesTotalByUserId(
int $userId,
array $whitelist
): int
{
return $this->createQueryBuilder('a')
->select('count(a.id)')
->where('a.userId = :userId')
->andWhere('a.event IN (:event)')
->setParameter(':userId', $userId)
->setParameter(':event', $whitelist)
->getQuery()
->getSingleScalarResult()
;
}
public function findActivitiesTotalByTorrentId(
int $torrentId,
array $whitelist
): int
{
return $this->createQueryBuilder('a')
->select('count(a.id)')
->where('a.torrentId = :torrentId')
->andWhere('a.event IN (:event)')
->setParameter(':torrentId', $torrentId)
->setParameter(':event', $whitelist)
->getQuery()
->getSingleScalarResult()
;
}
public function findActivitiesTotalByArticleId(
int $articleId,
array $whitelist
): int
{
return $this->createQueryBuilder('a')
->select('count(a.id)')
->where('a.articleId = :articleId')
->andWhere('a.event IN (:event)')
->setParameter(':articleId', $articleId)
->setParameter(':event', $whitelist)
->getQuery()
->getSingleScalarResult()
;
}
}

View file

@ -361,7 +361,9 @@ class ActivityService
}
public function findLastActivities(
array $whitelist
array $whitelist,
int $limit = 10,
int $offset = 0
): array
{
return $this->entityManagerInterface
@ -372,13 +374,17 @@ class ActivityService
],
[
'id' => 'DESC'
]
],
$limit,
$offset
);
}
public function findLastActivitiesByUserId(
int $userId,
array $whitelist
array $whitelist,
int $limit = 10,
int $offset = 0
): array
{
return $this->entityManagerInterface
@ -386,11 +392,105 @@ class ActivityService
->findBy(
[
'userId' => $userId,
'event' => $whitelist
'event' => $whitelist,
],
[
'id' => 'DESC'
]
],
$limit,
$offset
);
}
public function findLastActivitiesByTorrentId(
int $torrentId,
array $whitelist,
int $limit = 10,
int $offset = 0
): array
{
return $this->entityManagerInterface
->getRepository(Activity::class)
->findBy(
[
'torrentId' => $torrentId,
'event' => $whitelist,
],
[
'id' => 'DESC'
],
$limit,
$offset
);
}
public function findLastActivitiesByArticleId(
int $articleId,
array $whitelist,
int $limit = 10,
int $offset = 0
): array
{
return $this->entityManagerInterface
->getRepository(Activity::class)
->findBy(
[
'articleId' => $articleId,
'event' => $whitelist,
],
[
'id' => 'DESC'
],
$limit,
$offset
);
}
public function findActivitiesTotal(
array $whitelist
): int
{
return $this->entityManagerInterface
->getRepository(Activity::class)
->findActivitiesTotal($whitelist);
}
public function findActivitiesTotalByUserId(
int $userId,
array $whitelist
): int
{
return $this->entityManagerInterface
->getRepository(Activity::class)
->findActivitiesTotalByUserId(
$userId,
$whitelist
);
}
public function findActivitiesTotalByTorrentId(
int $torrentId,
array $whitelist
): int
{
return $this->entityManagerInterface
->getRepository(Activity::class)
->findActivitiesTotalByTorrentId(
$torrentId,
$whitelist
);
}
public function findActivitiesTotalByArticleId(
int $articleId,
array $whitelist
): int
{
return $this->entityManagerInterface
->getRepository(Activity::class)
->findActivitiesTotalByArticleId(
$articleId,
$whitelist
);
}