creating the api endpoint that fetches the content of the news instead of text metdata

This commit is contained in:
SimonGschnell
2024-06-24 16:05:51 +02:00
parent 7267d526bb
commit 27287bd07a
4 changed files with 68 additions and 12 deletions
@@ -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);
}
}
+11 -2
View File
@@ -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));
}
/**
+8
View File
@@ -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}
);
},
}
+7 -6
View File
@@ -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: `<div class="widgets-news w-100 h-100">
template: /*html*/`<div class="widgets-news w-100 h-100">
<div class="d-flex flex-column h-100">
<div class="d-flex">
<header><b>Top News</b></header>