From 48bbd3772f5ef04f49ace4c13d9d096953b86a74 Mon Sep 17 00:00:00 2001 From: Ruben Date: Fri, 31 Mar 2017 12:43:11 +0200 Subject: [PATCH] User stats --- src/Api/StatsControllerProvider.php | 2 ++ src/Models/UserRepository.php | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Api/StatsControllerProvider.php b/src/Api/StatsControllerProvider.php index a44cf2c..ff96889 100644 --- a/src/Api/StatsControllerProvider.php +++ b/src/Api/StatsControllerProvider.php @@ -27,6 +27,7 @@ class StatsControllerProvider implements ControllerProviderInterface $controllers->get('/', function (Application $app) { $gameRepo = $this->_eh->getEm()->getRepository(Models\Game::class); + $userRepo = $this->_eh->getEm()->getRepository(Models\User::class); $stats = [ 'games' => [ @@ -41,6 +42,7 @@ class StatsControllerProvider implements ControllerProviderInterface for ($i=0; $i < 61; $i++) { $day = date('Y-m-d',strtotime("-$i days")); $stats['games']['new'][$day] = $gameRepo->getCreatedCountOnDate($day); + $stats['users']['new'][$day] = $userRepo->getCreatedCountOnDate($day); } $stats['games']['total'] = $gameRepo->getCount(); diff --git a/src/Models/UserRepository.php b/src/Models/UserRepository.php index b834658..04167dc 100644 --- a/src/Models/UserRepository.php +++ b/src/Models/UserRepository.php @@ -91,4 +91,28 @@ class UserRepository extends EntityRepository implements UserProviderInterface ]); return (int) $query->getSingleScalarResult(); } + + + /** + * @return int + */ + public function getCreatedCountOnDate($date) { + $query = $this->_em->createQuery( + "SELECT COUNT(u.id) FROM ".User::class." u WHERE DATE(u.createdAt) = :date" + ) + ->setParameters([ + 'date'=> $date, + ]); + return (int) $query->getSingleScalarResult(); + } + + /** + * @return int + */ + public function getCount() { + $query = $this->_em->createQuery( + "SELECT COUNT(u.id) FROM ".User::class." u" + ); + return (int) $query->getSingleScalarResult(); + } } \ No newline at end of file