From 9d0e63e6953d9303ddf29a9204df0050bb91ad20 Mon Sep 17 00:00:00 2001 From: cgfhtw Date: Tue, 30 Apr 2024 13:22:08 +0200 Subject: [PATCH] API Konsolidierung Verband --- .../frontend/v1}/stv/Verband.php | 99 +++++++++---------- .../Stv/Studentenverwaltung/List/New.js | 10 +- .../Stv/Studentenverwaltung/Verband.js | 15 ++- 3 files changed, 61 insertions(+), 63 deletions(-) rename application/controllers/{components => api/frontend/v1}/stv/Verband.php (86%) diff --git a/application/controllers/components/stv/Verband.php b/application/controllers/api/frontend/v1/stv/Verband.php similarity index 86% rename from application/controllers/components/stv/Verband.php rename to application/controllers/api/frontend/v1/stv/Verband.php index 18ce5c999..5a7960fd5 100644 --- a/application/controllers/components/stv/Verband.php +++ b/application/controllers/api/frontend/v1/stv/Verband.php @@ -1,17 +1,41 @@ . + */ if (! defined('BASEPATH')) exit('No direct script access allowed'); -class Verband extends FHC_Controller +/** + * This controller operates between (interface) the JS (GUI) and the back-end + * Provides data to the ajax get calls about verbände + * This controller works with JSON calls on the HTTP GET or POST and the output is always JSON + */ +class Verband extends FHCAPI_Controller { public function __construct() { - // TODO(chris): access! - parent::__construct(); - + // TODO(chris): permissions + $permissions = []; + $router = load_class('Router'); + $permissions[$router->method] = self::PERM_LOGGED; + parent::__construct($permissions); + + // Load Models $this->load->model('organisation/Studiengang_model', 'StudiengangModel'); } - /** * Remap calls: * / @@ -75,12 +99,9 @@ class Verband extends FHC_Controller $this->StudiengangModel->addOrder('kurzbz'); $result = $this->StudiengangModel->loadWhere(['v.aktiv' => true]); - if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - return $this->outputJson(getError($result)); - } - $list = getData($result) ?: []; + $list = $this->getDataOrTerminateWithError($result); + $list[] = [ 'name' => 'International', 'link' => 'inout', @@ -102,7 +123,7 @@ class Verband extends FHC_Controller ] ] ]; - $this->outputJson($list); + $this->terminateWithSuccess($list); } /** @@ -139,12 +160,8 @@ class Verband extends FHC_Controller 'v.studiengang_kz' => $studiengang_kz, 'v.aktiv' => true ]); - if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - return $this->outputJson(getError($result)); - } + $list = $this->getDataOrTerminateWithError($result); - $list = getData($result) ?: []; array_unshift($list, [ 'name' => 'PreStudent', 'link' => $link . 'prestudent', @@ -154,12 +171,9 @@ class Verband extends FHC_Controller if ($org_form === null) { // NOTE(chris): if mischform show orgforms $result = $this->StudiengangModel->load($studiengang_kz); - if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - return $this->outputJson(getError($result)); - } - if (hasData($result)) { - if (current(getData($result))->mischform) { + $result = $this->getDataOrTerminateWithError($result); + if ($result) { + if (current($result)->mischform) { $this->load->model('organisation/Studienordnung_model', 'StudienordnungModel'); $this->StudienordnungModel->addDistinct(); @@ -175,18 +189,14 @@ class Verband extends FHC_Controller 'studiengang_kz' => $studiengang_kz, 'p.orgform_kurzbz !=' => 'DDP' ]); - if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - return $this->outputJson(getError($result)); - } + $result = $this->getDataOrTerminateWithError($result); - if (hasData($result)) - $list = array_merge($list, getData($result)); + $list = array_merge($list, $result); } } } - $this->outputJson($list); + $this->terminateWithSuccess($list); } /** @@ -231,13 +241,8 @@ class Verband extends FHC_Controller $where['orgform_kurzbz'] = $org_form; $result = $this->GruppeModel->loadWhere($where); - if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - return $this->outputJson(getError($result)); - } - - $list = getData($result) ?: []; + $list = $this->getDataOrTerminateWithError($result); $this->StudiengangModel->addJoin('public.tbl_lehrverband v', 'studiengang_kz'); @@ -263,14 +268,11 @@ class Verband extends FHC_Controller $where['v.orgform_kurzbz'] = $org_form; $result = $this->StudiengangModel->loadWhere($where); - if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - return $this->outputJson(getError($result)); - } + $result = $this->getDataOrTerminateWithError($result); - $list = array_merge($list, getData($result) ?: []); + $list = array_merge($list, $result); - $this->outputJson($list); + $this->terminateWithSuccess($list); } /** @@ -313,14 +315,10 @@ class Verband extends FHC_Controller $where['v.orgform_kurzbz'] = $org_form; $result = $this->StudiengangModel->loadWhere($where); - if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - return $this->outputJson(getError($result)); - } - $list = getData($result) ?: []; + $list = $this->getDataOrTerminateWithError($result); - $this->outputJson($list); + $this->terminateWithSuccess($list); } /** @@ -341,13 +339,8 @@ class Verband extends FHC_Controller * - then: $stsem_obj->getPlusMinus(NULL, $number_displayed_past_studiensemester, 'ende ASC'); */ $result = $this->StudiensemesterModel->load(); - if (isError($result)) { - $this->output->set_status_header(REST_Controller::HTTP_INTERNAL_SERVER_ERROR); - $this->outputJson(getError($result)); - exit; - } - $studiensemester = getData($result) ?: []; + $studiensemester = $this->getDataOrTerminateWithError($result); $result = []; foreach ($studiensemester as $sem) { diff --git a/public/js/components/Stv/Studentenverwaltung/List/New.js b/public/js/components/Stv/Studentenverwaltung/List/New.js index f23245b7c..351737e23 100644 --- a/public/js/components/Stv/Studentenverwaltung/List/New.js +++ b/public/js/components/Stv/Studentenverwaltung/List/New.js @@ -130,21 +130,21 @@ export default { this.abortController.places = new AbortController(); this.$refs.form - .send(CoreRESTClient.get( - 'components/stv/address/getPlaces/' + this.formData.address.plz, + .get( + 'api/frontend/v1/stv/address/getPlaces/' + this.formData.address.plz, undefined, { signal: this.abortController.places.signal } - )) + ) .then(result => { - this.places = result + this.places = result.data }) .catch(error => { if (error.code != "ERR_CANCELED") window.setTimeout(this.loadPlaces, 100); else - console.error(error); + this.$fhcAlert.handleSystemError(error); }); }, loadStudienplaene() { diff --git a/public/js/components/Stv/Studentenverwaltung/Verband.js b/public/js/components/Stv/Studentenverwaltung/Verband.js index 92d343f08..bdc5500a6 100644 --- a/public/js/components/Stv/Studentenverwaltung/Verband.js +++ b/public/js/components/Stv/Studentenverwaltung/Verband.js @@ -53,8 +53,8 @@ export default { }); this.loading = true; - CoreRESTClient - .get("components/stv/verband/" + node.data.link) + this.$fhcApi + .get('api/frontend/v1/stv/verband/' + node.data.link) .then(result => result.data) .then(result => { const subNodes = result.map(this.mapResultToTreeData); @@ -128,7 +128,12 @@ export default { let promises = []; for (let parent in sortedInParents) - promises.push(CoreRESTClient.get("components/stv/verband/" + (parent == '_' ? '' : parent)).then(res => res.data).then(res => res.filter(node => sortedInParents[parent].includes(node.link + '')))); + promises.push( + this.$fhcApi + .get('api/frontend/v1/stv/verband/' + (parent == '_' ? '' : parent)) + .then(res => res.data) + .then(res => res.filter(node => sortedInParents[parent].includes(node.link + ''))) + ); // NOTE(chris): merge the resulting arrays and transform them to an associative one let result = [].concat.apply([], await Promise.all(promises)).reduce((o, node) => { @@ -171,8 +176,8 @@ export default { } }, mounted() { - CoreRESTClient - .get("components/stv/verband") + this.$fhcApi + .get('api/frontend/v1/stv/verband') .then(result => result.data) .then(result => { this.nodes = result.map(this.mapResultToTreeData);