aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Repository/EntryRepository.php
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas@loeuillet.org>2017-12-22 15:44:00 +0100
committerJeremy Benoist <jeremy.benoist@gmail.com>2018-10-11 11:50:36 +0200
commitee360905ce41c6f28c34419f18e50cd87ae387e5 (patch)
tree0142459d89cc2c8b8cc5a8980d67e1acf4226569 /src/Wallabag/CoreBundle/Repository/EntryRepository.php
parent43b6f3a8a8173c47475632ca2869f190b552b5d6 (diff)
downloadwallabag-ee360905ce41c6f28c34419f18e50cd87ae387e5.tar.gz
wallabag-ee360905ce41c6f28c34419f18e50cd87ae387e5.tar.zst
wallabag-ee360905ce41c6f28c34419f18e50cd87ae387e5.zip
Added random feature
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository/EntryRepository.php')
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php46
1 files changed, 42 insertions, 4 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index 93c630c0..7fe090be 100644
--- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
@@ -110,8 +110,7 @@ class EntryRepository extends EntityRepository
110 */ 110 */
111 public function getBuilderForUntaggedByUser($userId) 111 public function getBuilderForUntaggedByUser($userId)
112 { 112 {
113 return $this 113 return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId));
114 ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId));
115 } 114 }
116 115
117 /** 116 /**
@@ -429,6 +428,46 @@ class EntryRepository extends EntityRepository
429 } 428 }
430 429
431 /** 430 /**
431 * Returns a random entry, filtering by status.
432 *
433 * @param $userId
434 * @param string $status can be unread, archive or starred
435 *
436 * @throws \Doctrine\ORM\NoResultException
437 * @throws \Doctrine\ORM\NonUniqueResultException
438 *
439 * @return Entry
440 */
441 public function getRandomEntry($userId, $status = '')
442 {
443 $max = $this->getEntityManager()
444 ->createQuery('SELECT MAX(e.id) FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
445 ->setParameter('userId', $userId)
446 ->getSingleScalarResult();
447
448 $qb = $this->createQueryBuilder('e')
449 ->where('e.user = :user_id')->setParameter('user_id', $userId);
450
451 if ('unread' === $status) {
452 $qb->andWhere('e.isArchived = false');
453 }
454
455 if ('archive' === $status) {
456 $qb->andWhere('e.isArchived = true');
457 }
458
459 if ('starred' === $status) {
460 $qb->andWhere('e.isStarred = true');
461 }
462
463 return $qb->andWhere('e.id >= :rand')
464 ->setParameter('rand', rand(0, $max))
465 ->setMaxResults(1)
466 ->getQuery()
467 ->getSingleResult();
468 }
469
470 /**
432 * Return a query builder to be used by other getBuilderFor* method. 471 * Return a query builder to be used by other getBuilderFor* method.
433 * 472 *
434 * @param int $userId 473 * @param int $userId
@@ -466,7 +505,6 @@ class EntryRepository extends EntityRepository
466 */ 505 */
467 private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') 506 private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc')
468 { 507 {
469 return $qb 508 return $qb->orderBy(sprintf('e.%s', $sortBy), $direction);
470 ->orderBy(sprintf('e.%s', $sortBy), $direction);
471 } 509 }
472} 510}