From aa008dbe914f5b27fc8c25c2eb829faa00f5d584 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sun, 14 Aug 2022 23:31:47 +0000 Subject: [PATCH] Add days api --- appinfo/routes.php | 3 ++- lib/Controller/ApiController.php | 22 ++++++++++++++++++--- lib/Db/Util.php | 33 +++++++++++++++++++------------- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index db2a8b4a..cc26a968 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -4,6 +4,7 @@ return [ ['name' => 'page#index', 'url' => '/', 'verb' => 'GET'], // API - ['name' => 'api#list', 'url' => '/api/list', 'verb' => 'GET'], + ['name' => 'api#days', 'url' => '/api/days', 'verb' => 'GET'], + ['name' => 'api#day', 'url' => '/api/days/{id}', 'verb' => 'GET'], ] ]; diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index f443815d..f8f3b409 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -54,19 +54,35 @@ class ApiController extends Controller { $this->connection = $connection; } - /** + /** * @NoAdminRequired * @NoCSRFRequired * * @return JSONResponse */ - public function list(): JSONResponse { + public function days(): JSONResponse { $user = $this->userSession->getUser(); if (is_null($user)) { return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED); } - $list = \OCA\BetterPhotos\Db\Util::getList($this->connection, $user->getUID()); + $list = \OCA\BetterPhotos\Db\Util::getDays($this->connection, $user->getUID()); + return new JSONResponse($list, Http::STATUS_OK); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function day(string $id): JSONResponse { + $user = $this->userSession->getUser(); + if (is_null($user) || !is_numeric($id)) { + return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED); + } + + $list = \OCA\BetterPhotos\Db\Util::getDay($this->connection, $user->getUID(), intval($id)); return new JSONResponse($list, Http::STATUS_OK); } diff --git a/lib/Db/Util.php b/lib/Db/Util.php index a5eabb32..2f006a4c 100644 --- a/lib/Db/Util.php +++ b/lib/Db/Util.php @@ -39,7 +39,7 @@ class Util { // Get parameters $fileId = $file->getId(); $dateTaken = $this->getDateTaken($file); - $dayId = 0; + $dayId = floor($dateTaken / 86400); // Insert or update file // todo: update dateTaken and dayId if needed @@ -83,24 +83,31 @@ class Util { } } - private static function getListQuery( + public static function getDays( IDBConnection $connection, string $user, - ) { + ): array { + $qb = $connection->getQueryBuilder(); + $qb->select('day_id', 'count') + ->from('betterphotos_day') + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user))) + ->orderBy('day_id', 'DESC'); + $result = $qb->executeQuery(); + $rows = $result->fetchAll(); + return $rows; + } + + public static function getDay( + IDBConnection $connection, + string $user, + int $dayId, + ): array { $qb = $connection->getQueryBuilder(); $qb->select('file_id', 'date_taken') ->from('betterphotos') ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user))) - ->orderBy('date_taken', 'DESC') - ->setMaxResults(100); - return $qb; - } - - public static function getList( - IDBConnection $connection, - string $user, - ): array { - $qb = self::getListQuery($connection, $user); + ->andWhere($qb->expr()->eq('day_id', $qb->createNamedParameter($dayId))) + ->orderBy('date_taken', 'DESC'); $result = $qb->executeQuery(); $rows = $result->fetchAll(); return $rows;