Collections API and Repository

Endpoint DELETE is working;
Method delete is implemented;
This commit is contained in:
weryques 2017-12-08 13:09:36 -02:00
parent ecf8a171d3
commit 3b3cfda7fb
3 changed files with 26 additions and 22 deletions

View File

@ -209,9 +209,15 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
*/ */
public function delete_item( $request ) { public function delete_item( $request ) {
$collection_id = $request['collection_id']; $collection_id = $request['collection_id'];
$this->collections_repository->delete($collection_id); $is_permanently = json_decode($request->get_body(), true);
return 'Não implementado'; $args = [$collection_id, $is_permanently];
$collection = $this->collections_repository->delete($args);
$prepared_collection = $this->prepare_item_for_response($collection, $request);
return new WP_REST_Response($prepared_collection, 200);
} }
/** /**
@ -222,11 +228,7 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
* @return bool|WP_Error * @return bool|WP_Error
*/ */
public function delete_item_permissions_check( $request ) { public function delete_item_permissions_check( $request ) {
if(current_user_can('delete_posts')){ return true;
return true;
}
return false;
} }
/** /**

View File

@ -154,7 +154,7 @@ class Collections extends Repository {
} }
/** /**
* @param Tainacan\Entities\Collection $collection * @param \Tainacan\Entities\Collection $collection
* @return \Tainacan\Entities\Collection * @return \Tainacan\Entities\Collection
* {@inheritDoc} * {@inheritDoc}
* @see \Tainacan\Repositories\Repository::insert() * @see \Tainacan\Repositories\Repository::insert()
@ -169,8 +169,12 @@ class Collections extends Repository {
} }
public function delete($object){ public function delete($args){
if($args[1]['is_permanently'] === true){
return new Entities\Collection(wp_delete_post($args[0], $args[1]['is_permanently']));
}
return new Entities\Collection(wp_trash_post($args[0]));
} }
/** /**

View File

@ -97,16 +97,15 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
// To be removed
if($response->get_status() != 200){
$this->markTestSkipped('Need method delete implemented.');
}
$this->assertEquals(200, $response->get_status()); $this->assertEquals(200, $response->get_status());
$data = $response->get_data(); $data = json_decode($response->get_data(), true);
$this->assertTrue($data); $this->assertEquals($collection1->get_id(), $data['id']);
$no_post = get_post($collection1->get_id());
$this->assertNull($no_post);
####################################################################################### #######################################################################################
@ -123,16 +122,15 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
// To be removed
if($response->get_status() != 200){
$this->markTestSkipped('Need method delete implemented.');
}
$this->assertEquals(200, $response->get_status()); $this->assertEquals(200, $response->get_status());
$data = json_decode($response->get_data(), true); $data = json_decode($response->get_data(), true);
$this->assertEquals('trash', $data['status']); $this->assertEquals($collection2->get_id(), $data['id']);
$post_meta = get_post_meta($collection2->get_id(), '_wp_trash_meta_status', true);
$this->assertNotEmpty($post_meta);
} }
} }