Items Api and Repository
Endpoint DELETE is working; Method delete is working; A route was changed in Items Controller.
This commit is contained in:
parent
1d53d99bb3
commit
ecf8a171d3
|
@ -30,8 +30,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/collection/(?P<collection_id>[\d]+)',
|
||||
$this->namespace, '/' . $this->rest_base . '/collection/(?P<collection_id>[\d]+)',
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
|
@ -48,8 +47,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
'schema' => array($this, 'get_public_item_schema'),
|
||||
));
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/collection/(?P<collection_id>[\d]+)/(?P<item_id>[\d]+)',
|
||||
$this->namespace, '/' . $this->rest_base . '/(?P<item_id>[\d]+)',
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
|
@ -97,6 +95,21 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$item_id = $request['item_id'];
|
||||
|
||||
$item = $this->items_repository->fetch($item_id);
|
||||
|
||||
$response = $this->prepare_item_for_response($item, $request);
|
||||
|
||||
return new WP_REST_Response($response, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
|
@ -122,7 +135,6 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
* @param $collection_id
|
||||
*
|
||||
* @return object|Entities\Item|WP_Error
|
||||
*/
|
||||
|
@ -147,15 +159,15 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
$collection_id = $request['collection_id'];
|
||||
$item = json_decode($request->get_body(), true);
|
||||
|
||||
$item_prepared = $this->prepare_item_for_database([$item, $collection_id]);
|
||||
$prepared_item = $this->prepare_item_for_database([$item, $collection_id]);
|
||||
|
||||
if($item_prepared->validate()){
|
||||
$item = $this->items_repository->insert($item_prepared);
|
||||
if($prepared_item->validate()){
|
||||
$item = $this->items_repository->insert($prepared_item);
|
||||
|
||||
return new WP_REST_Response($item->__toJSON(), 201);
|
||||
}
|
||||
|
||||
return new WP_REST_Response($item_prepared->get_errors(), 400);
|
||||
return new WP_REST_Response($prepared_item->get_errors(), 400);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -167,12 +179,31 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
return parent::delete_item( $request ); // TODO: Change the autogenerated stub
|
||||
$item_id = $request['item_id'];
|
||||
$is_permanently = json_decode($request->get_body(), true);
|
||||
|
||||
$args = [$item_id, $is_permanently];
|
||||
|
||||
$item = $this->items_repository->delete($args);
|
||||
|
||||
$prepared_item = $this->prepare_item_for_response($item, $request);
|
||||
|
||||
return new WP_REST_Response($prepared_item, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
return parent::delete_item_permissions_check( $request ); // TODO: Change the autogenerated stub
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -207,8 +207,12 @@ class Items extends Repository {
|
|||
|
||||
}
|
||||
|
||||
public function delete($object){
|
||||
public function delete($args){
|
||||
if($args[1]['is_permanently'] === true){
|
||||
return new Entities\Item(wp_delete_post($args[0], $args[1]['is_permanently']));
|
||||
}
|
||||
|
||||
return new Entities\Item(wp_trash_post($args[0]));
|
||||
}
|
||||
|
||||
}
|
|
@ -95,22 +95,21 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
|
|||
|
||||
$request = new \WP_REST_Request(
|
||||
'DELETE',
|
||||
$this->namespaced_route . '/items/collection/' . $collection->get_id() . '/' . $item1->get_id()
|
||||
$this->namespaced_route . '/items/' . $item1->get_id()
|
||||
);
|
||||
$request->set_body($delete_permanently);
|
||||
|
||||
$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());
|
||||
|
||||
$data = json_decode($response->get_data(), true);
|
||||
|
||||
$this->assertEquals('trash', $data['status']);
|
||||
$this->assertEquals($item1->get_title(), $data['title']);
|
||||
|
||||
$post_meta = get_post_meta($item1->get_id(), '_wp_trash_meta_status', true);
|
||||
|
||||
$this->assertNotEmpty($post_meta);
|
||||
|
||||
#######################################################################################
|
||||
|
||||
|
@ -129,22 +128,21 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
|
|||
|
||||
$request = new \WP_REST_Request(
|
||||
'DELETE',
|
||||
$this->namespaced_route . '/items/collection/' . $collection->get_id() . '/' . $item2->get_id()
|
||||
$this->namespaced_route . '/items/' . $item2->get_id()
|
||||
);
|
||||
$request->set_body($delete_permanently);
|
||||
|
||||
$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());
|
||||
|
||||
$data = json_decode($response->get_data(), true);
|
||||
|
||||
$this->assertTrue($data);
|
||||
$this->assertEquals($item2->get_title(), $data['title']);
|
||||
|
||||
$no_post = get_post($item2->get_id());
|
||||
|
||||
$this->assertNull($no_post);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue