Estrutura diferente

This commit is contained in:
weryques 2017-11-09 14:06:49 -02:00
parent 0f57f52a06
commit de092fa4ff
8 changed files with 306 additions and 1 deletions

View File

@ -0,0 +1,25 @@
<?php
require_once(realpath(dirname(__FILE__)) .'/../tainacan-helpers/class-collection-helper.php');
class TainacanAPICollection{
private $collection_helper;
function __construct(){
$this->collection_helper = new CollectionHelper();
}
public function insert_collection($name, $description){
return $this->collection_helper->insert_collection($name, $description);
}
public function get_collections($args){
return $this->collection_helper->get_collections($args);
}
public function get_collection($id){
return $this->collection_helper->get_collection($id);
}
}
?>

View File

@ -0,0 +1,26 @@
<?php
require_once('class-tainacan-api-collection.php');
class Tainacan_API extends WP_REST_Controller {
private $collection_API;
function __construct(){
$this->collection_API = new TainacanAPICollection();
}
public function insert_collection($name, $description){
return $this->collection_API->insert_collection($name, $description);
}
public function get_collections($args){
return $this->collection_API->get_collections($args);
}
public function get_collection($id){
return $this->collection_API->get_collection($id);
}
}
?>

View File

@ -0,0 +1,97 @@
<?php
require_once(realpath(dirname(__FILE__)) .'/../tainacan-dao/class-tainacan-dao.php');
class CollectionDAO implements TainacanDAO{
public function tainacan_insert($collection){
$mapped_atributes = $collection->get_mapped_atributes();
$db_identifier = Collection::DB_IDENTIFIER_META;
try {
$wp_post = new WP_Post($collection);
$wp_post->post_title = $collection->get_name();
$wp_post->post_content = $collection->get_description();
$id = wp_insert_post($wp_post);
foreach($mapped_atributes as $atribute => $value){
if($atribute == 'meta'){
update_post_meta($id, $atribute, $value);
}
}
if (!get_post_meta($id, $db_identifier, true)) {
$post = get_post($id);
add_post_meta($id, $db_identifier, $post->post_title);
}
}catch(Error $error){
var_dump($error);
return false;
}
return $id;
}
public function tainacan_register_post_type() {
$labels = array(
'name' => 'Collections',
'singular_name' => 'Collections',
'add_new' => 'Adicionar Novo',
'add_new_item' =>'Adicionar Collections',
'edit_item' => 'Editar',
'new_item' => 'Novo Collections',
'view_item' => 'Visualizar',
'search_items' => 'Pesquisar',
'not_found' => 'Nenhum ticket encontrado',
'not_found_in_trash' => 'Nenhum Collections encontrado na lixeira',
'parent_item_colon' => 'Collections acima:',
'menu_name' => 'Collections'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
//'supports' => array('title'),
//'taxonomies' => array(self::TAXONOMY),
'public' => true,
'show_ui' => tnc_enable_dev_wp_interface(),
'show_in_menu' => tnc_enable_dev_wp_interface(),
//'menu_position' => 5,
//'show_in_nav_menus' => false,
'publicly_queryable' => true,
'exclude_from_search' => true,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
);
register_post_type(Collection::POSTTYPE, $args);
}
public function tainacan_select($args){
if(is_numeric($args)){
$wp_post = get_post($args);
return Collection::__construct_with_wp_post($wp_post);
}
else{
$wp_posts = get_posts($args);
$posts_as_objects = [];
foreach ($wp_posts as $wp_post) {
$posts_as_objects[] = Collection::__construct_with_wp_post($wp_post);
}
return $posts_as_objects;
}
}
public function tainacan_delete($object){
#TODO: Implement
}
}
?>

View File

@ -0,0 +1,10 @@
<?php
interface TainacanDAO {
public function tainacan_insert($object);
public function tainacan_delete($object);
public function tainacan_select($object);
}
?>

View File

@ -0,0 +1,37 @@
<?php
require_once(realpath(dirname(__FILE__)) .'/../tainacan-models/class-collection.php');
class CollectionHelper {
private $collection;
public function __construct(){
$this->collection = new Collection();
}
public function get_collection($id){
return $this->collection->get_collection($id);
}
public function get_collections($args = []){
// $args = array_merge([
// 'post_type' => Collection::POSTTYPE,
// 'posts_per_page' => -1,
// 'post_status' => 'publish',
// ], $args);
return $this->collection->get_collections($args);
}
public function insert_collection($name, $description){
$collection = new Collection();
$collection->set_name($name);
$collection->set_description($description);
return $collection->insert_collection($collection);
}
}
?>

View File

@ -0,0 +1,79 @@
<?php
require_once(realpath(dirname(__FILE__)) . '/../tainacan-dao/class-collection-dao.php');
class Collection {
const POSTTYPE = 'tainacan_collection';
const DB_IDENTIFIER_META = '_db_identifier';
private $name;
private $description;
private $collection_DAO;
private $mapped_atributes = [
'ID' => 'ID',
'name' => 'post_title',
'order' => 'menu_order',
'parent' => 'parent',
'description' => 'post_content',
'itens_per_page' => 'meta'
];
/*
* Default constructor
*/
function __construct(){
$this->collection_DAO = new CollectionDAO();
if(!has_action( 'init', 'tainacan_register_post_type' )){
add_action( 'init', array($this->collection_DAO, 'tainacan_register_post_type'));
}
}
/*
* Constructor that receive a WP_Post object
* @param $wp_post
* @type WP_Post
*/
public static function __construct_with_wp_post($wp_post){
$instance = new self();
$instance->set_name($wp_post->post_title);
$instance->set_description($wp_post->post_content);
return $instance;
}
function set_name($name){
$this->name = $name;
}
public function get_name(){
return $this->name;
}
public function set_description($description){
$this->description = $description;
}
public function get_description(){
return $this->description;
}
public function get_mapped_atributes(){
return $this->mapped_atributes;
}
public function get_collections($args){
return $this->collection_DAO->tainacan_select($args);
}
public function get_collection($id){
return $this->collection_DAO->tainacan_select($id);
}
public function insert_collection($collection){
return $this->collection_DAO->tainacan_insert($collection);
}
}
?>

View File

@ -14,6 +14,9 @@ include('classes/Entity.php');
include('classes/Entities/Collection.php');
include('classes/Entities/Metadata.php');
require_once('tainacan-api-controllers/class-tainacan-api.php');
$tainacan_api = new Tainacan_API();
/**
*
*

View File

@ -35,4 +35,32 @@ class TestCollections extends WP_UnitTestCase {
}
function test_insert_and_get(){
$controller = new Tainacan_API();
$name = 'Code';
$description = 'Show me the code!';
$name2 = 'Again';
$description2 = 'Showing the code again!';
$id = $controller->insert_collection($name, $description);
$id2 = $controller->insert_collection($name2, $description2);
$this->assertNotFalse($id);
$this->assertNotFalse($id2);
// Getting one collection
$collection = $controller->get_collection($id);
$this->assertEquals($collection->get_name(), $name);
$this->assertEquals($collection->get_description(), $description);
// Getting all collections
$collections = $controller->get_collections(array('include' => array($id, $id2)));
$this->assertEquals($collections[0]->get_name(), $name2);
$this->assertEquals($collections[1]->get_name(), $name);
}
}