- $link['id'] = $this->linkDb->getNextId();
- $link['shorturl'] = link_small_hash($link['created'], $link['id']);
+ $this->bookmarkService->add($bookmark);
+ $out = ApiUtils::formatLink($bookmark, index_url($this->ci['environment']));
+ $redirect = $this->ci->router->pathFor('getLink', ['id' => $bookmark->getId()]);
+ return $response->withAddedHeader('Location', $redirect)
+ ->withJson($out, 201, $this->jsonStyle);
+ }
+
+ /**
+ * Updates an existing link from posted request body.
+ *
+ * @param Request $request Slim request.
+ * @param Response $response Slim response.
+ * @param array $args Path parameters. including the ID.
+ *
+ * @return Response response.
+ *
+ * @throws ApiLinkNotFoundException generating a 404 error.
+ */
+ public function putLink($request, $response, $args)
+ {
+ $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
+ if ($id === null || !$this->bookmarkService->exists($id)) {
+ throw new ApiLinkNotFoundException();
+ }
+
+ $index = index_url($this->ci['environment']);
+ $data = $request->getParsedBody();