From f2606b9a0ebb4e0ed045a1fc48aacc7260103bf3 Mon Sep 17 00:00:00 2001 From: ruturaj_kalal Date: Sun, 12 Nov 2023 01:13:11 -0500 Subject: [PATCH] pagination on fetch channels, playlists and videos --- .../scripts/firework/getChannelList.js | 50 +++++++++++++----- .../scripts/firework/getChannelPlaylist.js | 51 +++++++++++++------ .../scripts/firework/getChannelVideo.js | 49 +++++++++++++----- 3 files changed, 108 insertions(+), 42 deletions(-) diff --git a/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelList.js b/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelList.js index cd87674..b81444c 100644 --- a/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelList.js +++ b/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelList.js @@ -35,19 +35,43 @@ function getChannelListFun() } var htmlError = '
Something went wrong.
'; var service:Service =restService.getChannelListService; - service.URL += '/api/bus/'+businessId+'/channels/'; - var result:Result = service.call({ - 'Method':"GET", - 'token':getAccessToken - }); - if (result.isOk()) { - var htmlSuccess = result.getObject().toString(); - return htmlSuccess; - } else { - var resultMessage = JSON.parse(result.errorMessage); - ISML.renderTemplate('dashboard/errorMsg',{errorMsg:resultMessage}); - return; - } + var allChannels = []; + var nextPageUrl = '/api/bus/'+businessId+'/channels/'; + var originalURL = service.URL; + + do { + + service.URL = originalURL + nextPageUrl; + var result:Result = service.call({ + 'Method':"GET", + 'token':getAccessToken + }); + + if (result.isOk()) { + var response = JSON.parse(result.getObject().toString()); + if (response.channels && Array.isArray(response.channels)) { + allChannels = allChannels.concat(response.channels); + } + + // Check if there is a next page + if (response.paging && response.paging.next) { + nextPageUrl = response.paging.next; + } else { + nextPageUrl = null; + } + } else { + var resultMessage = JSON.parse(result.errorMessage); + ISML.renderTemplate('dashboard/errorMsg',{errorMsg:resultMessage}); + return; + } + + }while(nextPageUrl); + + var combinedChannels = { + channels: allChannels + }; + + return JSON.stringify(combinedChannels); } module.exports = { diff --git a/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelPlaylist.js b/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelPlaylist.js index 667d618..b250739 100644 --- a/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelPlaylist.js +++ b/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelPlaylist.js @@ -22,21 +22,42 @@ function getChannelPlayListFun(ChannelId) var businessId=businessOauthData.businessId; var getAccessToken=oauthtokenData.access_token; } - var htmlError = '
Something went wrong.
'; - var service:Service =restService.getChannelPlaylistService; - service.URL += '/api/bus/'+businessId+'/channels/'+ChannelId+'/playlists'; - var result:Result = service.call({ - 'Method':"GET", - 'token':getAccessToken - }); - if (result.isOk()) { - var htmlSuccess = result.getObject().toString(); - return htmlSuccess; - } else { - var resultMessage = JSON.parse(result.errorMessage); - ISML.renderTemplate('dashboard/errorMsg',{errorMsg:resultMessage}); - return; - } + var htmlError = '
Something went wrong.
'; + var service:Service =restService.getChannelPlaylistService; + var allPlaylists = []; + var nextPageUrl = '/api/bus/'+businessId+'/channels/'+ChannelId+'/playlists'; + var originalURL = service.URL; + + do { + service.URL = originalURL + nextPageUrl; + var result = service.call({ + 'Method': "GET", + 'token': getAccessToken + }); + + if (result.isOk()) { + var response = JSON.parse(result.getObject().toString()); + if (response.playlists && Array.isArray(response.playlists)) { + allPlaylists = allPlaylists.concat(response.playlists); + } + // Check if there is a next page + if (response.paging && response.paging.next) { + nextPageUrl = response.paging.next; + } else { + nextPageUrl = null; + } + } else { + var resultMessage = JSON.parse(result.errorMessage); + ISML.renderTemplate('dashboard/errorMsg',{errorMsg:resultMessage}); + return; + } + } while (nextPageUrl); + + var combinedPlaylists = { + playlists: allPlaylists + }; + + return JSON.stringify(combinedPlaylists); } module.exports = { getChannelPlayListFun: getChannelPlayListFun diff --git a/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelVideo.js b/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelVideo.js index 7c19fbd..495a5fe 100644 --- a/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelVideo.js +++ b/Cartridges/bm_firework_dashboard/cartridge/scripts/firework/getChannelVideo.js @@ -26,24 +26,45 @@ function getChannelVideoFun(ChannelId,playlistID) var service:Service =restService.getChannelVideoService; if(playlistID) { - service.URL += '/api/bus/'+businessId+'/channels/'+ChannelId+'/playlists/'+playlistID+'/videos'; + var nextPageUrl = '/api/bus/'+businessId+'/channels/'+ChannelId+'/playlists/'+playlistID+'/videos'; } else { - service.URL += '/api/bus/'+businessId+'/videos?channel_id='+ChannelId; + var nextPageUrl = '/api/bus/'+businessId+'/videos?channel_id='+ChannelId; } - var result:Result = service.call({ - 'Method':"GET", - 'token':getAccessToken - }); - if (result.isOk()) { - var htmlSuccess = result.getObject().toString(); - return htmlSuccess; - } else { - var resultMessage = JSON.parse(result.errorMessage); - ISML.renderTemplate('dashboard/errorMsg',{errorMsg:resultMessage}); - return; - } + var allVideos = []; + var originalURL = service.URL; + + do { + service.URL = originalURL + nextPageUrl; + var result:Result = service.call({ + 'Method':"GET", + 'token':getAccessToken + }); + if (result.isOk()) { + var response = JSON.parse(result.getObject().toString()); + if (response.videos && Array.isArray(response.videos)) { + allVideos = allVideos.concat(response.videos); + } + + // Check if there is a next page + if (response.paging && response.paging.next) { + nextPageUrl = response.paging.next; + } else { + nextPageUrl = null; + } + } else { + var resultMessage = JSON.parse(result.errorMessage); + ISML.renderTemplate('dashboard/errorMsg',{errorMsg:resultMessage}); + return; + } + } while(nextPageUrl); + + var combinedVideos = { + videos: allVideos + }; + + return JSON.stringify(combinedVideos); } module.exports = {