From 27455af77583be84db42fd439a14c58bfac7a794 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Fri, 28 Nov 2014 16:00:22 +0800 Subject: [PATCH] Implement download entity and related stuff. Will build things like user quota on top of this. --- Controllers/DownloadTestController.php | 32 +++++++++ Controllers/PackTestController.php | 26 -------- DataAccess/DownloadRepository.php | 76 +++++++++++++++++++++ DataAccess/IDownloadRepository.php | 16 +++++ DataAccess/Queries/DownloadQueryConstraints.php | 38 +++++++++++ DataAccess/Queries/IDownloadQueryConstraints.php | 11 +++ DataAccess/Queries/QueryConstraints.php | 85 ------------------------ Domain/Entities/Download.php | 48 +++++++++++++ Domain/Entities/DownloadFactory.php | 32 +++++++++ Domain/Entities/IDownload.php | 12 ++++ Services/IUserSession.php | 1 + Services/UserSession.php | 4 ++ config/DI.php | 1 + config/DataMaps.php | 11 +++ config/Routes.php | 6 ++ public_html/index.php | 3 + 16 files changed, 291 insertions(+), 111 deletions(-) create mode 100644 Controllers/DownloadTestController.php delete mode 100644 Controllers/PackTestController.php create mode 100644 DataAccess/DownloadRepository.php create mode 100644 DataAccess/IDownloadRepository.php create mode 100644 DataAccess/Queries/DownloadQueryConstraints.php create mode 100644 DataAccess/Queries/IDownloadQueryConstraints.php delete mode 100644 DataAccess/Queries/QueryConstraints.php create mode 100644 Domain/Entities/Download.php create mode 100644 Domain/Entities/DownloadFactory.php create mode 100644 Domain/Entities/IDownload.php diff --git a/Controllers/DownloadTestController.php b/Controllers/DownloadTestController.php new file mode 100644 index 0000000..c12df99 --- /dev/null +++ b/Controllers/DownloadTestController.php @@ -0,0 +1,32 @@ +_downloadRepository = $repository; + } + + public function indexAction() { + $start = new DateTime('0:00 today'); + $end = new DateTime(); + + $constraints = new DownloadQueryConstraints(); + $constraints->inDateRange($start, $end); + $downloads = $this->_downloadRepository->findByUserId(4, $constraints); + + echo '
';
+        print_r($downloads);
+        echo '
'; + } +} diff --git a/Controllers/PackTestController.php b/Controllers/PackTestController.php deleted file mode 100644 index 060514a..0000000 --- a/Controllers/PackTestController.php +++ /dev/null @@ -1,26 +0,0 @@ -_packRepository = $repository; - } - - public function indexAction() { - $pack = $this->_packRepository->findById(10); - - echo '
';
-        print_r($pack);
-        echo '
'; - } -} diff --git a/DataAccess/DownloadRepository.php b/DataAccess/DownloadRepository.php new file mode 100644 index 0000000..544611e --- /dev/null +++ b/DataAccess/DownloadRepository.php @@ -0,0 +1,76 @@ +_dataMapper = $dataMapper; + $this->_queryBuilderFactory = $queryBuilderFactory; + } + + public function findById($id) { + $queryBuilder = $this->_queryBuilderFactory->createInstance(); + $queryBuilder->where('id', '=', $id); + + $result = $this->_dataMapper->map('Download', $queryBuilder); + return reset($result); + } + + public function findAll() + { + $queryBuilder = $this->_queryBuilderFactory->createInstance(); + $queryBuilder->where('id', '>', 0); + $result = $this->_dataMapper->map('Download', $queryBuilder); + return $result; + } + + public function findRange($id, $limit) + { + $queryBuilder = $this->_queryBuilderFactory->createInstance(); + $queryBuilder->where('id', '>=', $id)->limit($limit); + + return $this->_dataMapper->map('Download', $queryBuilder); + } + + public function save(IDownload $entity) { + return $this->_dataMapper->save($entity); + } + + public function findByUserId($id, IDownloadQueryConstraints $constraints = null) + { + $queryBuilder = $this->_queryBuilderFactory->createInstance(); + $queryBuilder->where('user_id', '=', $id); + + return $this->applyConstraintsAndReturn($constraints, $queryBuilder); + } + + public function findByFileId($id, IDownloadQueryConstraints $constraints = null) + { + $queryBuilder = $this->_queryBuilderFactory->createInstance(); + $queryBuilder->where('file_id', '=', $id); + + return $this->applyConstraintsAndReturn($constraints, $queryBuilder); + } + + private function applyConstraintsAndReturn(IDownloadQueryConstraints $constraints = NULL, IQueryBuilder $queryBuilder) + { + if($constraints) + { + $constraints->applyTo($queryBuilder); + } + + return $this->_dataMapper->map('Download', $queryBuilder); + } +} \ No newline at end of file diff --git a/DataAccess/IDownloadRepository.php b/DataAccess/IDownloadRepository.php new file mode 100644 index 0000000..e1052dd --- /dev/null +++ b/DataAccess/IDownloadRepository.php @@ -0,0 +1,16 @@ +_queryBuilder = $queryBuilder; + $this->applyDateRange(); + } + + public function inDateRange(DateTimeInterface $start, DateTimeInterface $end) + { + $this->_dateRangeStart = $start; + $this->_dateRangeEnd = $end; + return $this; + } + + private function applyDateRange() + { + if($this->_dateRangeStart && $this->_dateRangeEnd) { + $this->_queryBuilder->where('timestamp', '>=', $this->_dateRangeStart->getTimestamp()) + ->where('timestamp', '<=', $this->_dateRangeEnd->getTimestamp()); + } + + return $this; + } +} + \ No newline at end of file diff --git a/DataAccess/Queries/IDownloadQueryConstraints.php b/DataAccess/Queries/IDownloadQueryConstraints.php new file mode 100644 index 0000000..f51eef8 --- /dev/null +++ b/DataAccess/Queries/IDownloadQueryConstraints.php @@ -0,0 +1,11 @@ +_queryString = $queryString; - - $this->applyJoinClause() - ->applyWhereClauses() - ->applyLimitClause(); - - return $this->_queryString; - } - - public function where($columnName, $operator, $value) - { - $this->_whereClauses[$columnName] = array('operator' => $operator, 'value' => $value); - return $this; - } - - public function limit($start, $end = null) - { - if($end) - { - $this->_limitClause = sprintf(' LIMIT %u,%u', $start, $end); - return $this; - } - - $this->_limitClause = sprintf(' LIMIT %u', $start); - - return $this; - } - - public function join($type, $tableA, $columnA, $tableB, $columnB) - { - $this->_joinClause = sprintf(' %s JOIN %s ON %s.%s = %s.%s', $type, $tableB, $tableA, $columnA, $tableB, $columnB); - return $this; - } - - private function applyJoinClause() - { - $this->_queryString .= $this->_joinClause; - return $this; - } - - private function applyWhereClauses() - { - $this->_queryString .= ' WHERE '; - - foreach($this->_whereClauses as $columnName => $columnValue) - { - switch(gettype($columnValue['value'])) - { - case 'integer': - $this->_queryString .= sprintf("%s%s%u", $columnName, $columnValue['operator'], $columnValue['value']) . ' AND '; - break; - case 'string': - $this->_queryString .= sprintf("%s %s '%s'", $columnName, $columnValue['operator'], $columnValue['value']) . ' AND '; - break; - } - - } - - $this->_queryString = rtrim($this->_queryString, ' AND '); - return $this; - } - - private function applyLimitClause() - { - $this->_queryString .= $this->_limitClause; - return $this; - } - -} \ No newline at end of file diff --git a/Domain/Entities/Download.php b/Domain/Entities/Download.php new file mode 100644 index 0000000..d00d776 --- /dev/null +++ b/Domain/Entities/Download.php @@ -0,0 +1,48 @@ +_user = $user; + $this->_file = $file; + $this->_timestamp = $timestamp; + $this->_ip = $ip; + } + + public function getFile() + { + return $this->_file; + } + + public function getIp() + { + return $this->_ip; + } + + public function getTimestamp() + { + return $this->_timestamp; + } + + public function getUser() + { + return $this->_user; + } +} + diff --git a/Domain/Entities/DownloadFactory.php b/Domain/Entities/DownloadFactory.php new file mode 100644 index 0000000..3b54f41 --- /dev/null +++ b/Domain/Entities/DownloadFactory.php @@ -0,0 +1,32 @@ +_currentUser; } + public function getCurrentUserQuota() { + ; + } + private function findToken() { if($this->_request->isPost()) diff --git a/config/DI.php b/config/DI.php index 3d117d0..34e6be0 100644 --- a/config/DI.php +++ b/config/DI.php @@ -42,6 +42,7 @@ return [ //DA 'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'), 'DataAccess\StepMania\IPackRepository' => DI\object('DataAccess\StepMania\PackRepository'), + 'DataAccess\IDownloadRepository' => DI\object('DataAccess\DownloadRepository'), 'DataAccess\IUserRepository' => DI\object('DataAccess\UserRepository'), 'DataAccess\IFileRepository' => DI\object('DataAccess\FileRepository'), 'DataAccess\IDatabaseFactory' => DI\object('DataAccess\DatabaseFactory') diff --git a/config/DataMaps.php b/config/DataMaps.php index e9a4a7d..532f940 100644 --- a/config/DataMaps.php +++ b/config/DataMaps.php @@ -149,5 +149,16 @@ return [ 'uri' => DataAccess\Varchar('uri'), 'source' => DataAccess\Varchar('source') ] + ], + + 'Download' => [ + 'class' => 'Domain\Entities\Download', + 'table' => 'downloads', + 'maps' => [ + 'user' => DataAccess\Entity('User'), + 'file' => DataAccess\Entity('File'), + 'timestamp' => DataAccess\Int('timestamp'), + 'ip' => DataAccess\Varchar('ip') + ] ] ]; diff --git a/config/Routes.php b/config/Routes.php index 6c9f5e7..c3b28ca 100644 --- a/config/Routes.php +++ b/config/Routes.php @@ -7,6 +7,12 @@ return [ 'action' => 'list' ], + //TODO: test controller, delete later + '/downloadtest' => [ + 'methods' => ['GET'], + 'controller' => 'downloadTest' + ], + '/simfiles/upload' => [ 'methods' => ['POST'], 'controller' => 'Simfile', diff --git a/public_html/index.php b/public_html/index.php index 7b8f8dd..d4843d3 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -7,6 +7,9 @@ header("Access-Control-Allow-Origin: http://roll.divinelegy.meeples:8000"); require_once('../vendor/autoload.php'); +// Everything time related should be UTC+0 based +date_default_timezone_set('UTC'); + // Set up the DI container $containerBuilder = new DI\ContainerBuilder(); $containerBuilder->addDefinitions('../config/DI.php'); -- 2.11.0