1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-10-20 19:52:09 +00:00

Added random feature

This commit is contained in:
Nicolas Lœuillet 2017-12-22 15:44:00 +01:00 committed by Jeremy Benoist
parent c73025ad8b
commit 09ef25c3c3
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
18 changed files with 169 additions and 10 deletions

View file

@ -110,8 +110,7 @@ class EntryRepository extends EntityRepository
*/
public function getBuilderForUntaggedByUser($userId)
{
return $this
->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId));
return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId));
}
/**
@ -432,6 +431,46 @@ class EntryRepository extends EntityRepository
->getResult();
}
/**
* Returns a random entry, filtering by status.
*
* @param $userId
* @param string $status can be unread, archive or starred
*
* @throws \Doctrine\ORM\NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*
* @return Entry
*/
public function getRandomEntry($userId, $status = '')
{
$max = $this->getEntityManager()
->createQuery('SELECT MAX(e.id) FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
->setParameter('userId', $userId)
->getSingleScalarResult();
$qb = $this->createQueryBuilder('e')
->where('e.user = :user_id')->setParameter('user_id', $userId);
if ('unread' === $status) {
$qb->andWhere('e.isArchived = false');
}
if ('archive' === $status) {
$qb->andWhere('e.isArchived = true');
}
if ('starred' === $status) {
$qb->andWhere('e.isStarred = true');
}
return $qb->andWhere('e.id >= :rand')
->setParameter('rand', rand(0, $max))
->setMaxResults(1)
->getQuery()
->getSingleResult();
}
/**
* Return a query builder to be used by other getBuilderFor* method.
*
@ -470,7 +509,6 @@ class EntryRepository extends EntityRepository
*/
private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc')
{
return $qb
->orderBy(sprintf('e.%s', $sortBy), $direction);
return $qb->orderBy(sprintf('e.%s', $sortBy), $direction);
}
}