Skip to content

Commit

Permalink
fix #1855 アップロードカテゴリAPI 削除 (#1923)
Browse files Browse the repository at this point in the history
Co-authored-by: Đỗ Văn Hùng <HungDV2022>
  • Loading branch information
HungDV2022 committed Mar 13, 2023
1 parent 1dcef73 commit d020798
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
Expand Up @@ -92,11 +92,36 @@ public function edit(UploaderCategoriesServiceInterface $service)
* 削除API
*
* @param UploaderCategoriesServiceInterface $service
* @return void
* @param int $id
*
* @checked
* @notodo
* @unitTest
*/
public function delete(UploaderCategoriesServiceInterface $service)
public function delete(UploaderCategoriesServiceInterface $service, int $id)
{
//todo 削除API
$this->request->allowMethod(['post', 'delete']);
$uploaderCategory = null;
try {
$uploaderCategory = $service->get($id);
if ($service->delete($id)) {
$message = __d('baser_core', 'アップロードカテゴリ「{0}」を削除しました。', $uploaderCategory->name);
} else {
$this->setResponse($this->response->withStatus(500));
$message = __d('baser_core', 'データベース処理中にエラーが発生しました。');
}
} catch (RecordNotFoundException $e) {
$this->setResponse($this->response->withStatus(404));
$message = __d('baser_core', 'データが見つかりません。');
} catch (\Throwable $e) {
$message = __d('baser_core', 'データベース処理中にエラーが発生しました。' . $e->getMessage());
$this->setResponse($this->response->withStatus(500));
}
$this->set([
'message' => $message,
'uploaderCategory' => $uploaderCategory
]);
$this->viewBuilder()->setOption('serialize', ['uploaderCategory', 'message']);
}

/**
Expand Down
Expand Up @@ -187,7 +187,25 @@ public function test_copy()
*/
public function test_delete()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
//テストデーターを生成
$this->loadFixtureScenario(UploaderCategoriesScenario::class);
//APIを呼ぶ
$this->post("/baser/api/bc-uploader/uploader_categories/delete/1.json?token=" . $this->accessToken);
//ステータスを確認
$this->assertResponseSuccess();
//戻る値を確認
$result = json_decode((string)$this->_response->getBody());
$this->assertEquals('アップロードカテゴリ「blog」を削除しました。', $result->message);
$this->assertEquals('blog', $result->uploaderCategory->name);

//無効なアップロードカテゴリIDを指定した場合、
//APIを呼ぶ
$this->post("/baser/api/bc-uploader/uploader_categories/delete/10.json?token=" . $this->accessToken);
//ステータスを確認
$this->assertResponseCode(404);
//戻る値を確認
$result = json_decode((string)$this->_response->getBody());
$this->assertEquals('データが見つかりません。', $result->message);
}

/**
Expand Down

0 comments on commit d020798

Please sign in to comment.