Skip to content

Commit be4b4dc

Browse files
committed
feat: tags module do not use proxy api
1 parent 08262b9 commit be4b4dc

File tree

4 files changed

+132
-6
lines changed

4 files changed

+132
-6
lines changed

config/routes.php

+20
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,26 @@
275275
['controller' => 'Tags', 'action' => 'index'],
276276
['_name' => 'tags:index']
277277
);
278+
$routes->connect(
279+
'/tags/delete/{id}',
280+
['controller' => 'Tags', 'action' => 'delete'],
281+
['pass' => ['id'], '_name' => 'tags:delete']
282+
);
283+
$routes->connect(
284+
'/tags/create',
285+
['controller' => 'Tags', 'action' => 'create'],
286+
['_name' => 'tags:create']
287+
);
288+
$routes->connect(
289+
'/tags/patch/{id}',
290+
['controller' => 'Tags', 'action' => 'patch'],
291+
['pass' => ['id'], '_name' => 'tags:patch']
292+
);
293+
$routes->connect(
294+
'/tags/search',
295+
['controller' => 'Tags', 'action' => 'search'],
296+
['_name' => 'tags:search']
297+
);
278298

279299
// view resource by id / uname
280300
$routes->connect(

resources/js/app/components/tag-form/tag-form.vue

+5-5
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export default {
165165
headers,
166166
method: 'GET',
167167
};
168-
const response = await fetch(`${BEDITA.base}/api/model/tags?filter[name]=${this.name}`, options);
168+
const response = await fetch(`${BEDITA.base}/tags/search?filter[name]=${this.name}`, options);
169169
const responseJson = await response.json();
170170
171171
return responseJson?.data?.length > 0;
@@ -212,11 +212,11 @@ export default {
212212
payload.data.id = this.obj.id;
213213
options.body = JSON.stringify(payload);
214214
options.method = 'PATCH'
215-
response = await fetch(`${BEDITA.base}/api/model/tags/${this.obj.id}`, options);
215+
response = await fetch(`${BEDITA.base}/tags/patch/${this.obj.id}`, options);
216216
} else {
217217
options.body = JSON.stringify(payload);
218218
options.method = 'POST'
219-
response = await fetch(`${BEDITA.base}/api/model/tags`, options);
219+
response = await fetch(`${BEDITA.base}/tags/create`, options);
220220
}
221221
if (response.status === 200) {
222222
if (this.redir) {
@@ -253,14 +253,14 @@ export default {
253253
const prefix = t`Error on deleting tag`;
254254
try {
255255
const options = {
256-
method: 'DELETE',
256+
method: 'POST',
257257
credentials: 'same-origin',
258258
headers: {
259259
'accept': 'application/json',
260260
'X-CSRF-Token': BEDITA.csrfToken,
261261
},
262262
};
263-
const response = await fetch(`${BEDITA.base}/api/model/tags/${this.obj.id}`, options);
263+
const response = await fetch(`${BEDITA.base}/tags/delete/${this.obj.id}`, options);
264264
if (response.status === 200) {
265265
this.$el.remove(this.$el);
266266
this.dialog?.hide();

resources/js/app/components/tag-picker/tag-picker.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export default {
153153
return callback(null, []);
154154
}
155155
156-
const res = await fetch(`${BEDITA.base}/api/model/tags?filter[query]=${searchQuery}&filter[enabled]=1&page_size=30`, API_OPTIONS);
156+
const res = await fetch(`${BEDITA.base}/tags/search?filter[query]=${searchQuery}&filter[enabled]=1&page_size=30`, API_OPTIONS);
157157
const json = await res.json();
158158
const tags = [...(json.data || [])];
159159

src/Controller/TagsController.php

+106
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
namespace App\Controller;
55

66
use App\Controller\Model\TagsController as ModelTagsController;
7+
use BEdita\SDK\BEditaClientException;
78
use Cake\Event\EventInterface;
89
use Cake\Http\Response;
10+
use Cake\Utility\Hash;
911

1012
/**
1113
* Tags Controller
@@ -21,6 +23,7 @@ public function initialize(): void
2123
{
2224
parent::initialize();
2325
$this->loadComponent('ProjectConfiguration');
26+
$this->Security->setConfig('unlockedActions', ['create', 'patch', 'delete']);
2427
}
2528

2629
/**
@@ -37,6 +40,109 @@ public function index(): ?Response
3740
return null;
3841
}
3942

43+
/**
44+
* Create new tag (ajax).
45+
*
46+
* @return \Cake\Http\Response|null
47+
*/
48+
public function create(): ?Response
49+
{
50+
$this->getRequest()->allowMethod(['post']);
51+
$this->viewBuilder()->setClassName('Json');
52+
$response = $error = null;
53+
try {
54+
$body = (array)$this->getRequest()->getData();
55+
$response = $this->apiClient->post('/model/tags', json_encode($body));
56+
} catch (BEditaClientException $e) {
57+
$error = $e->getMessage();
58+
$this->log($error, 'error');
59+
$this->set('error', $error);
60+
}
61+
$this->set('response', $response);
62+
$this->set('error', $error);
63+
$this->setSerialize(['response', 'error']);
64+
65+
return null;
66+
}
67+
68+
/**
69+
* Delete single tag (ajax).
70+
*
71+
* @param string $id Tag ID.
72+
* @return \Cake\Http\Response|null
73+
*/
74+
public function delete(string $id): ?Response
75+
{
76+
$this->getRequest()->allowMethod(['post']);
77+
$this->viewBuilder()->setClassName('Json');
78+
$response = $error = null;
79+
try {
80+
$response = $this->apiClient->delete(sprintf('/model/tags/%s', $id));
81+
} catch (BEditaClientException $e) {
82+
$error = $e->getMessage();
83+
$this->log($error, 'error');
84+
$this->set('error', $error);
85+
}
86+
$this->set('response', $response);
87+
$this->set('error', $error);
88+
$this->setSerialize(['response', 'error']);
89+
90+
return null;
91+
}
92+
93+
/**
94+
* Save tag (ajax).
95+
*
96+
* @param string $id Tag ID.
97+
* @return \Cake\Http\Response|null
98+
*/
99+
public function patch(string $id): ?Response
100+
{
101+
$this->getRequest()->allowMethod(['patch']);
102+
$this->viewBuilder()->setClassName('Json');
103+
$response = $error = null;
104+
try {
105+
$body = (array)$this->getRequest()->getData();
106+
$this->apiClient->patch(sprintf('/model/tags/%s', $id), json_encode($body));
107+
$response = 'ok';
108+
} catch (BEditaClientException $e) {
109+
$error = $e->getMessage();
110+
$this->log($error, 'error');
111+
$this->set('error', $error);
112+
}
113+
$this->set('response', $response);
114+
$this->set('error', $error);
115+
$this->setSerialize(['response', 'error']);
116+
117+
return null;
118+
}
119+
120+
/**
121+
* Search tags (ajax)
122+
*
123+
* @return \Cake\Http\Response|null
124+
*/
125+
public function search(): ?Response
126+
{
127+
$this->getRequest()->allowMethod(['get']);
128+
$this->viewBuilder()->setClassName('Json');
129+
$data = $error = null;
130+
try {
131+
$query = $this->getRequest()->getQueryParams();
132+
$response = $this->apiClient->get('/model/tags', $query);
133+
$data = (array)Hash::get($response, 'data');
134+
} catch (BEditaClientException $e) {
135+
$error = $e->getMessage();
136+
$this->log($error, 'error');
137+
$this->set('error', $error);
138+
}
139+
$this->set('data', $data);
140+
$this->set('error', $error);
141+
$this->setSerialize(['data', 'error']);
142+
143+
return null;
144+
}
145+
40146
/**
41147
* @inheritDoc
42148
*/

0 commit comments

Comments
 (0)