Skip to content

Commit ddedfd7

Browse files
committed
Added product crud
1 parent 869ff81 commit ddedfd7

70 files changed

Lines changed: 4778 additions & 522 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

includes/Api/Types/Product.php

Lines changed: 169 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Therakib7\WpPluginKit\Api\Types;
33

44
use Therakib7\WpPluginKit\Abstracts\RestApi;
5+
use Therakib7\WpPluginKit\Models\Product as ModelsProduct;
56

67
/**
78
* API Product class.
@@ -44,6 +45,16 @@ public function routes() {
4445
],
4546
]
4647
);
48+
49+
register_rest_route(
50+
$this->namespace,
51+
'/' . $this->base,
52+
[
53+
'methods' => 'POST',
54+
'callback' => [ $this, 'create' ],
55+
'permission_callback' => [ $this, 'permission' ],
56+
]
57+
);
4758
}
4859

4960
/**
@@ -55,14 +66,169 @@ public function routes() {
5566
*
5667
* @return WP_Error|WP_REST_Response
5768
*/
58-
public function get( $req ) {
59-
$param = $req->get_params();
69+
public function get( $request ) {
70+
$args = [];
71+
$data = [];
72+
$params = $this->get_collection_params();
73+
74+
foreach ( $params as $key => $value ) {
75+
if ( isset( $request[ $key ] ) ) {
76+
$args[ $key ] = $request[ $key ];
77+
}
78+
}
79+
80+
$product = new ModelsProduct();
81+
82+
$jobs = $product->getall( $args );
83+
foreach ( $jobs as $job ) {
84+
$response = $this->prepare_item_for_response( $job, $request );
85+
$data[] = $this->prepare_response_for_collection( $response );
86+
}
87+
88+
$args['count'] = 1;
89+
$total = $product->getall( $args );
90+
$max_pages = ceil( $total / (int) $args['limit'] );
91+
$response = rest_ensure_response( $data );
92+
93+
$response->header( 'X-WP-Total', (int) $total );
94+
$response->header( 'X-WP-TotalPages', (int) $max_pages );
95+
96+
return $response;
97+
/* $param = $req->get_params();
6098
6199
return new \WP_REST_Response(
62100
[
63101
'success' => true,
64102
'data' => [],
65103
], 200
66-
);
104+
); */
105+
}
106+
107+
/**
108+
* Add product
109+
*
110+
* @since 0.1.0
111+
*
112+
* @param \WP_REST_Request $req Request object.
113+
*
114+
* @return WP_Error|WP_REST_Response
115+
*/
116+
public function create( $req ) {
117+
$param = $req->get_params();
118+
$wp_err = new \WP_Error();
119+
120+
$product = new ModelsProduct();
121+
$product->add( $param );
122+
123+
return;
124+
$title = isset( $param['title'] ) ? sanitize_text_field( $param['title'] ) : '';
125+
126+
if ( empty( $title ) ) {
127+
$wp_err->add(
128+
'field',
129+
esc_html__( 'Title is missing', 'wp-plugin-kit' )
130+
);
131+
}
132+
133+
if ( $wp_err->get_error_messages() ) {
134+
return new \WP_REST_Response(
135+
[
136+
'success' => false,
137+
'data' => $wp_err->get_error_messages(),
138+
],
139+
200
140+
);
141+
} else {
142+
143+
//Save data
144+
145+
if ( $wp_err->get_error_messages() ) {
146+
return new \WP_REST_Response(
147+
[
148+
'success' => false,
149+
'data' => $wp_err->get_error_messages(),
150+
],
151+
200
152+
);
153+
} else {
154+
155+
return new \WP_REST_Response(
156+
[
157+
'success' => true,
158+
],
159+
200
160+
);
161+
}
162+
}
163+
}
164+
165+
/**
166+
* Prepares the item for the REST response.
167+
*
168+
* @since 0.3.0
169+
*
170+
* @param Job $item WordPress representation of the item
171+
* @param WP_REST_Request $request request object
172+
*
173+
* @return WP_Error|WP_REST_Response
174+
*/
175+
public function prepare_item_for_response( $item, $request ) {
176+
$data = [];
177+
178+
$data = ModelsProduct::to_array( $item );
179+
180+
$data = $this->prepare_response_for_collection( $data );
181+
182+
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
183+
$data = $this->filter_response_by_context( $data, $context );
184+
185+
$response = rest_ensure_response( $data );
186+
$response->add_links( $this->prepare_links( $item ) );
187+
188+
return $response;
189+
}
190+
191+
/**
192+
* Prepares links for the request.
193+
*
194+
* @since 0.3.0
195+
*
196+
* @param WP_Post $post post object
197+
*
198+
* @return array links for the given data.
199+
*/
200+
protected function prepare_links( $item ): array {
201+
$base = sprintf( '%s/%s%s', $this->namespace, $this->rest_base, $this->base );
202+
203+
$id = is_object( $item ) ? $item->id : $item['id'];
204+
205+
$links = [
206+
'self' => [
207+
'href' => rest_url( trailingslashit( $base ) . $id ),
208+
],
209+
'collection' => [
210+
'href' => rest_url( $base ),
211+
],
212+
];
213+
214+
return $links;
215+
}
216+
217+
/**
218+
* Retrieves the query params for collections.
219+
*
220+
* @since 0.3.0
221+
*
222+
* @return array
223+
*/
224+
public function get_collection_params(): array {
225+
$params = parent::get_collection_params();
226+
227+
$params['limit']['default'] = 10;
228+
$params['search']['default'] = '';
229+
$params['orderby']['default'] = 'id';
230+
$params['order']['default'] = 'DESC';
231+
232+
return $params;
67233
}
68234
}

includes/Database/Migrations/Tables/Types/Products.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,23 @@ public static function migrate() {
3939

4040
$schema_products = "CREATE TABLE IF NOT EXISTS `{$table_name_with_prefix}` (
4141
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
42+
`is_active` tinyint(1) NOT NULL DEFAULT 1,
43+
`created_by` bigint(20) unsigned NOT NULL,
4244
`title` varchar(255) NOT NULL,
4345
`slug` varchar(255) NOT NULL,
4446
`description` text DEFAULT NULL,
45-
`type_id` int(10) unsigned NULL,
46-
`is_active` tinyint(1) NOT NULL DEFAULT 1,
47-
`created_by` bigint(20) unsigned NOT NULL,
47+
`category_id` tinyint(1) DEFAULT NULL,
48+
`price` decimal(10,2) DEFAULT NULL,
49+
`currency` varchar(10) DEFAULT NULL,
50+
`image_id` tinyint(1) DEFAULT NULL,
51+
`gallery_ids` text DEFAULT NULL,
4852
`updated_by` bigint(20) unsigned NULL,
4953
`deleted_by` bigint(20) unsigned NULL,
5054
`created_at` datetime NOT NULL,
5155
`updated_at` datetime NULL,
5256
`deleted_at` datetime NULL,
5357
PRIMARY KEY (`id`),
54-
KEY `type_id` (`type_id`),
58+
KEY `category_id` (`category_id`),
5559
KEY `created_by` (`created_by`),
5660
KEY `updated_by` (`updated_by`)
5761
) $charset_collate";

0 commit comments

Comments
 (0)