From 518f0aeb2066d737e02bae019ab4f07453e9b6a6 Mon Sep 17 00:00:00 2001 From: Ro Date: Sun, 12 Jul 2020 14:25:35 -0700 Subject: [PATCH] added public methods for getting paginated entry data and single entry data to front end API --- brain/api/v1/pages.js | 82 ++++++++++++++++++++++++++++++++++++-- src/com/Base.js | 3 ++ src/libraries/FipamoAPI.js | 27 +++++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) diff --git a/brain/api/v1/pages.js b/brain/api/v1/pages.js index d3b2b00..f74a689 100644 --- a/brain/api/v1/pages.js +++ b/brain/api/v1/pages.js @@ -37,15 +37,89 @@ var post_upload = multer({ }).array('post_image'); /** - * Retrieves list of Pages + * Retrieves a page of a published entries * @public */ -router.get('/', (req, res) => { +router.get('/published/:pageNum?', (req, res) => { + //console.log('PAGE NUM', req.params.pageNum); + let pageNum = req.params.pageNum; + if (pageNum === null || pageNum === '' || !pageNum) pageNum = 1; + let pages = []; book.getPage().then(result => { - res.json(result); + result.sort((a, b) => parseFloat(b.metadata.id) - parseFloat(a.metadata.id)); + let displayed = _.filter(result, page => { + return ( + page.metadata.deleted === false && + page.metadata.published === true && + page.metadata.layout != 'index' + ); + }); + var pageLimit = 6; + var count = Math.ceil(displayed.length / pageLimit); + if (pageNum > count || isNaN(pageNum)) + res.json({ type: DataEvent.REQUEST_LAME, message: "That page doesn't exist, champ." }); + var rangeIndex = pageNum * pageLimit - pageLimit; + + let meta = []; + + for (let index = 0; index < pageLimit; index++) { + const page = displayed[index + rangeIndex]; + try { + if ( + page.metadata.id != null && + page.metadata.deleted === false && + page.metadata.published === true + ) { + let entry = page.metadata; + entry.content = page.content; + //console.log('ENTRY', entry); + pages.push({ + page: entry, + displayDate: moment(page.metadata.created).fromNow() + }); + } + } catch (e) { + //console.log("NO POST", e) + } + } + meta.push({ currentPage: pageNum, totalPages: count }); + let data = { pages: pages, meta: meta }; + res.json({ + type: DataEvent.REQUEST_GOOD, + message: 'This is Page ' + pageNum + ' of ' + count, + data: data + }); }); }); +/** + * Retrieves single entry + * @public + */ + +router.get('/single/:id', (req, res) => { + let id = req.params.id; + if (id === null || id === '') + res.json({ type: DataEvent.REQUEST_LAME, message: " Nah, this isn't here." }); + book.getPage(id) + .then(page => { + let entry = page.metadata; + entry.content = page.content; + res.json({ + type: DataEvent.REQUEST_GOOD, + message: 'Found it. Here you go.', + data: entry + }); + }) + .catch(err => { + res.json({ + type: DataEvent.REQUEST_LAME, + message: "This doesn't seem to be here, homie.", + err: err.message + }); + }); +}); + /** * Add/Update Page */ @@ -121,7 +195,7 @@ router.post('/write/:task?', feature_upload, (req, res) => { } }) .catch(() => { - console.log(); + //console.log(); }); res.json(result); }) diff --git a/src/com/Base.js b/src/com/Base.js index e938466..b44bad3 100644 --- a/src/com/Base.js +++ b/src/com/Base.js @@ -20,6 +20,9 @@ export default class Base { // methods //-------------------------- start() { + api.getPage('448a35c5-06a3-497f-b7fc-8475eb0eaf8a').then(result => { + console.log('PAGE', result); + }); if (document.getElementById('dash-form') || document.getElementById('dash-init')) { if (document.getElementById('dash-form')) { document diff --git a/src/libraries/FipamoAPI.js b/src/libraries/FipamoAPI.js index 52a729f..48adf2a 100644 --- a/src/libraries/FipamoAPI.js +++ b/src/libraries/FipamoAPI.js @@ -7,6 +7,8 @@ export const CONTENT_TYPE_FORM = 'x-www-form-urlencoded'; export const API_STATUS = '/api/v1/auth/status'; export const API_INIT = '/api/v1/auth/init'; export const API_LOGIN = '/api/v1/auth/login'; +export const API_GET_PAGES = '/api/v1/page/published'; +export const API_GET_PAGE = '/api/v1/page/single'; import * as DataEvent from '../com/events/DataEvent'; export default class APIUtils { //-------------------------- @@ -44,6 +46,31 @@ export default class APIUtils { }); }); } + getPages(num) { + let pageNum = num; + if (pageNum === null || pageNum === '' || !pageNum) pageNum = 1; + return new Promise((resolve, reject) => { + this._request(API_GET_PAGES + '/' + pageNum, DataEvent.API_GET_PAGES, REQUEST_TYPE_GET) + .then(result => { + resolve(result); + }) + .catch(err => { + reject(err); + }); + }); + } + + getPage(id) { + return new Promise((resolve, reject) => { + this._request(API_GET_PAGE + '/' + id, DataEvent.API_GET_PAGES, REQUEST_TYPE_GET) + .then(result => { + resolve(result); + }) + .catch(err => { + reject(err); + }); + }); + } //-------------------------- // private //--------------------------