Skip to content

Commit e50554e

Browse files
authored
fix #1800 テーマフォルダAPI 一覧取得 (#1813)
1 parent 4370dd7 commit e50554e

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

plugins/bc-theme-file/src/Controller/Api/ThemeFoldersController.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use BaserCore\Annotation\Checked;
1616
use BaserCore\Annotation\UnitTest;
1717
use BaserCore\Controller\Api\BcApiController;
18+
use BaserCore\Error\BcFormFailedException;
1819
use BcThemeFile\Service\ThemeFoldersService;
1920
use BcThemeFile\Service\ThemeFoldersServiceInterface;
2021

@@ -76,10 +77,34 @@ public function batch(ThemeFoldersServiceInterface $service)
7677
*
7778
* @param ThemeFoldersServiceInterface $service
7879
* @return void
80+
*
81+
* @checked
82+
* @noTodo
83+
* @unitTest
7984
*/
8085
public function index(ThemeFoldersServiceInterface $service)
8186
{
82-
//todo テーマフォルダAPI 一覧取得
87+
$this->request->allowMethod(['get']);
88+
89+
try {
90+
$data = $this->getRequest()->getQueryParams();
91+
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
92+
$themeFiles = $service->getIndex($data);
93+
} catch (BcFormFailedException $e) {
94+
$this->setResponse($this->response->withStatus(400));
95+
$errors = $e->getForm()->getErrors();
96+
$message = __d('baser', '入力エラーです。内容を修正してください。' . $e->getMessage());
97+
} catch (\Throwable $e) {
98+
$this->setResponse($this->response->withStatus(400));
99+
$message = __d('baser', '処理中にエラーが発生しました。');
100+
}
101+
102+
$this->set([
103+
'themeFiles' => $themeFiles ?? null,
104+
'message' => $message ?? null,
105+
'errors' => $errors ?? null
106+
]);
107+
$this->viewBuilder()->setOption('serialize', ['themeFiles', 'message', 'errors']);
83108
}
84109

85110
/**

plugins/bc-theme-file/src/Service/ThemeFoldersService.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,5 +314,28 @@ public function getForm(array $data)
314314
return (new ThemeFolderForm())->setData($data);
315315
}
316316

317-
317+
/**
318+
* fullpathを作成
319+
* @param string $theme
320+
* @param string $type
321+
* @param string $path
322+
* @return string
323+
*
324+
* @checked
325+
* @noTodo
326+
*/
327+
public function getFullpath(string $theme, string $type, string $path)
328+
{
329+
$assets = [
330+
'css',
331+
'js',
332+
'img'
333+
];
334+
if (in_array($type, $assets)) {
335+
$viewPath = Plugin::path($theme) . 'webroot' . DS . $type . DS . $path;
336+
} else {
337+
$viewPath = Plugin::templatePath($theme) . $type . DS . $path;
338+
}
339+
return $viewPath;
340+
}
318341
}

plugins/bc-theme-file/tests/TestCase/Controller/Api/ThemeFoldersControllerTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,23 @@ public function tearDown(): void
6565
*/
6666
public function test_index()
6767
{
68-
$this->markTestIncomplete('このテストは未実装です。');
68+
//POSTデータを生成
69+
$data = [
70+
'plugin' => '',
71+
'theme' => 'BcFront',
72+
'type' => 'img',
73+
'path' => '',
74+
'assets' => false,
75+
'token' => $this->accessToken
76+
];
77+
$query = http_build_query($data);
78+
//APIをコール
79+
$this->get('/baser/api/bc-theme-file/theme_folders/index.json?' . $query);
80+
//レスポンスコードを確認
81+
$this->assertResponseSuccess();
82+
//戻る値を確認
83+
$result = json_decode((string)$this->_response->getBody());
84+
$this->assertNotNull($result->themeFiles);
6985
}
7086

7187
/**

0 commit comments

Comments
 (0)