Skip to content

Commit

Permalink
add playlist create/update/remove actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahilles107 committed Feb 24, 2015
1 parent 8605685 commit 6292f8f
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down Expand Up @@ -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)
{
Expand All @@ -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(
Expand All @@ -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;
Expand Down
42 changes: 42 additions & 0 deletions newscoop/src/Newscoop/GimmeBundle/Form/Type/PlaylistType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* @package Newscoop\NewscoopBundle
* @author Paweł Mikołajczuk <[email protected]>
* @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
));
}
}

0 comments on commit 6292f8f

Please sign in to comment.