Skip to content

Commit

Permalink
create a single API route for locking/unocking
Browse files Browse the repository at this point in the history
  • Loading branch information
takeit committed Nov 18, 2014
1 parent eedcc3e commit fbc8f0f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 55 deletions.
63 changes: 16 additions & 47 deletions newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,11 +657,11 @@ public function changeArticleStatus(Request $request, $number, $language, $statu
}

/**
* Lock article
* Lock or unlock article
*
* @ApiDoc(
* statusCodes={
* 200="Returned when article has been locked successfully",
* 200="Returned when article lock status has been changed successfully",
* 403="Returned when trying to set the same status"
* },
* parameters={
Expand All @@ -671,9 +671,9 @@ public function changeArticleStatus(Request $request, $number, $language, $statu
* )
*
* @Route("/articles/{number}/{language}/lock.{_format}", defaults={"_format"="json", "language"="en"}, options={"expose"=true}, name="newscoop_gimme_articles_changearticlelockstatus")
* @Method("POST")
* @Method("POST|DELETE")
*/
public function postLockArticle(Request $request, $number, $language)
public function lockUnlockArticle(Request $request, $number, $language)
{
$em = $this->container->get('em');
$article = $em->getRepository('Newscoop\Entity\Article')
Expand All @@ -686,51 +686,20 @@ public function postLockArticle(Request $request, $number, $language)

$response = new Response();
$response->setStatusCode(403);
if (!$article->isLocked()) {
$article->setLockUser($this->getUser());
$article->setLockTime(new \DateTime());
$response->setStatusCode(200);
}

$em->flush();

return $response;
}

/**
* Unlock article
*
* @ApiDoc(
* statusCodes={
* 200="Returned when article has been unlocked successfully",
* 403="Returned when trying to set the same status"
* },
* parameters={
* {"name"="number", "dataType"="integer", "required"=true, "description"="Article number"},
* {"name"="language", "dataType"="string", "required"=true, "description"="Language code"}
* }
* )
*
* @Route("/articles/{number}/{language}/lock.{_format}", defaults={"_format"="json", "language"="en"}, options={"expose"=true}, name="newscoop_gimme_articles_changearticleunlockstatus")
* @Method("DELETE")
*/
public function deleteUnlockArticle(Request $request, $number, $language)
{
$em = $this->container->get('em');
$article = $em->getRepository('Newscoop\Entity\Article')
->getArticle($number, $language)
->getOneOrNullResult();

if (!$article) {
throw new NewscoopException('Article does not exist');
if ($request->getMethod() === "POST") {
if (!$article->isLocked()) {
$article->setLockUser($this->getUser());
$article->setLockTime(new \DateTime());
$response->setStatusCode(200);
}
}

$response = new Response();
$response->setStatusCode(403);
if ($article->isLocked()) {
$article->setLockUser();
$article->setLockTime();
$response->setStatusCode(200);
if ($request->getMethod() === "DELETE") {
if ($article->isLocked()) {
$article->setLockUser();
$article->setLockTime();
$response->setStatusCode(200);
}
}

$em->flush();
Expand Down
20 changes: 12 additions & 8 deletions spec/Newscoop/GimmeBundle/Controller/ArticlesControllerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,46 +50,50 @@ public function it_is_initializable()
$this->shouldImplement('FOS\RestBundle\Controller\FOSRestController');
}

public function its_postLockArticle_should_lock_article($request, $article, $query, $number, $language, $user, $token, $security)
public function its_lockUnlockArticle_should_lock_article($request, $article, $query, $number, $language, $user, $token, $security)
{
$query->getOneOrNullResult()->willReturn($article);
$request->getMethod()->willReturn('POST');
$article->isLocked()->willReturn(false);
$user = new User('jhon.doe@example.com');
$user->setUsername('doe');
$security->getToken()->willReturn($token);
$token->getUser()->willReturn($user);
$article->setLockUser($user)->willReturn(null);
$article->setLockTime(new \DateTime())->willReturn(null);
$response = $this->postLockArticle($request, $number, $language);
$response = $this->lockUnlockArticle($request, $number, $language);
$response->shouldBeAnInstanceOf('Symfony\Component\HttpFoundation\Response');
$response->getStatusCode()->shouldReturn(200);
}

public function its_deleteUnlockArticle_should_unlock_article($request, $article, $query, $number, $language)
public function its_lockUnlockArticle_should_unlock_article($request, $article, $query, $number, $language)
{
$article->isLocked()->willReturn(true);
$request->getMethod()->willReturn('DELETE');
$article->setLockUser()->willReturn(null);
$article->setLockTime()->willReturn(null);
$query->getOneOrNullResult()->willReturn($article);
$response = $this->deleteUnlockArticle($request, $number, $language);
$response = $this->lockUnlockArticle($request, $number, $language);
$response->shouldBeAnInstanceOf('Symfony\Component\HttpFoundation\Response');
$response->getStatusCode()->shouldReturn(200);
}

public function its_postLockArticle_should_return_status_code_403_when_setting_the_same_status($request, $article, $query, $number, $language)
public function its_lockUnlockArticle_should_return_status_code_403_when_setting_the_same_status_while_locking($request, $article, $query, $number, $language)
{
$article->isLocked()->willReturn(true);
$request->getMethod()->willReturn('POST');
$query->getOneOrNullResult()->willReturn($article);
$response = $this->postLockArticle($request, $number, $language);
$response = $this->lockUnlockArticle($request, $number, $language);
$response->shouldBeAnInstanceOf('Symfony\Component\HttpFoundation\Response');
$response->getStatusCode()->shouldReturn(403);
}

public function its_deleteUnlockArticle_should_return_status_code_403_when_setting_the_same_status($request, $article, $query, $number, $language)
public function its_deleteUnlockArticle_should_return_status_code_403_when_setting_the_same_status_while_unlocking($request, $article, $query, $number, $language)
{
$article->isLocked()->willReturn(false);
$request->getMethod()->willReturn('DELETE');
$query->getOneOrNullResult()->willReturn($article);
$response = $this->deleteUnlockArticle($request, $number, $language);
$response = $this->lockUnlockArticle($request, $number, $language);
$response->shouldBeAnInstanceOf('Symfony\Component\HttpFoundation\Response');
$response->getStatusCode()->shouldReturn(403);
}
Expand Down

0 comments on commit fbc8f0f

Please sign in to comment.