mirror of
https://github.com/wallabag/wallabag.git
synced 2024-12-28 10:30:28 +00:00
Refactorize the way to retrieve entries
One place to retrieve entries in Entry & Rss controller. More simple and easy to maintain.
This commit is contained in:
parent
4fcb7eaf13
commit
0ab7404f93
3 changed files with 124 additions and 115 deletions
|
@ -113,34 +113,7 @@ class EntryController extends Controller
|
|||
*/
|
||||
public function showUnreadAction(Request $request, $page)
|
||||
{
|
||||
$form = $this->get('form.factory')->create(new EntryFilterType());
|
||||
|
||||
$filterBuilder = $this->getDoctrine()
|
||||
->getRepository('WallabagCoreBundle:Entry')
|
||||
->findUnreadByUser($this->getUser()->getId());
|
||||
|
||||
if ($request->query->has($form->getName())) {
|
||||
// manually bind values from the request
|
||||
$form->submit($request->query->get($form->getName()));
|
||||
|
||||
// build the query from the given form object
|
||||
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder);
|
||||
}
|
||||
|
||||
$pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery());
|
||||
$entries = new Pagerfanta($pagerAdapter);
|
||||
|
||||
$entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
|
||||
$entries->setCurrentPage($page);
|
||||
|
||||
return $this->render(
|
||||
'WallabagCoreBundle:Entry:entries.html.twig',
|
||||
array(
|
||||
'form' => $form->createView(),
|
||||
'entries' => $entries,
|
||||
'currentPage' => $page,
|
||||
)
|
||||
);
|
||||
return $this->showEntries('unread', $request, $page);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -155,34 +128,7 @@ class EntryController extends Controller
|
|||
*/
|
||||
public function showArchiveAction(Request $request, $page)
|
||||
{
|
||||
$form = $this->get('form.factory')->create(new EntryFilterType());
|
||||
|
||||
$filterBuilder = $this->getDoctrine()
|
||||
->getRepository('WallabagCoreBundle:Entry')
|
||||
->findArchiveByUser($this->getUser()->getId());
|
||||
|
||||
if ($request->query->has($form->getName())) {
|
||||
// manually bind values from the request
|
||||
$form->submit($request->query->get($form->getName()));
|
||||
|
||||
// build the query from the given form object
|
||||
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder);
|
||||
}
|
||||
|
||||
$pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery());
|
||||
$entries = new Pagerfanta($pagerAdapter);
|
||||
|
||||
$entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
|
||||
$entries->setCurrentPage($page);
|
||||
|
||||
return $this->render(
|
||||
'WallabagCoreBundle:Entry:entries.html.twig',
|
||||
array(
|
||||
'form' => $form->createView(),
|
||||
'entries' => $entries,
|
||||
'currentPage' => $page,
|
||||
)
|
||||
);
|
||||
return $this->showEntries('archive', $request, $page);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -197,11 +143,64 @@ class EntryController extends Controller
|
|||
*/
|
||||
public function showStarredAction(Request $request, $page)
|
||||
{
|
||||
return $this->showEntries('starred', $request, $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Global method to retrieve entries depending on the given type
|
||||
* It returns the response to be send.
|
||||
*
|
||||
* @param string $type Entries type: unread, starred or archive
|
||||
* @param Request $request
|
||||
* @param int $page
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
private function showEntries($type, Request $request, $page)
|
||||
{
|
||||
$repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
|
||||
|
||||
switch ($type) {
|
||||
case 'starred':
|
||||
$qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
|
||||
break;
|
||||
|
||||
case 'archive':
|
||||
$qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
|
||||
break;
|
||||
|
||||
case 'unread':
|
||||
$qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
|
||||
}
|
||||
|
||||
$form = $this->get('form.factory')->create(new EntryFilterType());
|
||||
|
||||
$filterBuilder = $this->getDoctrine()
|
||||
->getRepository('WallabagCoreBundle:Entry')
|
||||
->findStarredByUser($this->getUser()->getId());
|
||||
if ($request->query->has($form->getName())) {
|
||||
// manually bind values from the request
|
||||
$form->submit($request->query->get($form->getName()));
|
||||
|
||||
// build the query from the given form object
|
||||
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
|
||||
}
|
||||
|
||||
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
|
||||
$entries = new Pagerfanta($pagerAdapter);
|
||||
|
||||
$entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
|
||||
$entries->setCurrentPage($page);
|
||||
|
||||
return $this->render(
|
||||
'WallabagCoreBundle:Entry:entries.html.twig',
|
||||
array(
|
||||
'form' => $form->createView(),
|
||||
'entries' => $entries,
|
||||
'currentPage' => $page,
|
||||
)
|
||||
);
|
||||
|
||||
if ($request->query->has($form->getName())) {
|
||||
// manually bind values from the request
|
||||
|
|
|
@ -22,22 +22,7 @@ class RssController extends Controller
|
|||
*/
|
||||
public function showUnreadAction(User $user)
|
||||
{
|
||||
$qb = $this->getDoctrine()
|
||||
->getRepository('WallabagCoreBundle:Entry')
|
||||
->findUnreadByUser(
|
||||
$user->getId()
|
||||
);
|
||||
|
||||
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
|
||||
$entries = new Pagerfanta($pagerAdapter);
|
||||
|
||||
$perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit');
|
||||
$entries->setMaxPerPage($perPage);
|
||||
|
||||
return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
|
||||
'type' => 'unread',
|
||||
'entries' => $entries,
|
||||
));
|
||||
return $this->showEntries('unread', $user);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -50,22 +35,7 @@ class RssController extends Controller
|
|||
*/
|
||||
public function showArchiveAction(User $user)
|
||||
{
|
||||
$qb = $this->getDoctrine()
|
||||
->getRepository('WallabagCoreBundle:Entry')
|
||||
->findArchiveByUser(
|
||||
$user->getId()
|
||||
);
|
||||
|
||||
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
|
||||
$entries = new Pagerfanta($pagerAdapter);
|
||||
|
||||
$perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit');
|
||||
$entries->setMaxPerPage($perPage);
|
||||
|
||||
return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
|
||||
'type' => 'archive',
|
||||
'entries' => $entries,
|
||||
));
|
||||
return $this->showEntries('archive', $user);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,11 +48,38 @@ class RssController extends Controller
|
|||
*/
|
||||
public function showStarredAction(User $user)
|
||||
{
|
||||
$qb = $this->getDoctrine()
|
||||
->getRepository('WallabagCoreBundle:Entry')
|
||||
->findStarredByUser(
|
||||
$user->getId()
|
||||
);
|
||||
return $this->showEntries('starred', $user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Global method to retrieve entries depending on the given type
|
||||
* It returns the response to be send.
|
||||
*
|
||||
* @param string $type Entries type: unread, starred or archive
|
||||
* @param User $user
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
private function showEntries($type, User $user)
|
||||
{
|
||||
$repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
|
||||
|
||||
switch ($type) {
|
||||
case 'starred':
|
||||
$qb = $repository->getBuilderForStarredByUser($user->getId());
|
||||
break;
|
||||
|
||||
case 'archive':
|
||||
$qb = $repository->getBuilderForArchiveByUser($user->getId());
|
||||
break;
|
||||
|
||||
case 'unread':
|
||||
$qb = $repository->getBuilderForUnreadByUser($user->getId());
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
|
||||
}
|
||||
|
||||
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
|
||||
$entries = new Pagerfanta($pagerAdapter);
|
||||
|
@ -91,7 +88,7 @@ class RssController extends Controller
|
|||
$entries->setMaxPerPage($perPage);
|
||||
|
||||
return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
|
||||
'type' => 'starred',
|
||||
'type' => $type,
|
||||
'entries' => $entries,
|
||||
));
|
||||
}
|
||||
|
|
|
@ -8,6 +8,22 @@ use Pagerfanta\Pagerfanta;
|
|||
|
||||
class EntryRepository extends EntityRepository
|
||||
{
|
||||
/**
|
||||
* Return a query builder to used by other getBuilderFor* method.
|
||||
*
|
||||
* @param int $userId
|
||||
*
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
private function getBuilderByUser($userId)
|
||||
{
|
||||
return $this->createQueryBuilder('e')
|
||||
->leftJoin('e.user', 'u')
|
||||
->andWhere('u.id = :userId')->setParameter('userId', $userId)
|
||||
->orderBy('e.id', 'desc')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves unread entries for a user.
|
||||
*
|
||||
|
@ -15,13 +31,12 @@ class EntryRepository extends EntityRepository
|
|||
*
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
public function findUnreadByUser($userId)
|
||||
public function getBuilderForUnreadByUser($userId)
|
||||
{
|
||||
return $this->createQueryBuilder('e')
|
||||
->leftJoin('e.user', 'u')
|
||||
->where('e.isArchived = false')
|
||||
->andWhere('u.id =:userId')->setParameter('userId', $userId)
|
||||
->orderBy('e.id', 'desc');
|
||||
return $this
|
||||
->getBuilderByUser($userId)
|
||||
->andWhere('e.isArchived = false')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -31,13 +46,12 @@ class EntryRepository extends EntityRepository
|
|||
*
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
public function findArchiveByUser($userId)
|
||||
public function getBuilderForArchiveByUser($userId)
|
||||
{
|
||||
return $this->createQueryBuilder('e')
|
||||
->leftJoin('e.user', 'u')
|
||||
->where('e.isArchived = true')
|
||||
->andWhere('u.id =:userId')->setParameter('userId', $userId)
|
||||
->orderBy('e.id', 'desc');
|
||||
return $this
|
||||
->getBuilderByUser($userId)
|
||||
->andWhere('e.isArchived = true')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -47,13 +61,12 @@ class EntryRepository extends EntityRepository
|
|||
*
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
public function findStarredByUser($userId)
|
||||
public function getBuilderForStarredByUser($userId)
|
||||
{
|
||||
return $this->createQueryBuilder('e')
|
||||
->leftJoin('e.user', 'u')
|
||||
->where('e.isStarred = true')
|
||||
->andWhere('u.id =:userId')->setParameter('userId', $userId)
|
||||
->orderBy('e.id', 'desc');
|
||||
return $this
|
||||
->getBuilderByUser($userId)
|
||||
->andWhere('e.isStarred = true')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue