Added support for ordering in shortcode:list command

This commit is contained in:
Alejandro Celaya 2016-10-22 23:10:30 +02:00
parent 85146e5676
commit 9ac48bfbc5
4 changed files with 36 additions and 12 deletions

View file

@ -70,6 +70,14 @@ class ListShortcodesCommand extends Command
InputOption::VALUE_OPTIONAL, InputOption::VALUE_OPTIONAL,
$this->translator->translate('A comma-separated list of tags to filter results') $this->translator->translate('A comma-separated list of tags to filter results')
) )
->addOption(
'orderBy',
'o',
InputOption::VALUE_OPTIONAL,
$this->translator->translate(
'The field from which we want to order by. Pass ASC or DESC separated by a comma'
)
)
->addOption( ->addOption(
'showTags', 'showTags',
null, null,
@ -85,12 +93,13 @@ class ListShortcodesCommand extends Command
$tags = $input->getOption('tags'); $tags = $input->getOption('tags');
$tags = ! empty($tags) ? explode(',', $tags) : []; $tags = ! empty($tags) ? explode(',', $tags) : [];
$showTags = $input->getOption('showTags'); $showTags = $input->getOption('showTags');
$orderBy = $input->getOption('orderBy');
/** @var QuestionHelper $helper */ /** @var QuestionHelper $helper */
$helper = $this->getHelper('question'); $helper = $this->getHelper('question');
do { do {
$result = $this->shortUrlService->listShortUrls($page, $searchTerm, $tags); $result = $this->shortUrlService->listShortUrls($page, $searchTerm, $tags, $this->processOrderBy($input));
$page++; $page++;
$table = new Table($output); $table = new Table($output);
@ -136,4 +145,15 @@ class ListShortcodesCommand extends Command
} }
} while ($continue); } while ($continue);
} }
protected function processOrderBy(InputInterface $input)
{
$orderBy = $input->getOption('orderBy');
if (empty($orderBy)) {
return null;
}
$orderBy = explode(',', $orderBy);
return count($orderBy) === 1 ? $orderBy[0] : [$orderBy[0] => $orderBy[1]];
}
} }

View file

@ -46,8 +46,8 @@ class ListShortcodesCommandTest extends TestCase
public function noInputCallsListJustOnce() public function noInputCallsListJustOnce()
{ {
$this->questionHelper->setInputStream($this->getInputStream('\n')); $this->questionHelper->setInputStream($this->getInputStream('\n'));
$this->shortUrlService->listShortUrls(1, null, [])->willReturn(new Paginator(new ArrayAdapter())) $this->shortUrlService->listShortUrls(1, null, [], null)->willReturn(new Paginator(new ArrayAdapter()))
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$this->commandTester->execute(['command' => 'shortcode:list']); $this->commandTester->execute(['command' => 'shortcode:list']);
} }
@ -103,8 +103,8 @@ class ListShortcodesCommandTest extends TestCase
{ {
$page = 5; $page = 5;
$this->questionHelper->setInputStream($this->getInputStream('\n')); $this->questionHelper->setInputStream($this->getInputStream('\n'));
$this->shortUrlService->listShortUrls($page, null, [])->willReturn(new Paginator(new ArrayAdapter())) $this->shortUrlService->listShortUrls($page, null, [], null)->willReturn(new Paginator(new ArrayAdapter()))
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$this->commandTester->execute([ $this->commandTester->execute([
'command' => 'shortcode:list', 'command' => 'shortcode:list',
@ -118,8 +118,8 @@ class ListShortcodesCommandTest extends TestCase
public function ifTagsFlagIsProvidedTagsColumnIsIncluded() public function ifTagsFlagIsProvidedTagsColumnIsIncluded()
{ {
$this->questionHelper->setInputStream($this->getInputStream('\n')); $this->questionHelper->setInputStream($this->getInputStream('\n'));
$this->shortUrlService->listShortUrls(1, null, [])->willReturn(new Paginator(new ArrayAdapter())) $this->shortUrlService->listShortUrls(1, null, [], null)->willReturn(new Paginator(new ArrayAdapter()))
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$this->commandTester->execute([ $this->commandTester->execute([
'command' => 'shortcode:list', 'command' => 'shortcode:list',

View file

@ -43,7 +43,11 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
$fieldName = is_array($orderBy) ? key($orderBy) : $orderBy; $fieldName = is_array($orderBy) ? key($orderBy) : $orderBy;
$order = is_array($orderBy) ? $orderBy[$fieldName] : 'ASC'; $order = is_array($orderBy) ? $orderBy[$fieldName] : 'ASC';
if ($fieldName === 'visits') { if (in_array($fieldName, [
'visits',
'visitsCount',
'visitCount',
])) {
$qb->addSelect('COUNT(v) AS totalVisits') $qb->addSelect('COUNT(v) AS totalVisits')
->leftJoin('s.visits', 'v') ->leftJoin('s.visits', 'v')
->groupBy('s') ->groupBy('s')

View file

@ -34,8 +34,8 @@ class ListShortcodesActionTest extends TestCase
public function properListReturnsSuccessResponse() public function properListReturnsSuccessResponse()
{ {
$page = 3; $page = 3;
$this->service->listShortUrls($page, null, [])->willReturn(new Paginator(new ArrayAdapter())) $this->service->listShortUrls($page, null, [], null)->willReturn(new Paginator(new ArrayAdapter()))
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$response = $this->action->__invoke( $response = $this->action->__invoke(
ServerRequestFactory::fromGlobals()->withQueryParams([ ServerRequestFactory::fromGlobals()->withQueryParams([
@ -52,8 +52,8 @@ class ListShortcodesActionTest extends TestCase
public function anExceptionsReturnsErrorResponse() public function anExceptionsReturnsErrorResponse()
{ {
$page = 3; $page = 3;
$this->service->listShortUrls($page, null, [])->willThrow(\Exception::class) $this->service->listShortUrls($page, null, [], null)->willThrow(\Exception::class)
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$response = $this->action->__invoke( $response = $this->action->__invoke(
ServerRequestFactory::fromGlobals()->withQueryParams([ ServerRequestFactory::fromGlobals()->withQueryParams([