aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/api/controllers/Links.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-04-01 11:11:25 +0200
committerArthurHoaro <arthur@hoa.ro>2017-05-07 15:49:16 +0200
commitcf9181dddf8b6113b1b017e4bcb21fac0a0b1c83 (patch)
treed8426d3b7b61dbd5f9d849e1114c3ee64d102db3 /application/api/controllers/Links.php
parent4b385d6c344c4a0a0b424622833bc72974c21cb5 (diff)
downloadShaarli-cf9181dddf8b6113b1b017e4bcb21fac0a0b1c83.tar.gz
Shaarli-cf9181dddf8b6113b1b017e4bcb21fac0a0b1c83.tar.zst
Shaarli-cf9181dddf8b6113b1b017e4bcb21fac0a0b1c83.zip
REST API: implement PUT method
* Related to #609 * Documentation: http://shaarli.github.io/api-documentation/#links-link-put
Diffstat (limited to 'application/api/controllers/Links.php')
-rw-r--r--application/api/controllers/Links.php42
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}