mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-29 07:09:48 +03:00
52b6668955
- remove lodash from bower - move all resources to /mirage instead of /app/mirage - update endpoints to use ORM - general cleanup
63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
/* eslint-disable max-statements-per-line */
|
|
import {Response} from 'ember-cli-mirage';
|
|
|
|
export function paginatedResponse(modelName) {
|
|
return function (schema, request) {
|
|
let page = +request.queryParams.page || 1;
|
|
let limit = request.queryParams.limit || 15;
|
|
let pages, next, prev, models;
|
|
|
|
let allModels = this.serialize(schema[modelName].all())[modelName];
|
|
|
|
if (limit === 'all') {
|
|
pages = 1;
|
|
} else {
|
|
limit = +limit;
|
|
|
|
let start = (page - 1) * limit;
|
|
let end = start + limit;
|
|
|
|
pages = Math.ceil(allModels.length / limit);
|
|
models = allModels.slice(start, end);
|
|
|
|
if (start > 0) {
|
|
prev = page - 1;
|
|
}
|
|
|
|
if (end < allModels.length) {
|
|
next = page + 1;
|
|
}
|
|
}
|
|
|
|
return {
|
|
meta: {
|
|
pagination: {
|
|
page,
|
|
limit,
|
|
pages,
|
|
total: allModels.length,
|
|
next: next || null,
|
|
prev: prev || null
|
|
}
|
|
},
|
|
[modelName]: models || allModels
|
|
};
|
|
};
|
|
}
|
|
|
|
export function maintenanceResponse() {
|
|
return new Response(503, {}, {
|
|
errors: [{
|
|
errorType: 'Maintenance'
|
|
}]
|
|
});
|
|
}
|
|
|
|
export function versionMismatchResponse() {
|
|
return new Response(400, {}, {
|
|
errors: [{
|
|
errorType: 'VersionMismatchError'
|
|
}]
|
|
});
|
|
}
|