diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2017-04-24 11:31:00 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2017-05-01 09:26:12 +0200 |
commit | a7abcc7b7a5e3417eff70e2a5993558f83fc5d5a (patch) | |
tree | 4f4c3212fbcaee839e0fa4910b32a6f4f72f6c43 /src/Wallabag/ApiBundle/Controller/EntryRestController.php | |
parent | 1eca7831a69b9470b92dcc72e1ce51b42b291338 (diff) | |
download | wallabag-a7abcc7b7a5e3417eff70e2a5993558f83fc5d5a.tar.gz wallabag-a7abcc7b7a5e3417eff70e2a5993558f83fc5d5a.tar.zst wallabag-a7abcc7b7a5e3417eff70e2a5993558f83fc5d5a.zip |
Splitted the endpoint in two
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 96 |
1 files changed, 62 insertions, 34 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 3833ce3c..0c98c242 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -173,68 +173,96 @@ class EntryRestController extends WallabagRestController | |||
173 | } | 173 | } |
174 | 174 | ||
175 | /** | 175 | /** |
176 | * Handles an entries list and create or remove URL. | 176 | * Handles an entries list and delete URL. |
177 | * | 177 | * |
178 | * @ApiDoc( | 178 | * @ApiDoc( |
179 | * parameters={ | 179 | * parameters={ |
180 | * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...', 'action': 'delete'}, {'url': 'http://...', 'action': 'add'}]", "description"="Urls (as an array) to handle."} | 180 | * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to delete."} |
181 | * } | 181 | * } |
182 | * ) | 182 | * ) |
183 | * | 183 | * |
184 | * @return JsonResponse | 184 | * @return JsonResponse |
185 | */ | 185 | */ |
186 | public function postEntriesListAction(Request $request) | 186 | public function deleteEntriesListAction(Request $request) |
187 | { | 187 | { |
188 | $this->validateAuthentication(); | 188 | $this->validateAuthentication(); |
189 | 189 | ||
190 | $list = json_decode($request->query->get('list', [])); | 190 | $urls = json_decode($request->query->get('urls', [])); |
191 | $results = []; | 191 | $results = []; |
192 | 192 | ||
193 | // handle multiple urls | 193 | // handle multiple urls |
194 | if (!empty($list)) { | 194 | if (!empty($urls)) { |
195 | $results = []; | 195 | $results = []; |
196 | foreach ($list as $key => $element) { | 196 | foreach ($urls as $key => $url) { |
197 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | 197 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( |
198 | $element->url, | 198 | $url, |
199 | $this->getUser()->getId() | 199 | $this->getUser()->getId() |
200 | ); | 200 | ); |
201 | 201 | ||
202 | $results[$key]['url'] = $element->url; | 202 | $results[$key]['url'] = $url; |
203 | $results[$key]['action'] = $element->action; | ||
204 | 203 | ||
205 | switch ($element->action) { | 204 | if (false !== $entry) { |
206 | case 'delete': | 205 | $em = $this->getDoctrine()->getManager(); |
207 | if (false !== $entry) { | 206 | $em->remove($entry); |
208 | $em = $this->getDoctrine()->getManager(); | 207 | $em->flush(); |
209 | $em->remove($entry); | ||
210 | $em->flush(); | ||
211 | 208 | ||
212 | // entry deleted, dispatch event about it! | 209 | // entry deleted, dispatch event about it! |
213 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | 210 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); |
214 | } | 211 | } |
215 | 212 | ||
216 | $results[$key]['entry'] = $entry instanceof Entry ? true : false; | 213 | $results[$key]['entry'] = $entry instanceof Entry ? true : false; |
214 | } | ||
215 | } | ||
217 | 216 | ||
218 | break; | 217 | $json = $this->get('serializer')->serialize($results, 'json'); |
219 | case 'add': | 218 | |
220 | if (false === $entry) { | 219 | return (new JsonResponse())->setJson($json); |
221 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry( | 220 | } |
222 | new Entry($this->getUser()), | 221 | |
223 | $element->url | 222 | /** |
224 | ); | 223 | * Handles an entries list and create URL. |
225 | } | 224 | * |
225 | * @ApiDoc( | ||
226 | * parameters={ | ||
227 | * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to create."} | ||
228 | * } | ||
229 | * ) | ||
230 | * | ||
231 | * @return JsonResponse | ||
232 | */ | ||
233 | public function postEntriesListAction(Request $request) | ||
234 | { | ||
235 | $this->validateAuthentication(); | ||
226 | 236 | ||
227 | $em = $this->getDoctrine()->getManager(); | 237 | $urls = json_decode($request->query->get('urls', [])); |
228 | $em->persist($entry); | 238 | $results = []; |
229 | $em->flush(); | ||
230 | 239 | ||
231 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | 240 | // handle multiple urls |
241 | if (!empty($urls)) { | ||
242 | $results = []; | ||
243 | foreach ($urls as $key => $url) { | ||
244 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | ||
245 | $url, | ||
246 | $this->getUser()->getId() | ||
247 | ); | ||
232 | 248 | ||
233 | // entry saved, dispatch event about it! | 249 | $results[$key]['url'] = $url; |
234 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
235 | 250 | ||
236 | break; | 251 | if (false === $entry) { |
252 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry( | ||
253 | new Entry($this->getUser()), | ||
254 | $url | ||
255 | ); | ||
237 | } | 256 | } |
257 | |||
258 | $em = $this->getDoctrine()->getManager(); | ||
259 | $em->persist($entry); | ||
260 | $em->flush(); | ||
261 | |||
262 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | ||
263 | |||
264 | // entry saved, dispatch event about it! | ||
265 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
238 | } | 266 | } |
239 | } | 267 | } |
240 | 268 | ||