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
foreach ($topUsers as $user) {
$chartData['titles'][] = $user->login;
$chartData['subscribers'][] = $user->subscribersCount;
$chartData['titles'][] = $user->getLogin();
$chartData['subscribers'][] = $user->getSubscribersCount();
}
// Chart

View file

@ -10,16 +10,32 @@ class TopUserDTO
/**
* @var string
*/
public $login;
private $login;
/**
* @var int
*/
public $subscribersCount;
private $subscribersCount;
public function __construct($login, $subscribersCount)
{
$this->login = $login;
$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[]
*/
public function getTopUsers($limit = 30)
@ -95,16 +99,29 @@ class UserRepository extends EntityRepository
throw new \InvalidArgumentException('$limit must be an integer');
}
// TODO: refactor query
$qb = $this->getEntityManager()->getRepository('SkobkinPointToolsBundle:Subscription')->createQueryBuilder('s');
return $qb
->select(['COUNT(s.subscriber) as cnt', 'NEW Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO(a.login, COUNT(s.subscriber))'])
$rows = $qb
->select([
'NEW Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO(a.login, COUNT(s.subscriber))',
'COUNT(s.subscriber) as subscribers_count'
])
->innerJoin('s.author', 'a')
->orderBy('cnt', 'desc')
->orderBy('subscribers_count', 'desc')
->groupBy('a.id')
->setMaxResults($limit)
->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;
}
}