From 6292f8f33a920ca5166ee21e1b1f8e1f13091444 Mon Sep 17 00:00:00 2001 From: Pawel Mikolajczuk Date: Tue, 24 Feb 2015 11:45:32 +0100 Subject: [PATCH] add playlist create/update/remove actions --- .../Controller/ArticlesListController.php | 102 +++++++++++++++++- .../GimmeBundle/Form/Type/PlaylistType.php | 42 ++++++++ 2 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 newscoop/src/Newscoop/GimmeBundle/Form/Type/PlaylistType.php diff --git a/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesListController.php b/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesListController.php index 42df901b6f..f4bd43b585 100644 --- a/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesListController.php +++ b/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesListController.php @@ -283,7 +283,7 @@ public function unlinkFromPlaylistAction(Request $request, $id) * } * ) * - * @Route("articles-lists/{id}.{_format}", defaults={"_format"="json"}, options={"expose"=true}) + * @Route("articles-lists/{id}/articles.{_format}", defaults={"_format"="json"}, options={"expose"=true}) * @Method("POST") * @View(statusCode=200) */ @@ -390,7 +390,6 @@ public function saveBatchActionsAction(Request $request, $id) * * @Route("articles-lists.{_format}", defaults={"_format"="json"}, options={"expose"=true}) * @Method("POST") - * @View(statusCode=200) */ public function createPlaylistAction(Request $request) { @@ -399,15 +398,69 @@ public function createPlaylistAction(Request $request) throw new AccessDeniedException("You do not have the right to manage playlists."); } + $em = $this->container->get('em'); $playlist = new Playlist(); $form = $this->createForm(new PlaylistType(), $playlist); $form->handleRequest($request); if ($form->isValid()) { + $em->persist($playlist); + $em->flush(); + + $view = FOSView\View::create($playlist, 200); + $view->setHeader('X-Location', $this->generateUrl('newscoop_gimme_articles_lists_getlist', array( + 'id' => $playlist->getId(), + ), true)); + + return $view; + } + + return $form; + } + + /** + * Update playlist + * + * @ApiDoc( + * statusCodes={ + * 201="Returned when successful", + * 404="Returned when resource not found" + * }, + * parameters={ + * {"name"="id", "dataType"="integer", "required"=true, "description"="Playlist id"}, + * {"name"="access_token", "dataType"="string", "required"=false, "description"="Access token"} + * }, + * input="\Newscoop\GimmeBundle\Form\Type\PlaylistType" + * ) + * + * @Route("articles-lists/{id}.{_format}", defaults={"_format"="json"}, options={"expose"=true}) + * @Method("PATCH|POST") + */ + public function updatePlaylistAction(Request $request, $id) + { + $user = $this->container->get('user')->getCurrentUser(); + if (!$user->hasPermission('ManagePlaylist')) { + throw new AccessDeniedException("You do not have the right to manage playlists."); + } + + $em = $this->container->get('em'); + $playlist = $em->getRepository('Newscoop\Entity\Playlist') + ->getPlaylist($id) + ->getOneOrNullResult(); + + if (!$playlist) { + throw new NotFoundHttpException('Result was not found.'); + } + + $form = $this->createForm(new PlaylistType(), $playlist, array( + 'method' => $request->getMethod() + )); + $form->handleRequest($request); - //create playlist - + if ($form->isValid()) { + $em->persist($playlist); + $em->flush(); $view = FOSView\View::create($playlist, 201); $view->setHeader('X-Location', $this->generateUrl('newscoop_gimme_articles_lists_getlist', array( @@ -420,6 +473,47 @@ public function createPlaylistAction(Request $request) return $form; } + /** + * Delete playlist + * + * @ApiDoc( + * statusCodes={ + * 204="Returned when playlist removed succesfuly", + * 404={ + * "Returned when the playlist is not found", + * } + * }, + * parameters={ + * {"name"="id", "dataType"="integer", "required"=true, "description"="{laylist id"} + * } + * ) + * + * @Route("articles-lists/{id}.{_format}", defaults={"_format"="json"}, options={"expose"=true}) + * @Method("DELETE") + * @View(statusCode=204) + * + * @return Form + */ + public function deletePlaylistAction(Request $request, $id) + { + $user = $this->container->get('user')->getCurrentUser(); + if (!$user->hasPermission('ManagePlaylist')) { + throw new AccessDeniedException("You do not have the right to manage playlists."); + } + + $em = $this->container->get('em'); + $playlist = $em->getRepository('Newscoop\Entity\Playlist') + ->getPlaylist($id) + ->getOneOrNullResult(); + + if (!$playlist) { + throw new NotFoundHttpException('Result was not found.'); + } + + $em->remove($playlist); + $em->flush(); + } + private function linkOrUnlinkResources($playlist, $request, $action = 'link') { $matched = false; diff --git a/newscoop/src/Newscoop/GimmeBundle/Form/Type/PlaylistType.php b/newscoop/src/Newscoop/GimmeBundle/Form/Type/PlaylistType.php new file mode 100644 index 0000000000..4de23a8b00 --- /dev/null +++ b/newscoop/src/Newscoop/GimmeBundle/Form/Type/PlaylistType.php @@ -0,0 +1,42 @@ + + * @copyright 2015 Sourcefabric z.ú. + * @license http://www.gnu.org/licenses/gpl-3.0.txt + */ + +namespace Newscoop\GimmeBundle\Form\Type; + +use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolverInterface; + +class PlaylistType extends AbstractType +{ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('name', 'text', array( + 'required' => true, + )) + ->add('notes', 'text', array( + 'required' => false, + )) + ->add('maxItems', 'integer', array( + 'required' => false, + )); + } + + public function getName() + { + return 'playlist'; + } + + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array( + 'csrf_protection' => false + )); + } +}