From 27287bd07ad1fed8e3b35f6729ddf3e1039627de Mon Sep 17 00:00:00 2001 From: SimonGschnell Date: Mon, 24 Jun 2024 16:05:51 +0200 Subject: [PATCH] creating the api endpoint that fetches the content of the news instead of text metdata --- .../controllers/api/frontend/v1/Cms.php | 46 +++++++++++++++++-- application/models/content/News_model.php | 13 +++++- public/js/api/cms.js | 8 ++++ public/js/components/DashboardWidget/News.js | 13 +++--- 4 files changed, 68 insertions(+), 12 deletions(-) diff --git a/application/controllers/api/frontend/v1/Cms.php b/application/controllers/api/frontend/v1/Cms.php index 5877de709..07a705e49 100644 --- a/application/controllers/api/frontend/v1/Cms.php +++ b/application/controllers/api/frontend/v1/Cms.php @@ -36,12 +36,29 @@ class Cms extends FHCAPI_Controller 'ContentID' => self::PERM_LOGGED, 'getOrtKurzbzContent' => self::PERM_LOGGED, 'content' => self::PERM_LOGGED, + 'news' => self::PERM_LOGGED, ]); $this->load->library('CmsLib'); } + //------------------------------------------------------------------------------------------------------------------ + // Private methods + + private function fetchContent($content_id){ + $content = $this->cmslib->getContent($content_id); + + if (isError($content)) + $this->terminateWithError(getError($content), self::ERROR_TYPE_GENERAL); + + if(getData($content)){ + return getData($content); + }else{ + $this->terminateWithError("No content was found", self::ERROR_TYPE_GENERAL); + } + } + //------------------------------------------------------------------------------------------------------------------ // Public methods @@ -60,11 +77,8 @@ class Cms extends FHCAPI_Controller if(!isset($content_id)) $this->terminateWithError("content_id is missing", self::ERROR_TYPE_GENERAL); - $content = $this->cmslib->getContent($content_id, $version, $sprache, $sichtbar); + $content = $this->fetchContent($content_id); - if (isError($content)) - $this->terminateWithError(getError($content), self::ERROR_TYPE_GENERAL); - $this->terminateWithSuccess(getData($content)); } @@ -93,6 +107,30 @@ class Cms extends FHCAPI_Controller $this->terminateWithSuccess($result->content_id ?? NULL); } + public function news() + { + $limit = $this->input->get('limit',TRUE); + + // return early if the limit parameter is missing or is not greater than 0 + if(!isset($limit) and $limit > 0) + $this->terminateWithError("limit parameter is missing", self::ERROR_TYPE_GENERAL); + + $this->load->model('content/news_model', 'NewsModel'); + + $news_content_ids = $this->NewsModel->getNewsContentIDs($limit); + $news_content = array(); + + if(isError($news_content_ids)) + $this->terminateWithError(getError($news_content_ids), self::ERROR_TYPE_GENERAL); + + foreach(getData($news_content_ids) as $content_id){ + $news_content[] = $this->fetchContent($content_id->content_id); + } + + $this->terminateWithSuccess($news_content); + + } + } diff --git a/application/models/content/News_model.php b/application/models/content/News_model.php index f4f98b601..f8e1fbb66 100755 --- a/application/models/content/News_model.php +++ b/application/models/content/News_model.php @@ -19,14 +19,23 @@ class News_model extends DB_Model */ public function getAll($limit = null) { + $this->addJoin("campus.tbl_content","content_id"); return $this->loadWhere(' - text IS NOT NULL - AND datum <= NOW() AND (datum_bis IS NULL OR datum_bis >= now()::date) + text IS NOT NULL + datum <= NOW() AND (datum_bis IS NULL OR datum_bis >= now()::date) ORDER BY datum DESC LIMIT ' . $this->escape($limit) ); } + public function getNewsContentIDs($limit=10){ + $this->addSelect(['content_id']); + return $this->loadWhere("datum <= NOW() AND (datum_bis IS NULL OR datum_bis >= now()::date) + ORDER BY datum DESC + LIMIT " . $this->escape($limit)); + + } + /** diff --git a/public/js/api/cms.js b/public/js/api/cms.js index 5602c7bf8..651ddd9b7 100644 --- a/public/js/api/cms.js +++ b/public/js/api/cms.js @@ -11,5 +11,13 @@ export default { } ); }, + news(limit) { + return this.$fhcApi.get( + FHC_JS_DATA_STORAGE_OBJECT.app_root + + FHC_JS_DATA_STORAGE_OBJECT.ci_router + + "/api/frontend/v1/Cms/news", + { limit: limit} + ); + }, } \ No newline at end of file diff --git a/public/js/components/DashboardWidget/News.js b/public/js/components/DashboardWidget/News.js index 6f862d085..8cdacd66f 100755 --- a/public/js/components/DashboardWidget/News.js +++ b/public/js/components/DashboardWidget/News.js @@ -28,11 +28,12 @@ export default { } }, created(){ - axios - .get(this.apiurl + '/dashboard/Api/getNews', {params: {limit: MAX_LOADED_NEWS}}) - .then(res => { this.allNewsList = res.data }) - .catch(err => { console.error('ERROR: ', err.response.data) }); - + + + this.$fhcApi.factory.cms.news(MAX_LOADED_NEWS) + .then(res => { this.allNewsList = res.data }) + .catch(err => { console.error('ERROR: ', err.response.data) }); + this.$emit('setConfig', false); }, methods: { @@ -41,7 +42,7 @@ export default { this.$refs.newsModal.show(); } }, - template: `
+ template: /*html*/`
Top News