2017-01-02 21:49:44 +03:00
|
|
|
/* 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;
|
2017-01-25 23:05:28 +03:00
|
|
|
let limit = +request.queryParams.limit || 15;
|
2018-03-13 14:17:29 +03:00
|
|
|
let collection = schema[modelName].all();
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
return paginateModelCollection(modelName, collection, page, limit);
|
2017-01-25 23:05:28 +03:00
|
|
|
};
|
|
|
|
}
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
export function paginateModelCollection(modelName, collection, page, limit) {
|
2017-01-25 23:05:28 +03:00
|
|
|
let pages, next, prev, models;
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2017-01-25 23:05:28 +03:00
|
|
|
if (limit === 'all') {
|
|
|
|
pages = 1;
|
|
|
|
} else {
|
|
|
|
limit = +limit;
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2017-01-25 23:05:28 +03:00
|
|
|
let start = (page - 1) * limit;
|
|
|
|
let end = start + limit;
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
pages = Math.ceil(collection.models.length / limit);
|
|
|
|
models = collection.models.slice(start, end);
|
2017-01-25 23:05:28 +03:00
|
|
|
|
|
|
|
if (start > 0) {
|
|
|
|
prev = page - 1;
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
if (end < collection.models.length) {
|
2017-01-25 23:05:28 +03:00
|
|
|
next = page + 1;
|
2017-01-02 21:49:44 +03:00
|
|
|
}
|
2017-01-25 23:05:28 +03:00
|
|
|
}
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
collection.meta = {
|
|
|
|
pagination: {
|
|
|
|
page,
|
|
|
|
limit,
|
|
|
|
pages,
|
|
|
|
total: collection.models.length,
|
|
|
|
next: next || null,
|
|
|
|
prev: prev || null
|
|
|
|
}
|
2017-01-02 21:49:44 +03:00
|
|
|
};
|
2018-03-13 14:17:29 +03:00
|
|
|
|
|
|
|
if (models) {
|
|
|
|
collection.models = models;
|
|
|
|
}
|
|
|
|
|
|
|
|
return collection;
|
2017-01-02 21:49:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function maintenanceResponse() {
|
|
|
|
return new Response(503, {}, {
|
|
|
|
errors: [{
|
|
|
|
errorType: 'Maintenance'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function versionMismatchResponse() {
|
|
|
|
return new Response(400, {}, {
|
|
|
|
errors: [{
|
|
|
|
errorType: 'VersionMismatchError'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|