From 980ced9d72ca526634af0c2ba915b5181cea567a Mon Sep 17 00:00:00 2001 From: wangyuting Date: Thu, 30 Nov 2023 10:08:45 +0800 Subject: [PATCH] * Optimize api edit function code. --- module/api/config/form.php | 15 +++++++++++++ module/api/control.php | 25 ++++++--------------- module/api/model.php | 43 +++++++++++++------------------------ module/api/ui/edit.html.php | 14 ++++++------ 4 files changed, 44 insertions(+), 53 deletions(-) diff --git a/module/api/config/form.php b/module/api/config/form.php index 16c9a8d2e3..94609dbe4d 100644 --- a/module/api/config/form.php +++ b/module/api/config/form.php @@ -50,3 +50,18 @@ $config->api->form->create['paramsExample'] = array('type' => 'string', 'requi $config->api->form->create['response'] = array('type' => 'string', 'required' => false, 'default' => '', 'control' => 'editor'); $config->api->form->create['responseExample'] = array('type' => 'string', 'required' => false, 'default' => ''); $config->api->form->create['desc'] = array('type' => 'string', 'required' => false, 'default' => '', 'control' => 'editor'); + +$config->api->form->edit = array(); +$config->api->form->edit['module'] = array('type' => 'int', 'required' => false, 'default' => 0); +$config->api->form->edit['title'] = array('type' => 'string', 'required' => true, 'default' => ''); +$config->api->form->edit['protocol'] = array('type' => 'string', 'required' => true, 'default' => ''); +$config->api->form->edit['method'] = array('type' => 'string', 'required' => true, 'default' => ''); +$config->api->form->edit['path'] = array('type' => 'string', 'required' => true, 'default' => ''); +$config->api->form->edit['requestType'] = array('type' => 'string', 'required' => false, 'default' => ''); +$config->api->form->edit['status'] = array('type' => 'string', 'required' => false, 'default' => ''); +$config->api->form->edit['owner'] = array('type' => 'string', 'required' => false, 'default' => ''); +$config->api->form->edit['params'] = array('type' => 'string', 'required' => false, 'default' => '', 'control' => 'editor'); +$config->api->form->edit['paramsExample'] = array('type' => 'string', 'required' => false, 'default' => ''); +$config->api->form->edit['response'] = array('type' => 'string', 'required' => false, 'default' => '', 'control' => 'editor'); +$config->api->form->edit['responseExample'] = array('type' => 'string', 'required' => false, 'default' => ''); +$config->api->form->edit['desc'] = array('type' => 'string', 'required' => false, 'default' => '', 'control' => 'editor'); diff --git a/module/api/control.php b/module/api/control.php index 367ed4aa27..e4a505fbfc 100755 --- a/module/api/control.php +++ b/module/api/control.php @@ -496,34 +496,21 @@ class api extends control $api = $this->api->getLibById($apiID); if(!empty($_POST)) { - $changes = $this->api->update($apiID); + $formData = form::data($this->config->api->form->edit)->add('id', $apiID)->add('version', $api->version)->add('editedBy', $this->app->user->account)->add('editedDate', helper::now())->get(); + + $this->api->update($formData); + if(dao::isError()) return $this->sendError(dao::getError()); - - if($changes) - { - $actionID = $this->action->create('api', $apiID, 'edited', '', '', '', false); - $this->action->logHistory($actionID, $changes); - } - return $this->sendSuccess(array('locate' => helper::createLink('api', 'index', "libID=$api->lib&moduleID=0&apiID=$apiID"))); } - if($api) - { - $this->view->api = $api; - $this->view->edit = true; - } - $this->setMenu($api->lib); - $this->getTypeOptions($api->lib); - $this->view->title = $api->title . $this->lang->api->edit; - $this->view->gobackLink = $this->createLink('api', 'index', "libID={$api->lib}&moduleID={$api->module}&apiID=$apiID"); - $this->view->user = $this->app->user->account; + $this->view->title = $api->title . $this->lang->colon . $this->lang->api->edit; + $this->view->api = $api; $this->view->allUsers = $this->loadModel('user')->getPairs('devfirst|noclosed');; $this->view->moduleOptionMenu = $this->loadModel('tree')->getOptionMenu($api->lib, 'api', $startModuleID = 0); - $this->view->moduleID = $api->module ? (int)$api->module : (int)$this->cookie->lastDocModule; $this->display(); } diff --git a/module/api/model.php b/module/api/model.php index 2465e8b9fa..e2ba9e61a6 100644 --- a/module/api/model.php +++ b/module/api/model.php @@ -195,46 +195,33 @@ class apiModel extends model * * @param int $apiID * @access public - * @return bool|array + * @return bool */ - public function update($apiID) + public function update(object $formData): bool { - $oldApi = $this->dao->findByID($apiID)->from(TABLE_API)->fetch(); - - if(!empty($_POST['editedDate']) and $oldApi->editedDate != $this->post->editedDate) - { - dao::$errors[] = $this->lang->error->editedByOther; - return false; - } - - $now = helper::now(); - $data = fixer::input('post') - ->skipSpecial('params,response') - ->add('editedBy', $this->app->user->account) - ->add('editedDate', $now) - ->add('version', $oldApi->version) - ->setDefault('product,module', 0) - ->stripTags($this->config->api->editor->edit['id'], $this->config->allowedTags) - ->remove('type,undefined') - ->get(); - - $changes = common::createChanges($oldApi, $data); - if(!empty($changes)) $data->version = $oldApi->version + 1; + $oldApi = $this->dao->findByID($formData->id)->from(TABLE_API)->fetch(); + $changes = common::createChanges($oldApi, $formData); + if(!empty($changes)) $formData->version = $formData->version + 1; $this->dao->update(TABLE_API) - ->data($data) + ->data($formData) ->autoCheck() ->batchCheck($this->config->api->edit->requiredFields, 'notempty') - ->where('id')->eq($apiID) + ->where('id')->eq($formData->id) ->exec(); if(dao::isError()) return false; - $data->id = $apiID; - $apiSpec = $this->getApiSpecByData($data); + if($changes) + { + $actionID = $this->loadModel('action')->create('api', $formData->id, 'edited', '', '', '', false); + $this->action->logHistory($actionID, $changes); + } + + $apiSpec = $this->getApiSpecByData($formData); $this->dao->replace(TABLE_API_SPEC)->data($apiSpec)->exec(); - return $changes; + return !dao::isError(); } /** diff --git a/module/api/ui/edit.html.php b/module/api/ui/edit.html.php index 9ecd168113..b14c0311d8 100644 --- a/module/api/ui/edit.html.php +++ b/module/api/ui/edit.html.php @@ -61,7 +61,7 @@ if(!empty($api->params['header'])) ), h::td ( - $param['required'] ? html("") : html("") + $param['required'] ? html("") : html("") ), h::td ( @@ -113,7 +113,7 @@ if(!empty($api->params['query'])) ), h::td ( - $query['required'] ? html("") : html("") + $query['required'] ? html("") : html("") ), h::td ( @@ -178,7 +178,7 @@ $parseTree = function($data, $typeList, $level = 0) use (&$parseTree) ), h::td ( - $data['required'] ? html("") : html("") + $data['required'] ? html("") : html("") ), h::td ( @@ -354,7 +354,7 @@ formPanel div ( setClass('panel-title text-lg'), - $lang->api->edit + $title ) ), formGroup @@ -569,7 +569,8 @@ formPanel set::label($lang->api->paramsExample), textarea ( - set::name('paramsExample') + set::name('paramsExample'), + set::value($api->paramsExample) ) ), formGroup @@ -616,7 +617,8 @@ formPanel set::label($lang->api->responseExample), textarea ( - set::name('responseExample') + set::name('responseExample'), + set::value($api->responseExample) ) ), formGroup