Skip to content

Commit

Permalink
Merge branch 'playlists' of https://github.com/takeit/Newscoop into p…
Browse files Browse the repository at this point in the history
…laylists
  • Loading branch information
takeit committed Feb 25, 2015
2 parents 59a4dfc + 50a1fe4 commit aea8a61
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
13 changes: 13 additions & 0 deletions newscoop/library/Newscoop/Entity/Repository/PlaylistRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ public function getPlaylist($id)
return $query;
}

public function getPlaylistByTitle($title)
{
$em = $this->getEntityManager();
$queryBuilder = $em->getRepository('Newscoop\Entity\Playlist')
->createQueryBuilder('p')
->where('p.name = :title')
->setParameter('title', $title);

$query = $queryBuilder->getQuery();

return $query;
}

/**
* Returns articles for a given playlist
* @param Newscoop\Entity\Playlist $playlist
Expand Down
15 changes: 10 additions & 5 deletions newscoop/library/Newscoop/Services/PlaylistsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,7 @@ private function positionPlaylistArticle($playlist, $playlistArticle, $position)
$this->em->flush();

if ($oldOrder == 0 && $playlist->getMaxItems() !== null && (int) $maxPosition+1 >= $playlist->getMaxItems()) {
$this->em
->createQuery('DELETE FROM Newscoop\Entity\PlaylistArticle pa WHERE pa.order > :maxPosition AND pa.idPlaylist = :playlistId')
->setParameter('maxPosition', $playlist->getMaxItems())
->setParameter('playlistId', $playlist->getId())
->execute();
$this->removeLeftItems($playlist);
}

$this->em->getConnection()->exec('UNLOCK TABLES;');
Expand All @@ -207,4 +203,13 @@ private function positionPlaylistArticle($playlist, $playlistArticle, $position)
)));
$this->cacheService->clearNamespace('boxarticles');
}

public function removeLeftItems($playlist)
{
$this->em
->createQuery('DELETE FROM Newscoop\Entity\PlaylistArticle pa WHERE pa.order > :maxPosition AND pa.idPlaylist = :playlistId')
->setParameter('maxPosition', $playlist->getMaxItems())
->setParameter('playlistId', $playlist->getId())
->execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Newscoop\Entity\Article;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Newscoop\Exception\ResourcesConflictException;
use Newscoop\Exception\InvalidParametersException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
Expand Down Expand Up @@ -384,7 +385,8 @@ public function saveBatchActionsAction(Request $request, $id)
* },
* parameters={
* {"name"="access_token", "dataType"="string", "required"=false, "description"="Access token"}
* }
* },
* input="\Newscoop\GimmeBundle\Form\Type\PlaylistType"
* )
*
* @Route("articles-lists.{_format}", defaults={"_format"="json"}, options={"expose"=true})
Expand All @@ -404,6 +406,14 @@ public function createPlaylistAction(Request $request)
$form->handleRequest($request);

if ($form->isValid()) {
$existingPlaylist = $em->getRepository('Newscoop\Entity\Playlist')
->getPlaylistByTitle($playlist->getName())
->getOneOrNullResult();

if ($existingPlaylist) {
throw new ResourcesConflictException("Playlist with that name already exists", 409);
}

$em->persist($playlist);
$em->flush();

Expand Down Expand Up @@ -451,16 +461,21 @@ public function updatePlaylistAction(Request $request, $id)
if (!$playlist) {
throw new NotFoundHttpException('Result was not found.');
}
$oldMaxItems = $playlist->getMaxItems();

$form = $this->createForm(new PlaylistType(), $playlist, array(
'method' => $request->getMethod()
));
$form->handleRequest($request);

if ($form->isValid()) {
$em->persist($playlist);
$em->flush();

if ($oldMaxItems != $playlist->getMaxItems()) {
$playlistService = $this->get('playlists');
$playlistService->removeLeftItems($playlist);
}

$view = FOSView\View::create($playlist, 201);
$view->setHeader('X-Location', $this->generateUrl('newscoop_gimme_articles_lists_getlist', array(
'id' => $playlist->getId(),
Expand Down

0 comments on commit aea8a61

Please sign in to comment.