Skip to content

Commit ac95766

Browse files
committed
Added new API
1 parent b392858 commit ac95766

File tree

6 files changed

+137
-8
lines changed

6 files changed

+137
-8
lines changed

Vuefront/Vuefront/ApiGraphql/model/blog/category.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,17 @@ public function getTotalCategories($data = array())
9191

9292
return $result['total'];
9393
}
94+
95+
public function getCategoryByPostId($post_id)
96+
{
97+
$sql = "SELECT c.category_id
98+
FROM `".$this->db->getTableName('magefan_blog_category')."` c
99+
left join `".$this->db->getTableName('magefan_blog_category_store')."` cs on c.category_id = cs.category_id
100+
left join `".$this->db->getTableName('magefan_blog_post_category')."` pc on pc.category_id = c.category_id
101+
where c.is_active = '1' AND pc.post_id = '".$post_id."'";
102+
103+
$results = $this->db->fetchAll($sql);
104+
105+
return $results;
106+
}
94107
}

Vuefront/Vuefront/ApiGraphql/model/blog/post.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ public function getPosts($data = array())
7171

7272
public function getTotalPosts($data = array())
7373
{
74-
global $wpdb;
75-
7674
$sql = "SELECT count(*) as total
7775
FROM `".$this->db->getTableName('magefan_blog_post')."` p";
7876

@@ -94,4 +92,28 @@ public function getTotalPosts($data = array())
9492

9593
return $result['total'];
9694
}
95+
96+
public function getNextPost($post_id)
97+
{
98+
$sql = "SELECT p.*
99+
FROM `".$this->db->getTableName('magefan_blog_post')."` p
100+
WHERE p.post_id > '".$post_id."'
101+
ORDER BY p.publish_time ASC";
102+
103+
$result = $this->db->fetchOne($sql);
104+
105+
return $result;
106+
}
107+
108+
public function getPrevPost($post_id)
109+
{
110+
$sql = "SELECT p.*
111+
FROM `".$this->db->getTableName('magefan_blog_post')."` p
112+
WHERE p.post_id < '".$post_id."'
113+
ORDER BY p.post_id DESC";
114+
115+
$result = $this->db->fetchOne($sql);
116+
117+
return $result;
118+
}
97119
}

Vuefront/Vuefront/ApiGraphql/resolver/blog/post.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ class ResolverBlogPost extends Resolver
66
{
77
private $blog = false;
88

9-
public function __construct($registry) {
9+
public function __construct($registry)
10+
{
1011
parent::__construct($registry);
1112
$moduleManager = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Framework\Module\Manager');
1213

@@ -26,16 +27,42 @@ public function get($args)
2627
$thumb = '';
2728
$thumbLazy = '';
2829
}
29-
30+
31+
$date_format = '%A %d %B %Y';
3032

3133
return array(
3234
'id' => $post['post_id'],
35+
'name' => $post['title'],
3336
'title' => $post['title'],
3437
'shortDescription' => $post['short_content'],
3538
'description' => $post['content'],
3639
'keyword' => $post['identifier'],
3740
'image' => $thumb,
3841
'imageLazy' => $thumbLazy,
42+
'rating' => null,
43+
'datePublished' => iconv(
44+
mb_detect_encoding(strftime($date_format, strtotime($post['publish_time']))),
45+
"utf-8//IGNORE",
46+
strftime($date_format, strtotime($post['publish_time']))
47+
),
48+
'categories' => function ($root, $args) {
49+
return $this->load->resolver('blog/post/categories', array(
50+
'parent' => $root,
51+
'args' => $args
52+
));
53+
},
54+
'next' => function ($root, $args) {
55+
return $this->load->resolver('blog/post/next', array(
56+
'parent' => $root,
57+
'args' => $args
58+
));
59+
},
60+
'prev' => function ($root, $args) {
61+
return $this->load->resolver('blog/post/prev', array(
62+
'parent' => $root,
63+
'args' => $args
64+
));
65+
},
3966
'reviews' => function ($root, $args) {
4067
return $this->load->resolver('blog/review/get', array(
4168
'parent' => $root,
@@ -88,4 +115,56 @@ public function getList($args)
88115
);
89116
}
90117
}
118+
public function categories($args)
119+
{
120+
if ($this->blog) {
121+
$this->load->model('blog/category');
122+
$post = $args['parent'];
123+
124+
$result = $this->model_blog_category->getCategoryByPostId($post['id']);
125+
$categories = array();
126+
foreach ($result as $category) {
127+
$categories[] =$this->load->resolver(
128+
'blog/category/get',
129+
array('id' => $category['category_id'])
130+
);
131+
}
132+
return $categories;
133+
} else {
134+
return array();
135+
}
136+
}
137+
138+
139+
public function prev($args)
140+
{
141+
if ($this->blog) {
142+
$this->load->model('blog/post');
143+
$post = $args['parent'];
144+
$prev_post_info = $this->model_blog_post->getPrevPost($post['id']);
145+
if (empty($prev_post_info)) {
146+
return null;
147+
}
148+
return $this->get(array('id' => $prev_post_info['post_id']));
149+
} else {
150+
return array();
151+
}
152+
}
153+
154+
public function next($args)
155+
{
156+
if ($this->blog) {
157+
$this->load->model('blog/post');
158+
$post = $args['parent'];
159+
160+
161+
$next_post_info = $this->model_blog_post->getNextPost($post['id']);
162+
if (empty($next_post_info)) {
163+
return null;
164+
}
165+
return $this->get(array('id' => $next_post_info['post_id']));
166+
} else {
167+
return array();
168+
}
169+
}
91170
}

Vuefront/Vuefront/ApiGraphql/resolver/common/page.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function get($args) {
1111
return array(
1212
'id' => $page_info['page_id'],
1313
'title' => $page_info['title'],
14+
'name' => $page_info['title'],
1415
'description' => $page_info['content'],
1516
'sort_order' => (int)$page_info['sort_order'],
1617
'keyword' => $page_info['identifier']

Vuefront/Vuefront/ApiGraphql/resolver/store/currency.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function get()
2323

2424
$currencies[] = array(
2525
'title' => $allCurrencies[$code][1] ?: $code,
26+
'name' => $allCurrencies[$code][1] ?: $code,
2627
'code' => $code,
2728
'symbol_left' => $objectManager->create('Magento\Framework\Locale\CurrencyInterface')->getCurrency($code)->getSymbol(),
2829
'symbol_right' => '',

Vuefront/Vuefront/ApiGraphql/schema.graphql

100755100644
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ type CategoryResult {
7676
}
7777

7878
type Currency {
79-
title: String
79+
title: String @deprecated(reason: "Changed to name!")
80+
name: String
8081
code: String
8182
symbol_left: String
8283
symbol_right: String
@@ -120,7 +121,8 @@ type OptionValue {
120121

121122
type Page {
122123
id: ID
123-
title: String
124+
title: String @deprecated(reason: "Changed to name!")
125+
name: String
124126
description: String
125127
keyword: String
126128
sort_order: Int
@@ -137,15 +139,26 @@ type PageResult {
137139
totalElements: Int
138140
}
139141

142+
type postReviewResult {
143+
content: [postReview]
144+
totalElements: Int
145+
}
146+
140147
type Post {
141148
id: ID
142-
title: String
149+
title: String @deprecated(reason: "Changed to name!")
150+
name: String
143151
shortDescription: String
144152
description: String
145153
image: String
146154
imageLazy: String
147155
keyword: String
148-
reviews: [postReview]
156+
rating: Float
157+
reviews: postReviewResult
158+
categories: [categoryBlog]
159+
datePublished: String
160+
next: Post
161+
prev: Post
149162
}
150163

151164
type PostResult {

0 commit comments

Comments
 (0)