Skip to content

Commit

Permalink
Implements removeByForeignId API method
Browse files Browse the repository at this point in the history
  • Loading branch information
artstorm committed Aug 19, 2015
1 parent ced54b8 commit 3310fff
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 10 deletions.
6 changes: 1 addition & 5 deletions api/app/Models/ArticleDiscussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ public function create()
*/
public function delete()
{
$discussion = $this->client->api('discussions')->findByForeignId(
$this->client->api('discussions')->removeByForeignId(
$this->article->article_id
);

$this->client->api('discussions')->remove(
$discussion['Discussion']['DiscussionID']
);
}
}
12 changes: 12 additions & 0 deletions api/app/Services/Api/Vanilla/Api/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,16 @@ public function remove($id)
{
return $this->delete('discussions/'.$id);
}

/**
* Remove a discussion by foreign id.
*
* @param string $id
*
* @return array
*/
public function removeByForeignId($id)
{
return $this->delete('discussions/foreign/'.$id);
}
}
14 changes: 14 additions & 0 deletions api/tests/Services/Api/VanillaApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ public function shouldRemoveDiscussion()
$api->remove(123);
}

/**
* @test
*/
public function shouldRemoveDiscussionByForeignId()
{
$api = $this->getApiMock(Discussion::class);

$api->expects($this->once())
->method('delete')
->with('discussions/foreign/123');

$api->removeByForeignId(123);
}


// Comments

Expand Down
16 changes: 16 additions & 0 deletions api/tests/Services/Api/VanillaIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Rhumsaa\Uuid\Uuid;
use App\Services\Api\Vanilla\Client;

class VanillaIntegrationTest extends TestCase
Expand Down Expand Up @@ -252,6 +253,21 @@ public function shouldDeleteDiscussion()
$this->client->api('discussions')->find($id);
}

/**
* @test
*
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function shouldDeleteDiscussionByForeignId()
{
$id = (string) Uuid::uuid4();
$discussion = $this->client->api('discussions')->create('Foo', 'Bar', 1, ['ForeignID' => $id]);

$this->client->api('discussions')->removeByForeignId($id);

$this->client->api('discussions')->find($discussion['Discussion']['DiscussionID']);
}


// Comments

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ApiDiscussionController extends DiscussionController
/**
* Get a single discussion.
*
* @param int $foreignId
* @param string $foreignId
* @param string $page
*
* @throws Gdn_UserException
Expand All @@ -29,6 +29,30 @@ public function getByForeignId($foreignId = '', $page = '')
$this->index($this->Discussion->DiscussionID, '', $page);
}

/**
* Delete a single discussion.
*
* @param string $foreignId
*
* @throws Gdn_UserException
*
* @return void
*/
public function deleteByForeignId($foreignId = '')
{
if (!$this->isValidUuid($foreignId)) {
throw new Gdn_UserException('Bad Request', 400);
}

$discussion = $this->DiscussionModel->getForeignID($foreignId);

if (!is_object($discussion)) {
throw notFoundException('Discussion');
}

$this->delete($discussion->DiscussionID);
}

/**
* Checks that a string appears to be in the format of a UUID.
*
Expand Down
7 changes: 3 additions & 4 deletions forum/src/apiextended/settings/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
}

// If running the forum on PHP cli server, the content type server variable
// is not set as CONTENT_TYPE like the API application expects, but as
// HTTP_CONTENT_TYPE.
// To remedy this, so we can handle API requests via the CLI server, we copy the
// value to CONTENT_TYPE when needed.
// is not set as CONTENT_TYPE like the API expects, but as HTTP_CONTENT_TYPE.
// To rememdy this so we can handle API requests via the CLI server we copy the
// value in this case
$apiExtendedRequest = Gdn::request();
$apiExtendedContentType = $apiExtendedRequest->getValueFrom('server', 'HTTP_CONTENT_TYPE');

Expand Down
5 changes: 5 additions & 0 deletions forum/src/apiextended/settings/class.hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function DiscussionsAPI_register_handler($sender)
'Page' => val('Page', $data)
]
]);

$sender::delete('/foreign/[*:foreignID]', [
'controller' => 'ApiDiscussion',
'method' => 'deletebyforeignid'
]);
}

/**
Expand Down

0 comments on commit 3310fff

Please sign in to comment.