From 6d2a2b38a25ea6159fb968eea4e474a7527bf8d2 Mon Sep 17 00:00:00 2001 From: Mischa Spelt Date: Thu, 11 Jun 2020 19:55:19 +0200 Subject: [PATCH] Kabelkrant API gemaakt --- api/app/Http/Controllers/NewsController.php | 45 +++++++++++++++++---- api/routes/web.php | 1 + 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/api/app/Http/Controllers/NewsController.php b/api/app/Http/Controllers/NewsController.php index 6acc0d8..4ea2a0b 100644 --- a/api/app/Http/Controllers/NewsController.php +++ b/api/app/Http/Controllers/NewsController.php @@ -12,6 +12,9 @@ class NewsController extends Controller private static $EXTERNAL_NEWS_CATEGORY = 44; private static $BLOG_CATEGORY = 45; + private static $WEBSITE_TARGET = 1; + private static $TV_TARGET = 2; + private static $BASE_SQL = <<retrieveNewsItems($page, $count, $filter, []); return response()->json(['page' => $page, 'count' => $count, 'news' => $result]); } - + /** + * Lijst van alle nieuwsberichten voor de kabelkrant + */ + public function tvlist(Request $request) { + $count = (int)$request->get('aantal', 15); + if($count <= 0) { + return abort(400); + } + + $params = ['target' => self::$TV_TARGET, 'category' => self::$NEWS_CATEGORY, 'secondarycategory' => self::$EXTERNAL_NEWS_CATEGORY]; + $newsItems = app('db')->select(self::$BASE_SQL + . ' ORDER BY COALESCE(`news`.`pubupdatedt`, `news`.`creationdt`) DESC' + . ' LIMIT 0, ' . (int)$count, $params); + + $result = array(); + foreach($newsItems as $newsItem) { + $pictures = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $newsItem->id]); + $newsItem = new \Model\NewsItem($newsItem, $pictures); + $newsItem->content = strip_tags($newsItem->content); + if($newsItem->images && count($newsItem->images) > 1) { + $newsItem->images = [$newsItem->images[0]]; + } + + $result[] = $newsItem; + } + + return response()->json(['news' => $result]); + } + public function retrieveNewsItems($page, $count, $filter, $params) { $filterSql = ""; - $params = ['category' => self::$NEWS_CATEGORY, 'secondarycategory' => self::$EXTERNAL_NEWS_CATEGORY]; + $params = ['target' => self::$WEBSITE_TARGET, 'category' => self::$NEWS_CATEGORY, 'secondarycategory' => self::$EXTERNAL_NEWS_CATEGORY]; if($filter) { foreach($filter as $field => $value) { $paramName = preg_replace('/[^A-Za-z0-9]/', '', $field); @@ -143,7 +174,7 @@ QUERY; $today = new \DateTime('today'); $agendaItems = app('db')->select(self::$BASE_SQL . ' AND `news`.`enddt` >= :today ' - . ' ORDER BY `news`.`startdt` ASC, `news`.`enddt` ASC', ['category' => self::$CALENDAR_CATEGORY, 'secondarycategory' => 0, 'today' => $today]); + . ' ORDER BY `news`.`startdt` ASC, `news`.`enddt` ASC', ['target' => self::$WEBSITE_TARGET, 'category' => self::$CALENDAR_CATEGORY, 'secondarycategory' => 0, 'today' => $today]); $result = array(); foreach($agendaItems as $agendaItem) { @@ -192,7 +223,7 @@ QUERY; . ' AND `news`.`startdt` >= :start AND `news`.`startdt` <= :end' . ' ORDER BY COALESCE(`news`.`pubupdatedt`, `news`.`creationdt`) DESC' . ' LIMIT ' . (int)$startIndex . ', ' . (int)$count, - ['category' => $blog->news_category, 'secondarycategory' => 0, 'start' => $blog->start_date, 'end' => $blog->end_date]); + ['target' => self::$WEBSITE_TARGET, 'category' => $blog->news_category, 'secondarycategory' => 0, 'start' => $blog->start_date, 'end' => $blog->end_date]); $result = array(); foreach($blogItems as $blogItem) { @@ -256,7 +287,7 @@ QUERY $result = array(); foreach($recent as $item) { - $newsItem = app('db')->select(self::$BASE_SQL . ' AND `news`.`id` = :newsId', ['category' => $category, 'secondarycategory' => $secondarycategory, 'newsId' => $item->id])[0]; + $newsItem = app('db')->select(self::$BASE_SQL . ' AND `news`.`id` = :newsId', ['target' => self::$WEBSITE_TARGET, 'category' => $category, 'secondarycategory' => $secondarycategory, 'newsId' => $item->id])[0]; $pictures = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $newsItem->id]); $result[] = new \Model\NewsItem($newsItem, $pictures); } @@ -296,7 +327,7 @@ QUERY } private function itemFromCategory($category, $id) { - $params = ['id' => (int)$id]; + $params = ['id' => (int)$id, 'target' => self::$WEBSITE_TARGET]; if(is_array($category)) { $params['category'] = $category[0]; $params['secondarycategory'] = $category[1]; diff --git a/api/routes/web.php b/api/routes/web.php index 475ecec..0cfef85 100644 --- a/api/routes/web.php +++ b/api/routes/web.php @@ -22,6 +22,7 @@ $app->get( 'rss/nieuws', 'NewsController@rss' ); $app->get( 'menu/special', 'MenuController@special' ); $app->get('nieuws/overzicht', 'NewsController@newslist' ); +$app->get('nieuws/kabelkrant', 'NewsController@tvlist' ); $app->get('nieuws/regio/{region:[a-z0-9-]+}', 'NewsController@regionlist' ); $app->get('nieuws/thema/{theme:[a-z0-9-]+}', 'NewsController@themelist' ); $app->get('nieuws/zoeken/{query}', 'NewsController@findnews' );