Skip to content

Commit 9998ff6

Browse files
committed
Add simple REST API
1 parent e803def commit 9998ff6

8 files changed

+294
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Api;
10+
11+
interface CategoryManagementInterface extends ManagementInterface
12+
{
13+
14+
}

Api/ManagementInterface.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Api;
10+
11+
interface ManagementInterface
12+
{
13+
/**
14+
* Create new item.
15+
*
16+
* @api
17+
* @param string $data.
18+
* @return string.
19+
*/
20+
public function create($data);
21+
22+
/**
23+
* Update item by id.
24+
*
25+
* @api
26+
* @param int $id.
27+
* @param string $data.
28+
* @return string.
29+
*/
30+
public function update($id, $data);
31+
32+
/**
33+
* Remove item by id.
34+
*
35+
* @api
36+
* @param int $id.
37+
* @return bool.
38+
*/
39+
public function delete($id);
40+
}

Api/PostManagementInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Api;
10+
11+
interface PostManagementInterface extends ManagementInterface
12+
{
13+
14+
}

Model/AbstractManagement.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Model;
10+
11+
use Magefan\Blog\Api\ManagementInterface;
12+
13+
/**
14+
* Abstract management model
15+
*/
16+
abstract class AbstractManagement implements ManagementInterface
17+
{
18+
/**
19+
* @var Magento\Framework\Model\AbstractModel
20+
*/
21+
protected $_itemFactory;
22+
23+
/**
24+
* Create new item using data
25+
*
26+
* @param string $data
27+
* @return string || false
28+
*/
29+
public function create($data)
30+
{
31+
try {
32+
$data = json_decode($data, true);
33+
$item = $this->_itemFactory->create();
34+
$item->setData($data)->save();
35+
} catch (Exception $e) {
36+
return false;
37+
}
38+
39+
return json_encode($item->getData());
40+
}
41+
42+
/**
43+
* Update item using data
44+
*
45+
* @param int $id
46+
* @param string $data
47+
* @return string || false
48+
*/
49+
public function update($id, $data)
50+
{
51+
try {
52+
$item = $this->_itemFactory->create();
53+
$item->load($id);
54+
55+
if (!$item->getId()) {
56+
return false;
57+
}
58+
$data = json_decode($data, true);
59+
$item->addData($data)->save();
60+
} catch (Exception $e) {
61+
return false;
62+
}
63+
64+
return json_encode($item->getData());
65+
}
66+
67+
/**
68+
* Delete item by id
69+
*
70+
* @param int $id
71+
* @return bool
72+
*/
73+
public function delete($id)
74+
{
75+
try {
76+
$item = $this->_itemFactory->create();
77+
$item->load($id);
78+
if ($item->getId()) {
79+
$item->delete();
80+
return true;
81+
}
82+
return false;
83+
} catch (Exception $e) {
84+
return false;
85+
}
86+
}
87+
88+
}

Model/CategoryManagement.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Model;
10+
11+
/**
12+
* Category management model
13+
*/
14+
class CategoryManagement extends AbstractManagement
15+
{
16+
/**
17+
* @var \Magefan\Blog\Model\CategoryFactory
18+
*/
19+
protected $_itemFactory;
20+
21+
/**
22+
* Initialize dependencies.
23+
*
24+
* @param \Magefan\Blog\Model\CategoryFactory $categoryFactory
25+
*/
26+
public function __construct(
27+
\Magefan\Blog\Model\CategoryFactory $categoryFactory
28+
) {
29+
$this->_itemFactory = $categoryFactory;
30+
}
31+
32+
}

Model/PostManagement.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Model;
10+
11+
/**
12+
* Post management model
13+
*/
14+
class PostManagement extends AbstractManagement
15+
{
16+
/**
17+
* @var \Magefan\Blog\Model\PostFactory
18+
*/
19+
protected $_itemFactory;
20+
21+
/**
22+
* Initialize dependencies.
23+
*
24+
* @param \Magefan\Blog\Model\PostFactory $postFactory
25+
*/
26+
public function __construct(
27+
\Magefan\Blog\Model\PostFactory $postFactory
28+
) {
29+
$this->_itemFactory = $postFactory;
30+
}
31+
32+
}

etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
*/
99
-->
1010
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
11+
<preference for="Magefan\Blog\Api\PostManagementInterface" type="Magefan\Blog\Model\PostManagement" />
12+
<preference for="Magefan\Blog\Api\CategoryManagementInterface" type="Magefan\Blog\Model\CategoryManagement" />
1113
<preference for="Magento\Sitemap\Model\Sitemap" type="Magefan\Blog\Model\Sitemap" />
1214
</config>

etc/webapi.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
5+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
6+
*
7+
* Glory to Ukraine! Glory to the heroes!
8+
*/
9+
-->
10+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
11+
12+
<!-- Login Example to get Bearer: curl -X POST "http://mystore.com/index.php/rest/V1/integration/admin/token" \
13+
-H "Content-Type:application/json" \
14+
-d '{"username":"username", "password":"userpassword"}' -->
15+
16+
<!-- Example: curl -X POST "http://mystore.com/index.php/rest/V1/blog/post/create/" \
17+
-H "Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
18+
-H "Content-Type:application/json" \
19+
-d '{"data":"{\"param_1\":\"value_1\",\"param_2\":\"value_2\",\"param_n\":\"value_n\"}"}' -->
20+
<route url="/V1/blog/post/create" method="POST">
21+
<service class="Magefan\Blog\Api\PostManagementInterface" method="create"/>
22+
<resources>
23+
<resource ref="Magefan_Blog::post"/>
24+
</resources>
25+
</route>
26+
<!-- Example: curl -X POST "http://mystore.com/index.php/rest/V1/blog/post/update/postId/" \
27+
-H "Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
28+
-H "Content-Type:application/json" \
29+
-d '{"data":"{\"param_1\":\"new_value_1\",\"param_2\":\"new_value_2\",\"param_n\":\"new_value_n\"}"}' -->
30+
<route url="/V1/blog/post/update/:id/" method="POST">
31+
<service class="Magefan\Blog\Api\PostManagementInterface" method="update"/>
32+
<resources>
33+
<resource ref="Magefan_Blog::post"/>
34+
</resources>
35+
</route>
36+
<!-- Example: curl -X GET "http://mystore.com/m2_21/index.php/rest/V1/blog/post/delete/postId/" \
37+
-H "Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -->
38+
<route url="/V1/blog/post/delete/:id/" method="GET">
39+
<service class="Magefan\Blog\Api\PostManagementInterface" method="delete"/>
40+
<resources>
41+
<resource ref="Magefan_Blog::post"/>
42+
</resources>
43+
</route>
44+
<!-- Example: curl -X POST "http://mystore.com/index.php/rest/V1/blog/category/create/" \
45+
-H "Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
46+
-H "Content-Type:application/json" \
47+
-d '{"data":"{\"param_1\":\"value_1\",\"param_2\":\"value_2\",\"param_n\":\"value_n\"}"}' -->
48+
<route url="/V1/blog/category/create" method="POST">
49+
<service class="Magefan\Blog\Api\CategoryManagementInterface" method="create"/>
50+
<resources>
51+
<resource ref="Magefan_Blog::category"/>
52+
</resources>
53+
</route>
54+
<!-- Example: curl -X POST "http://mystore.com/index.php/rest/V1/blog/category/update/categoryId/" \
55+
-H "Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
56+
-H "Content-Type:application/json" \
57+
-d '{"data":"{\"param_1\":\"new_value_1\",\"param_2\":\"new_value_2\",\"param_n\":\"new_value_n\"}"}' -->
58+
<route url="/V1/blog/category/update/:id/" method="POST">
59+
<service class="Magefan\Blog\Api\CategoryManagementInterface" method="update"/>
60+
<resources>
61+
<resource ref="Magefan_Blog::category"/>
62+
</resources>
63+
</route>
64+
<!-- Example: curl -X GET "http://mystore.com/m2_21/index.php/rest/V1/blog/post/category/categoryId/" \
65+
-H "Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -->
66+
<route url="/V1/blog/category/delete/:id/" method="GET">
67+
<service class="Magefan\Blog\Api\CategoryManagementInterface" method="delete"/>
68+
<resources>
69+
<resource ref="Magefan_Blog::category"/>
70+
</resources>
71+
</route>
72+
</routes>

0 commit comments

Comments
 (0)