From 17512e60b2c083b382429741e6b11ca69d47c49c Mon Sep 17 00:00:00 2001 From: cgfhtw Date: Tue, 21 May 2024 10:53:23 +0200 Subject: [PATCH] Favorites => fhcApi --- .../api/frontend/v1/stv/Favorites.php | 74 +++++++++++++++++++ public/js/api/stv.js | 2 + public/js/api/stv/verband.js | 15 ++++ .../Stv/Studentenverwaltung/Verband.js | 21 +++--- 4 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 application/controllers/api/frontend/v1/stv/Favorites.php create mode 100644 public/js/api/stv/verband.js diff --git a/application/controllers/api/frontend/v1/stv/Favorites.php b/application/controllers/api/frontend/v1/stv/Favorites.php new file mode 100644 index 000000000..5b4b4fa73 --- /dev/null +++ b/application/controllers/api/frontend/v1/stv/Favorites.php @@ -0,0 +1,74 @@ +. + */ + +if (! defined('BASEPATH')) exit('No direct script access allowed'); + +/** + * This controller operates between (interface) the JS (GUI) and the back-end + * Provides data to the ajax get calls about favorite verbände + * Listens to ajax post calls to change the favorite verbände data + * This controller works with JSON calls on the HTTP GET or POST and the output is always JSON + */ +class Favorites extends FHCAPI_Controller +{ + public function __construct() + { + // TODO(chris): access! + parent::__construct([ + 'index' => self::PERM_LOGGED, + 'set' => self::PERM_LOGGED + ]); + + // Load models + $this->load->model('system/Variable_model', 'VariableModel'); + + // TODO(chris): variable table might be to small to store favorites! + } + + public function index() + { + $result = $this->VariableModel->getVariables(getAuthUID(), ['stv_favorites']); + + $data = $this->getDataOrTerminateWithError($result); + + if (!$data) + $this->outputJson(null); + else + $this->outputJson($data['stv_favorites']); + } + + public function set() + { + $this->load->library('form_validation'); + + $this->form_validation->set_rules('favorites', 'Favorites', 'required'); + + if ($this->form_validation->run() == false) { + $this->output->set_status_header(REST_Controller::HTTP_BAD_REQUEST); + return $this->outputJson($this->form_validation->error_array()); + } + + $favorites = $this->input->post('favorites'); + + $result = $this->VariableModel->setVariable(getAuthUID(), 'stv_favorites', $favorites); + + $this->getDataOrTerminateWithError($result); + + $this->outputJsonSuccess(true); + } +} diff --git a/public/js/api/stv.js b/public/js/api/stv.js index 3f16b0847..734a22649 100644 --- a/public/js/api/stv.js +++ b/public/js/api/stv.js @@ -1,7 +1,9 @@ +import verband from './stv/verband.js'; import filter from './stv/filter.js'; import konto from './stv/konto.js'; export default { + verband, filter, konto, configStudent() { diff --git a/public/js/api/stv/verband.js b/public/js/api/stv/verband.js new file mode 100644 index 000000000..f3479c81b --- /dev/null +++ b/public/js/api/stv/verband.js @@ -0,0 +1,15 @@ +export default { + get() { + return this.$fhcApi.get('api/frontend/v1/stv/verband'); + }, + favorites: { + get() { + return this.$fhcApi.get('api/frontend/v1/stv/favorites'); + }, + set(favorites) { + return this.$fhcApi.post('api/frontend/v1/stv/favorites/set', { + favorites + }); + } + } +} \ No newline at end of file diff --git a/public/js/components/Stv/Studentenverwaltung/Verband.js b/public/js/components/Stv/Studentenverwaltung/Verband.js index bdc5500a6..4ea1184ad 100644 --- a/public/js/components/Stv/Studentenverwaltung/Verband.js +++ b/public/js/components/Stv/Studentenverwaltung/Verband.js @@ -105,7 +105,8 @@ export default { this.favnodes = await this.loadNodes(this.favorites.list); } this.favorites.on = !this.favorites.on; - CoreRESTClient.post("components/stv/favorites/set", {favorites: JSON.stringify(this.favorites)}); + this.$fhcApi + .factory.stv.verband.favorites.set(JSON.stringify(this.favorites)); this.loading = false; }, async loadNodes(links) { @@ -156,7 +157,8 @@ export default { this.favorites.list.push(key.data.link + ''); } - CoreRESTClient.post("components/stv/favorites/set", {favorites: JSON.stringify(this.favorites)}); + this.$fhcApi + .factory.stv.verband.favorites.set(JSON.stringify(this.favorites)); }, unsetFavFocus(e) { if (e.target.dataset?.linkFavAdd !== undefined) { @@ -177,19 +179,18 @@ export default { }, mounted() { this.$fhcApi - .get('api/frontend/v1/stv/verband') - .then(result => result.data) + .factory.stv.verband.get() .then(result => { - this.nodes = result.map(this.mapResultToTreeData); + this.nodes = result.data.map(this.mapResultToTreeData); this.loading = false; }) .catch(this.$fhcAlert.handleSystemError); - CoreRESTClient - .get("components/stv/favorites") - .then(result => result.data) + + this.$fhcApi + .factory.stv.verband.favorites.get() .then(result => { - if (result) { - let f = JSON.parse(result); + if (result.data) { + let f = JSON.parse(result.data); if (f.on) { this.loading = true; this.favorites = f;