Skip to content

Commit

Permalink
fix baserproject#1999 API エントリー管理 編集 (baserproject#2046)
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 14, 2023
1 parent b33cbe5 commit 1b91085
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,42 @@ public function add(CustomEntriesServiceInterface $service)
/**
* カスタムエントリー 編集
* @param CustomEntriesServiceInterface $service
* @param int $id
*
* @checked
* @noTodo
* @unitTest
*/
public function edit(CustomEntriesServiceInterface $service)
public function edit(CustomEntriesServiceInterface $service, int $id)
{
//todo 編集
$this->request->allowMethod(['patch', 'post', 'put']);
$queryParams = $this->getRequest()->getQueryParams();
if (empty($queryParams['custom_table_id'])) {
throw new BadRequestException(__d('baser_core', 'パラメーターに custom_table_id を指定してください。'));
}
$entry = $errors = null;
try {
$service->setup($queryParams['custom_table_id'], $this->getRequest()->getData());
$entry = $service->update($service->get($id), $this->request->getData());
$message = __d('baser_core', 'フィールド「{0}」を更新しました。', $entry->title);
} catch (PersistenceFailedException $e) {
$errors = $e->getEntity()->getErrors();
$message = __d('baser_core', "入力エラーです。内容を修正してください。");
$this->setResponse($this->response->withStatus(400));
} 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,
'entry' => $entry,
'errors' => $errors
]);
$this->viewBuilder()->setOption('serialize', ['entry', 'message', 'errors']);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,52 @@ public function test_add()
*/
public function test_edit()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
$dataBaseService = $this->getService(BcDatabaseServiceInterface::class);
$customTable = $this->getService(CustomTablesServiceInterface::class);
//カスタムテーブルとカスタムエントリテーブルを生成
$customTable->create([
'id' => 1,
'name' => 'recruit_categories',
'title' => '求人情報',
'type' => '1',
'display_field' => 'title',
'has_child' => 0
]);
//フィクチャーからデーターを生成
$this->loadFixtureScenario(CustomEntriesScenario::class);
$data = [
'title' => 'title updated',
];
//APIを呼ぶ
$this->post('/baser/api/bc-custom-content/custom_entries/edit/1.json?custom_table_id=1&token=' . $this->accessToken, $data);
//ステータスを確認
$this->assertResponseOk();
//戻る値を確認
$result = json_decode((string)$this->_response->getBody());
$this->assertNotNull($result->entry);
$this->assertEquals('フィールド「title updated」を更新しました。', $result->message);

//タイトルを指定しない場合、
$this->post('/baser/api/bc-custom-content/custom_entries/edit/1.json?custom_table_id=1&token=' . $this->accessToken, ['title' => '']);
$this->assertResponseCode(400);
$result = json_decode((string)$this->_response->getBody());
$this->assertEquals('入力エラーです。内容を修正してください。', $result->message);
$this->assertEquals('タイトルは必須項目です。', $result->errors->title->_empty);

//存在しないIDを指定した場合、
$this->post('/baser/api/bc-custom-content/custom_entries/edit/11.json?custom_table_id=11&token=' . $this->accessToken, ['title' => '']);
$this->assertResponseCode(404);
$result = json_decode((string)$this->_response->getBody());
$this->assertEquals('データが見つかりません。', $result->message);

//custom_table_idを指定しない場合、
$this->post('/baser/api/bc-custom-content/custom_entries/edit/11.json?token=' . $this->accessToken, ['title' => '']);
$this->assertResponseCode(400);
$result = json_decode((string)$this->_response->getBody());
$this->assertEquals('パラメーターに custom_table_id を指定してください。', $result->message);

//不要なテーブルを削除
$dataBaseService->dropTable('custom_entry_1_recruit_categories');
}

/**
Expand Down

0 comments on commit 1b91085

Please sign in to comment.