diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2017-04-24 10:22:57 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2017-05-01 09:25:34 +0200 |
commit | 1eca7831a69b9470b92dcc72e1ce51b42b291338 (patch) | |
tree | a8627c4bc1457729f92096ab4b36ece95b1a8857 /src/Wallabag/ApiBundle/Controller/EntryRestController.php | |
parent | 3cbb0cc3ef89873a06bda6583747a2660b989fb8 (diff) | |
download | wallabag-1eca7831a69b9470b92dcc72e1ce51b42b291338.tar.gz wallabag-1eca7831a69b9470b92dcc72e1ce51b42b291338.tar.zst wallabag-1eca7831a69b9470b92dcc72e1ce51b42b291338.zip |
Added API endpoint to handle a list of URL
By passing an array, you can add / delete URL in mass (bulk request)
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 7590efbb..3833ce3c 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -173,6 +173,77 @@ class EntryRestController extends WallabagRestController | |||
173 | } | 173 | } |
174 | 174 | ||
175 | /** | 175 | /** |
176 | * Handles an entries list and create or remove URL. | ||
177 | * | ||
178 | * @ApiDoc( | ||
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."} | ||
181 | * } | ||
182 | * ) | ||
183 | * | ||
184 | * @return JsonResponse | ||
185 | */ | ||
186 | public function postEntriesListAction(Request $request) | ||
187 | { | ||
188 | $this->validateAuthentication(); | ||
189 | |||
190 | $list = json_decode($request->query->get('list', [])); | ||
191 | $results = []; | ||
192 | |||
193 | // handle multiple urls | ||
194 | if (!empty($list)) { | ||
195 | $results = []; | ||
196 | foreach ($list as $key => $element) { | ||
197 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | ||
198 | $element->url, | ||
199 | $this->getUser()->getId() | ||
200 | ); | ||
201 | |||
202 | $results[$key]['url'] = $element->url; | ||
203 | $results[$key]['action'] = $element->action; | ||
204 | |||
205 | switch ($element->action) { | ||
206 | case 'delete': | ||
207 | if (false !== $entry) { | ||
208 | $em = $this->getDoctrine()->getManager(); | ||
209 | $em->remove($entry); | ||
210 | $em->flush(); | ||
211 | |||
212 | // entry deleted, dispatch event about it! | ||
213 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | ||
214 | } | ||
215 | |||
216 | $results[$key]['entry'] = $entry instanceof Entry ? true : false; | ||
217 | |||
218 | break; | ||
219 | case 'add': | ||
220 | if (false === $entry) { | ||
221 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry( | ||
222 | new Entry($this->getUser()), | ||
223 | $element->url | ||
224 | ); | ||
225 | } | ||
226 | |||
227 | $em = $this->getDoctrine()->getManager(); | ||
228 | $em->persist($entry); | ||
229 | $em->flush(); | ||
230 | |||
231 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | ||
232 | |||
233 | // entry saved, dispatch event about it! | ||
234 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
235 | |||
236 | break; | ||
237 | } | ||
238 | } | ||
239 | } | ||
240 | |||
241 | $json = $this->get('serializer')->serialize($results, 'json'); | ||
242 | |||
243 | return (new JsonResponse())->setJson($json); | ||
244 | } | ||
245 | |||
246 | /** | ||
176 | * Create an entry. | 247 | * Create an entry. |
177 | * | 248 | * |
178 | * @ApiDoc( | 249 | * @ApiDoc( |