From 3c0858b706542ef7fb77a80d7d07e80d5e6ab1b6 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Tue, 16 Aug 2022 03:58:55 +0000 Subject: [PATCH] Add folder APIs --- appinfo/routes.php | 2 ++ lib/Controller/ApiController.php | 32 +++++++++++++++++ lib/Db/Util.php | 60 ++++++++++++++++++++++++++------ 3 files changed, 84 insertions(+), 10 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index cc26a968..9debe1ea 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -6,5 +6,7 @@ return [ // API ['name' => 'api#days', 'url' => '/api/days', 'verb' => 'GET'], ['name' => 'api#day', 'url' => '/api/days/{id}', 'verb' => 'GET'], + ['name' => 'api#shared', 'url' => '/api/shared/{folder}', 'verb' => 'GET'], + ['name' => 'api#sharedDay', 'url' => '/api/shared/{folder}/{dayId}', 'verb' => 'GET'], ] ]; diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 61770906..e4569cd8 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -88,6 +88,38 @@ class ApiController extends Controller { return new JSONResponse($list, Http::STATUS_OK); } + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function shared(string $folder): JSONResponse { + $user = $this->userSession->getUser(); + if (is_null($user) || !is_numeric($folder)) { + return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED); + } + + $list = $this->util->getDaysFolder(intval($folder)); + return new JSONResponse($list, Http::STATUS_OK); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function sharedDay(string $folder, string $dayId): JSONResponse { + $user = $this->userSession->getUser(); + if (is_null($user) || !is_numeric($folder) || !is_numeric($dayId)) { + return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED); + } + + $list = $this->util->getDayFolder(intval($folder), intval($dayId)); + return new JSONResponse($list, Http::STATUS_OK); + } + /** * @NoAdminRequired * diff --git a/lib/Db/Util.php b/lib/Db/Util.php index 528e65ec..9a1c05c4 100644 --- a/lib/Db/Util.php +++ b/lib/Db/Util.php @@ -79,6 +79,14 @@ class Util { $this->connection->executeStatement($sql, [$file->getId()], [\PDO::PARAM_INT]); } + public function processDays(&$days) { + foreach($days as &$row) { + $row["day_id"] = intval($row["day_id"]); + $row["count"] = intval($row["count"]); + } + return $days; + } + public function getDays( string $user, ): array { @@ -90,7 +98,32 @@ class Util { $rows = $this->connection->executeQuery($sql, [$user], [ \PDO::PARAM_STR, ])->fetchAll(); - return $rows; + return $this->processDays($rows); + } + + public function getDaysFolder(int $folderId) { + $sql = 'SELECT day_id, COUNT(file_id) AS count + FROM `oc_polaroid` + INNER JOIN `oc_filecache` + ON `oc_polaroid`.`file_id` = `oc_filecache`.`fileid` + AND (`oc_filecache`.`parent`=? OR `oc_filecache`.`fileid`=?) + GROUP BY day_id + ORDER BY day_id DESC'; + $rows = $this->connection->executeQuery($sql, [$folderId, $folderId], [ + \PDO::PARAM_INT, \PDO::PARAM_INT, + ])->fetchAll(); + return $this->processDays($rows); + } + + public function processDay(&$day) { + foreach($day as &$row) { + $row["file_id"] = intval($row["file_id"]); + $row["is_video"] = intval($row["is_video"]); + if (!$row["is_video"]) { + unset($row["is_video"]); + } + } + return $day; } public function getDay( @@ -106,15 +139,22 @@ class Util { $rows = $this->connection->executeQuery($sql, [$user, $dayId], [ \PDO::PARAM_STR, \PDO::PARAM_INT, ])->fetchAll(); + return $this->processDay($rows); + } - foreach($rows as &$row) { - $row["file_id"] = intval($row["file_id"]); - $row["is_video"] = intval($row["is_video"]); - if (!$row["is_video"]) { - unset($row["is_video"]); - } - } - - return $rows; + public function getDayFolder( + int $folderId, + int $dayId, + ): array { + $sql = 'SELECT file_id, oc_filecache.etag, is_video + FROM `oc_polaroid` + INNER JOIN `oc_filecache` + ON `oc_polaroid`.`day_id`=? + AND `oc_polaroid`.`file_id` = `oc_filecache`.`fileid` + AND (`oc_filecache`.`parent`=? OR `oc_filecache`.`fileid`=?);'; + $rows = $this->connection->executeQuery($sql, [$dayId, $folderId, $folderId], [ + \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, + ])->fetchAll(); + return $this->processDay($rows); } } \ No newline at end of file