From d3f42ca487287447efb81061609644108044a038 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 19 May 2018 15:04:04 +0200 Subject: Implements Tags endpoints for Shaarli's REST API Endpoints: * List All Tags [GET] * Get a tag [GET] * Update a tag [PUT] * Delete a tag [DELETE] Fixes #904 References shaarli/api-documentation#34 --- application/api/controllers/Tags.php | 161 +++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 application/api/controllers/Tags.php (limited to 'application/api/controllers/Tags.php') diff --git a/application/api/controllers/Tags.php b/application/api/controllers/Tags.php new file mode 100644 index 00000000..6dd78750 --- /dev/null +++ b/application/api/controllers/Tags.php @@ -0,0 +1,161 @@ +getParam('visibility'); + $tags = $this->linkDb->linksCountPerTag([], $visibility); + + // Return tags from the {offset}th tag, starting from 0. + $offset = $request->getParam('offset'); + if (! empty($offset) && ! ctype_digit($offset)) { + throw new ApiBadParametersException('Invalid offset'); + } + $offset = ! empty($offset) ? intval($offset) : 0; + if ($offset > count($tags)) { + return $response->withJson([], 200, $this->jsonStyle); + } + + // limit parameter is either a number of links or 'all' for everything. + $limit = $request->getParam('limit'); + if (empty($limit)) { + $limit = self::$DEFAULT_LIMIT; + } + if (ctype_digit($limit)) { + $limit = intval($limit); + } elseif ($limit === 'all') { + $limit = count($tags); + } else { + throw new ApiBadParametersException('Invalid limit'); + } + + $out = []; + $index = 0; + foreach ($tags as $tag => $occurrences) { + if (count($out) >= $limit) { + break; + } + if ($index++ >= $offset) { + $out[] = ApiUtils::formatTag($tag, $occurrences); + } + } + + return $response->withJson($out, 200, $this->jsonStyle); + } + + /** + * Return a single formatted tag by its name. + * + * @param Request $request Slim request. + * @param Response $response Slim response. + * @param array $args Path parameters. including the tag name. + * + * @return Response containing the link array. + * + * @throws ApiTagNotFoundException generating a 404 error. + */ + public function getTag($request, $response, $args) + { + $tags = $this->linkDb->linksCountPerTag(); + if (!isset($tags[$args['tagName']])) { + throw new ApiTagNotFoundException(); + } + $out = ApiUtils::formatTag($args['tagName'], $tags[$args['tagName']]); + + return $response->withJson($out, 200, $this->jsonStyle); + } + + /** + * Rename a tag from the given name. + * If the new name provided matches an existing tag, they will be merged. + * + * @param Request $request Slim request. + * @param Response $response Slim response. + * @param array $args Path parameters. including the tag name. + * + * @return Response response. + * + * @throws ApiTagNotFoundException generating a 404 error. + * @throws ApiBadParametersException new tag name not provided + */ + public function putTag($request, $response, $args) + { + $tags = $this->linkDb->linksCountPerTag(); + if (! isset($tags[$args['tagName']])) { + throw new ApiTagNotFoundException(); + } + + $data = $request->getParsedBody(); + if (empty($data['name'])) { + throw new ApiBadParametersException('New tag name is required in the request body'); + } + + $updated = $this->linkDb->renameTag($args['tagName'], $data['name']); + $this->linkDb->save($this->conf->get('resource.page_cache')); + foreach ($updated as $link) { + $this->history->updateLink($link); + } + + $tags = $this->linkDb->linksCountPerTag(); + $out = ApiUtils::formatTag($data['name'], $tags[$data['name']]); + return $response->withJson($out, 200, $this->jsonStyle); + } + + /** + * Delete an existing tag by its name. + * + * @param Request $request Slim request. + * @param Response $response Slim response. + * @param array $args Path parameters. including the tag name. + * + * @return Response response. + * + * @throws ApiTagNotFoundException generating a 404 error. + */ + public function deleteTag($request, $response, $args) + { + $tags = $this->linkDb->linksCountPerTag(); + if (! isset($tags[$args['tagName']])) { + throw new ApiTagNotFoundException(); + } + $updated = $this->linkDb->renameTag($args['tagName'], null); + $this->linkDb->save($this->conf->get('resource.page_cache')); + foreach ($updated as $link) { + $this->history->updateLink($link); + } + + return $response->withStatus(204); + } +} -- cgit v1.2.3