diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-05-07 15:55:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-07 15:55:38 +0200 |
commit | 77de24876ff542e3770aa2845e993c58f87e37df (patch) | |
tree | 9eca13b3718eeba2d69fe262a8b6b35ba2596ea5 /application/api/controllers | |
parent | eb6e729808e1c3d9787ad1183aa61ab2e375a537 (diff) | |
parent | cf9181dddf8b6113b1b017e4bcb21fac0a0b1c83 (diff) | |
download | Shaarli-77de24876ff542e3770aa2845e993c58f87e37df.tar.gz Shaarli-77de24876ff542e3770aa2845e993c58f87e37df.tar.zst Shaarli-77de24876ff542e3770aa2845e993c58f87e37df.zip |
Merge pull request #840 from ArthurHoaro/api/putLink
REST API: implement PUT method
Diffstat (limited to 'application/api/controllers')
-rw-r--r-- | application/api/controllers/Links.php | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php index 0db10fd0..1c68b062 100644 --- a/application/api/controllers/Links.php +++ b/application/api/controllers/Links.php | |||
@@ -146,4 +146,46 @@ class Links extends ApiController | |||
146 | return $response->withAddedHeader('Location', $redirect) | 146 | return $response->withAddedHeader('Location', $redirect) |
147 | ->withJson($out, 201, $this->jsonStyle); | 147 | ->withJson($out, 201, $this->jsonStyle); |
148 | } | 148 | } |
149 | |||
150 | /** | ||
151 | * Updates an existing link from posted request body. | ||
152 | * | ||
153 | * @param Request $request Slim request. | ||
154 | * @param Response $response Slim response. | ||
155 | * @param array $args Path parameters. including the ID. | ||
156 | * | ||
157 | * @return Response response. | ||
158 | * | ||
159 | * @throws ApiLinkNotFoundException generating a 404 error. | ||
160 | */ | ||
161 | public function putLink($request, $response, $args) | ||
162 | { | ||
163 | if (! isset($this->linkDb[$args['id']])) { | ||
164 | throw new ApiLinkNotFoundException(); | ||
165 | } | ||
166 | |||
167 | $index = index_url($this->ci['environment']); | ||
168 | $data = $request->getParsedBody(); | ||
169 | |||
170 | $requestLink = ApiUtils::buildLinkFromRequest($data, $this->conf->get('privacy.default_private_links')); | ||
171 | // duplicate URL on a different link, return 409 Conflict | ||
172 | if (! empty($requestLink['url']) | ||
173 | && ! empty($dup = $this->linkDb->getLinkFromUrl($requestLink['url'])) | ||
174 | && $dup['id'] != $args['id'] | ||
175 | ) { | ||
176 | return $response->withJson( | ||
177 | ApiUtils::formatLink($dup, $index), | ||
178 | 409, | ||
179 | $this->jsonStyle | ||
180 | ); | ||
181 | } | ||
182 | |||
183 | $responseLink = $this->linkDb[$args['id']]; | ||
184 | $responseLink = ApiUtils::updateLink($responseLink, $requestLink); | ||
185 | $this->linkDb[$responseLink['id']] = $responseLink; | ||
186 | $this->linkDb->save($this->conf->get('resource.page_cache')); | ||
187 | |||
188 | $out = ApiUtils::formatLink($responseLink, $index); | ||
189 | return $response->withJson($out, 200, $this->jsonStyle); | ||
190 | } | ||
149 | } | 191 | } |