diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-04-01 11:11:25 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2017-05-07 15:49:16 +0200 |
commit | cf9181dddf8b6113b1b017e4bcb21fac0a0b1c83 (patch) | |
tree | d8426d3b7b61dbd5f9d849e1114c3ee64d102db3 /application/api | |
parent | 4b385d6c344c4a0a0b424622833bc72974c21cb5 (diff) | |
download | Shaarli-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')
-rw-r--r-- | application/api/ApiUtils.php | 26 | ||||
-rw-r--r-- | application/api/controllers/Links.php | 42 |
2 files changed, 68 insertions, 0 deletions
diff --git a/application/api/ApiUtils.php b/application/api/ApiUtils.php index b8155a34..f154bb52 100644 --- a/application/api/ApiUtils.php +++ b/application/api/ApiUtils.php | |||
@@ -108,4 +108,30 @@ class ApiUtils | |||
108 | ]; | 108 | ]; |
109 | return $link; | 109 | return $link; |
110 | } | 110 | } |
111 | |||
112 | /** | ||
113 | * Update link fields using an updated link object. | ||
114 | * | ||
115 | * @param array $oldLink data | ||
116 | * @param array $newLink data | ||
117 | * | ||
118 | * @return array $oldLink updated with $newLink values | ||
119 | */ | ||
120 | public static function updateLink($oldLink, $newLink) | ||
121 | { | ||
122 | foreach (['title', 'url', 'description', 'tags', 'private'] as $field) { | ||
123 | $oldLink[$field] = $newLink[$field]; | ||
124 | } | ||
125 | $oldLink['updated'] = new \DateTime(); | ||
126 | |||
127 | if (empty($oldLink['url'])) { | ||
128 | $oldLink['url'] = '?' . $oldLink['shorturl']; | ||
129 | } | ||
130 | |||
131 | if (empty($oldLink['title'])) { | ||
132 | $oldLink['title'] = $oldLink['url']; | ||
133 | } | ||
134 | |||
135 | return $oldLink; | ||
136 | } | ||
111 | } | 137 | } |
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 | } |