Skip to content

Commit

Permalink
Implements the DELETE Resource for the Snippet Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
terwey committed Apr 30, 2014
1 parent 101833e commit 4e6934b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
57 changes: 40 additions & 17 deletions newscoop/library/Newscoop/Entity/Repository/SnippetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Newscoop\Entity\Snippet;
use Newscoop\Exception\ResourcesConflictException;

/**
* Snippet repository
Expand All @@ -33,15 +34,19 @@ class SnippetRepository extends EntityRepository
*
* @return Doctrine\ORM\Querybuilder $queryBuilder
*/
protected function getSnippetQueryBuilder($show)
protected function getSnippetQueryBuilder($show, $templateEnabled = true)
{
if (!in_array($show, array('enabled', 'disabled', 'all'))) {
$show = 'enabled';
}

$queryBuilder = $this->createQueryBuilder('snippet')
->join('snippet.template', 'template')
->andWhere('template.enabled = 1'); // Template should always be enabled
$queryBuilder = $this->createQueryBuilder('snippet');

if ($templateEnabled) { // by default a Template should always be enabled
$queryBuilder
->join('snippet.template', 'template')
->andWhere('template.enabled = 1');
}

if ($show == 'enabled') {
$queryBuilder
Expand Down Expand Up @@ -92,8 +97,19 @@ protected function getArticleSnippetQueryBuilder($articleNr, $language, $show)
}

// need to be able to search for all articles attached to a snippet
protected function getSnippetArticlesQueryBuilder(Snippet $snippet, $show)
protected function getSnippetArticles($id)
{
$queryBuilder = $this->getSnippetQueryBuilder('all', false)
->andWhere('snippet.id = :id')
->setParameter('id', $id);

$snippet = $queryBuilder->getQuery()->getOneOrNullResult();

if (!$snippet) {
throw new \Exception('Snippet with ID: '.$id.' does not exist');
}

return $snippet->getArticles();
}

/**
Expand Down Expand Up @@ -144,13 +160,13 @@ public function getArticleSnippets($articleNr, $languageCode, $show = 'enabled')
*
* @return Newscoop\Entity\Snippet
*/
public function getSnippetById($id, $show = 'enabled')
public function getSnippetById($id, $show = 'enabled', $template = true)
{
if (!is_numeric($id)) {
throw new \InvalidArgumentException("ID is not numeric: ".$id);
}

$queryBuilder = $this->getSnippetQueryBuilder($show)
$queryBuilder = $this->getSnippetQueryBuilder($show, $template)
->andWhere('snippet.id = :id')
->setParameter('id', $id);

Expand Down Expand Up @@ -236,17 +252,24 @@ public function findSnippetsByNameForArticle($articleNr, $languageCode, $name, $
return $queryBuilder->getQuery();
}

public function deleteSnippet(Snippet $snippet, $force = false)
public function deleteSnippet($id, $force = false)
{
// We got to do some checks, remove it from the Article and such
//
// If all is good, and it's not attached, remove

$em = $this->getEntityManager();
$em->remove($snippet);
$em->flush();

return true;
$articles = $this->getSnippetArticles($id)->toArray();
if (count($articles) == 0 || $force == true) {
$snippet = $this->getSnippetById($id, 'all', false);
$em = $this->getEntityManager();
$em->remove($snippet);
$em->flush();

return true;
} else {
$articleNumbers = array();
foreach ($articles as $article) {
$articleNumbersArr[$article->getNumber()] = $article->getNumber();
}
$articleNumbers = implode(", ", array_flip($articleNumbersArr));
throw new \Newscoop\Exception\ResourcesConflictException('Snippet with ID: '.$id.' is in use by Articles ('.$articleNumbers.')');
}
}

public function createSnippetForArticle($articleNr, $languageCode, array $snippetData)
Expand Down
5 changes: 5 additions & 0 deletions newscoop/library/Newscoop/Entity/Snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ public function setData($fieldName, $fieldData = null)

return $this;
}

public function getArticles()
{
return $this->articles;
}

/**
* Getter for enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,40 +193,32 @@ public function updateSnippetAction(Request $request, $snippetId)
}

/**
* Delete comment
* Delete Snippet
*
* @ApiDoc(
* statusCodes={
* 204="Returned when comment removed succesfuly",
* 204="Returned when Snippet removed succesfuly",
* 404={
* "Returned when the comment is not found",
* }
* "Returned when the Snippet is not found",
* },
* 409="Returned when Snippet is used by Articles"
* },
* parameters={
* {"name"="number", "dataType"="integer", "required"=true, "description"="Image id"}
* {"name"="force", "dataType"="boolean", "required"=false, "description"="Force delete"},
* }
* )
*
* @Route("/snippets/{snippetId}.{_format}", defaults={"_format"="json"})
* @Route("/snippets/article/{articleNumber}/{languageCode}/{snippetId}.{_format}", defaults={"_format"="json"})
* @Method("DELETE")
* @View(statusCode=204)
*
* @return Form
*/
public function deleteSnippetAction(Request $request, $snippetId, $articleNumber = null, $languageCode = null)
{
$snippetService = $this->container->get('comment');
$force = $request->query->get('force', false);
$em = $this->container->get('em');
$snippet = $em->getRepository('Newscoop\Entity\Snippet')
->getComment($snippetId, false)
->getOneOrNullResult();

if (!$snippet) {
throw new EntityNotFoundException('Result was not found.');
}

$snippetService->remove($snippet);
$articleSnippets = $em->getRepository('Newscoop\Entity\Snippet')
->deleteSnippet($snippetId, $force);
}

/**
Expand Down

0 comments on commit 4e6934b

Please sign in to comment.