Fix #26. Top users page fixed.

This commit is contained in:
Alexey Skobkin 2016-12-12 01:26:27 +03:00
parent 8ed6d1dd6c
commit 9264170d0b
3 changed files with 41 additions and 8 deletions

View file

@ -73,8 +73,8 @@ class UserController extends Controller
// Preparing chart data // Preparing chart data
foreach ($topUsers as $user) { foreach ($topUsers as $user) {
$chartData['titles'][] = $user->login; $chartData['titles'][] = $user->getLogin();
$chartData['subscribers'][] = $user->subscribersCount; $chartData['subscribers'][] = $user->getSubscribersCount();
} }
// Chart // Chart

View file

@ -10,16 +10,32 @@ class TopUserDTO
/** /**
* @var string * @var string
*/ */
public $login; private $login;
/** /**
* @var int * @var int
*/ */
public $subscribersCount; private $subscribersCount;
public function __construct($login, $subscribersCount) public function __construct($login, $subscribersCount)
{ {
$this->login = $login; $this->login = $login;
$this->subscribersCount = $subscribersCount; $this->subscribersCount = $subscribersCount;
} }
/**
* @return string
*/
public function getLogin()
{
return $this->login;
}
/**
* @return int
*/
public function getSubscribersCount()
{
return $this->subscribersCount;
}
} }

View file

@ -87,6 +87,10 @@ class UserRepository extends EntityRepository
} }
/** /**
* Returns top users by subscribers count
*
* @param int $limit
*
* @return TopUserDTO[] * @return TopUserDTO[]
*/ */
public function getTopUsers($limit = 30) public function getTopUsers($limit = 30)
@ -95,16 +99,29 @@ class UserRepository extends EntityRepository
throw new \InvalidArgumentException('$limit must be an integer'); throw new \InvalidArgumentException('$limit must be an integer');
} }
// TODO: refactor query
$qb = $this->getEntityManager()->getRepository('SkobkinPointToolsBundle:Subscription')->createQueryBuilder('s'); $qb = $this->getEntityManager()->getRepository('SkobkinPointToolsBundle:Subscription')->createQueryBuilder('s');
return $qb $rows = $qb
->select(['COUNT(s.subscriber) as cnt', 'NEW Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO(a.login, COUNT(s.subscriber))']) ->select([
'NEW Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO(a.login, COUNT(s.subscriber))',
'COUNT(s.subscriber) as subscribers_count'
])
->innerJoin('s.author', 'a') ->innerJoin('s.author', 'a')
->orderBy('cnt', 'desc') ->orderBy('subscribers_count', 'desc')
->groupBy('a.id') ->groupBy('a.id')
->setMaxResults($limit) ->setMaxResults($limit)
->getQuery()->getResult() ->getQuery()->getResult()
; ;
$result = [];
// Removing subscribers_count element, saving TopUserDTO
// @todo remove crutches, refactor query
foreach ($rows as $row) {
unset($row['subscribers_count']);
$result[] = reset($row);
}
return $result;
} }
} }