More refactoring.
This commit is contained in:
parent
da7b8894f5
commit
c87192a1f2
|
@ -2,9 +2,7 @@
|
||||||
|
|
||||||
namespace Skobkin\Bundle\PointToolsBundle\Controller;
|
namespace Skobkin\Bundle\PointToolsBundle\Controller;
|
||||||
|
|
||||||
use Doctrine\ORM\QueryBuilder;
|
|
||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager;
|
||||||
use Skobkin\Bundle\PointToolsBundle\Entity\TopUserDTO;
|
|
||||||
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
||||||
use Skobkin\Bundle\PointToolsBundle\Service\UserApi;
|
use Skobkin\Bundle\PointToolsBundle\Service\UserApi;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
|
@ -17,16 +15,11 @@ class UserController extends Controller
|
||||||
*/
|
*/
|
||||||
public function showAction($login)
|
public function showAction($login)
|
||||||
{
|
{
|
||||||
/** @var QueryBuilder $qb */
|
/** @var EntityManager $em */
|
||||||
$qb = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:User')->createQueryBuilder('u');
|
$em = $this->getDoctrine()->getManager();
|
||||||
|
|
||||||
$user = $qb
|
/** @var User $user */
|
||||||
->select('u')
|
$user = $em->getRepository('SkobkinPointToolsBundle:User')->findUserByLogin($login);
|
||||||
->where('LOWER(u.login) = LOWER(:login)')
|
|
||||||
->setMaxResults(1)
|
|
||||||
->setParameter('login', $login)
|
|
||||||
->getQuery()->getOneOrNullResult()
|
|
||||||
;
|
|
||||||
|
|
||||||
if (!$user) {
|
if (!$user) {
|
||||||
throw $this->createNotFoundException('User ' . $login . ' not found.');
|
throw $this->createNotFoundException('User ' . $login . ' not found.');
|
||||||
|
@ -34,56 +27,18 @@ class UserController extends Controller
|
||||||
|
|
||||||
$userApi = $this->container->get('skobkin_point_tools.api_user');
|
$userApi = $this->container->get('skobkin_point_tools.api_user');
|
||||||
|
|
||||||
$qb = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:User')->createQueryBuilder('u');
|
|
||||||
|
|
||||||
$subscribers = $qb
|
|
||||||
->select('u')
|
|
||||||
->innerJoin('u.subscriptions', 's')
|
|
||||||
->where('s.author = :author')
|
|
||||||
->orderBy('u.login', 'asc')
|
|
||||||
->setParameter('author', $user->getId())
|
|
||||||
->getQuery()->getResult()
|
|
||||||
;
|
|
||||||
|
|
||||||
$qb = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->createQueryBuilder('se');
|
|
||||||
|
|
||||||
$subscriptionsEvents = $qb
|
|
||||||
->select()
|
|
||||||
->where('se.author = :author')
|
|
||||||
->orderBy('se.date', 'desc')
|
|
||||||
->setMaxResults(10)
|
|
||||||
->setParameter('author', $user)
|
|
||||||
->getQuery()->getResult()
|
|
||||||
;
|
|
||||||
|
|
||||||
return $this->render('SkobkinPointToolsBundle:User:show.html.twig', [
|
return $this->render('SkobkinPointToolsBundle:User:show.html.twig', [
|
||||||
'user' => $user,
|
'user' => $user,
|
||||||
'subscribers' => $subscribers,
|
'subscribers' => $em->getRepository('SkobkinPointToolsBundle:User')->findUserSubscribersById($user->getId()),
|
||||||
'log' => $subscriptionsEvents,
|
'log' => $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getUserLastSubscriptionEventsById($user, 10),
|
||||||
'avatar_url' => $userApi->getAvatarUrl($user, UserApi::AVATAR_SIZE_LARGE),
|
'avatar_url' => $userApi->getAvatarUrl($user, UserApi::AVATAR_SIZE_LARGE),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function topAction()
|
public function topAction()
|
||||||
{
|
{
|
||||||
/** @var EntityManager $em */
|
|
||||||
$em = $this->getDoctrine()->getManager();
|
|
||||||
|
|
||||||
/** @var QueryBuilder $qb */
|
|
||||||
$qb = $em->getRepository('SkobkinPointToolsBundle:Subscription')->createQueryBuilder('s');
|
|
||||||
|
|
||||||
/** @var TopUserDTO[] $topUsers */
|
|
||||||
$topUsers = $qb
|
|
||||||
->select(['COUNT(s.subscriber) as cnt', 'NEW SkobkinPointToolsBundle:TopUserDTO(a.login, COUNT(s.subscriber))'])
|
|
||||||
->innerJoin('s.author', 'a')
|
|
||||||
->orderBy('cnt', 'desc')
|
|
||||||
->groupBy('a.id')
|
|
||||||
->setMaxResults(30)
|
|
||||||
->getQuery()->getResult()
|
|
||||||
;
|
|
||||||
|
|
||||||
return $this->render('@SkobkinPointTools/User/top.html.twig', [
|
return $this->render('@SkobkinPointTools/User/top.html.twig', [
|
||||||
'top_users' => $topUsers
|
'top_users' => $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:Subscription')->getTopUsers(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,4 +22,27 @@ class SubscriptionEventRepository extends EntityRepository
|
||||||
->getQuery()->getSingleScalarResult()
|
->getQuery()->getSingleScalarResult()
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
* @param integer $limit
|
||||||
|
* @return SubscriptionEvent[]
|
||||||
|
*/
|
||||||
|
public function getUserLastSubscriptionEventsById(User $user, $limit)
|
||||||
|
{
|
||||||
|
if (!is_int($limit)) {
|
||||||
|
throw new \InvalidArgumentException('$limit must be an integer');
|
||||||
|
}
|
||||||
|
|
||||||
|
$qb = $this->createQueryBuilder('se');
|
||||||
|
|
||||||
|
return $qb
|
||||||
|
->select()
|
||||||
|
->where('se.author = :author')
|
||||||
|
->orderBy('se.date', 'desc')
|
||||||
|
->setMaxResults($limit)
|
||||||
|
->setParameter('author', $user)
|
||||||
|
->getQuery()->getResult()
|
||||||
|
;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -12,6 +12,10 @@ class SubscriptionRepository extends EntityRepository
|
||||||
*/
|
*/
|
||||||
public function getUserSubscribersCountById($id)
|
public function getUserSubscribersCountById($id)
|
||||||
{
|
{
|
||||||
|
if (!is_int($id)) {
|
||||||
|
throw new \InvalidArgumentException('$id must be an integer');
|
||||||
|
}
|
||||||
|
|
||||||
$qb = $this->createQueryBuilder('s');
|
$qb = $this->createQueryBuilder('s');
|
||||||
return $qb
|
return $qb
|
||||||
->select('COUNT(s)')
|
->select('COUNT(s)')
|
||||||
|
@ -21,4 +25,21 @@ class SubscriptionRepository extends EntityRepository
|
||||||
->getQuery()->getSingleScalarResult()
|
->getQuery()->getSingleScalarResult()
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return TopUserDTO[]
|
||||||
|
*/
|
||||||
|
public function getTopUsers()
|
||||||
|
{
|
||||||
|
$qb = $this->createQueryBuilder('s');
|
||||||
|
|
||||||
|
return $qb
|
||||||
|
->select(['COUNT(s.subscriber) as cnt', 'NEW SkobkinPointToolsBundle:TopUserDTO(a.login, COUNT(s.subscriber))'])
|
||||||
|
->innerJoin('s.author', 'a')
|
||||||
|
->orderBy('cnt', 'desc')
|
||||||
|
->groupBy('a.id')
|
||||||
|
->setMaxResults(30)
|
||||||
|
->getQuery()->getResult()
|
||||||
|
;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -6,6 +6,24 @@ use Doctrine\ORM\EntityRepository;
|
||||||
|
|
||||||
class UserRepository extends EntityRepository
|
class UserRepository extends EntityRepository
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param string $login
|
||||||
|
* @return User[]
|
||||||
|
* @throws \Doctrine\ORM\NonUniqueResultException
|
||||||
|
*/
|
||||||
|
public function findUserByLogin($login)
|
||||||
|
{
|
||||||
|
$qb = $this->createQueryBuilder('u');
|
||||||
|
|
||||||
|
return $qb
|
||||||
|
->select('u')
|
||||||
|
->where('LOWER(u.login) = LOWER(:login)')
|
||||||
|
->setMaxResults(1)
|
||||||
|
->setParameter('login', $login)
|
||||||
|
->getQuery()->getOneOrNullResult()
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return integer
|
* @return integer
|
||||||
*/
|
*/
|
||||||
|
@ -15,4 +33,26 @@ class UserRepository extends EntityRepository
|
||||||
|
|
||||||
return $qb->select('COUNT(u)')->getQuery()->getSingleScalarResult();
|
return $qb->select('COUNT(u)')->getQuery()->getSingleScalarResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param integer $id
|
||||||
|
* @return User[]
|
||||||
|
*/
|
||||||
|
public function findUserSubscribersById($id)
|
||||||
|
{
|
||||||
|
if (!is_int($id)) {
|
||||||
|
throw new \InvalidArgumentException('$id must be an integer');
|
||||||
|
}
|
||||||
|
|
||||||
|
$qb = $this->createQueryBuilder('u');
|
||||||
|
|
||||||
|
return $qb
|
||||||
|
->select('u')
|
||||||
|
->innerJoin('u.subscriptions', 's')
|
||||||
|
->where('s.author = :author')
|
||||||
|
->orderBy('u.login', 'asc')
|
||||||
|
->setParameter('author', $id)
|
||||||
|
->getQuery()->getResult()
|
||||||
|
;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue