added public methods for getting paginated entry data and single entry data to front end API
This commit is contained in:
parent
634011059d
commit
518f0aeb20
3 changed files with 108 additions and 4 deletions
|
@ -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);
|
||||
})
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
//--------------------------
|
||||
|
|
Loading…
Reference in a new issue