aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/api/controllers/Links.php
diff options
context:
space:
mode:
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}