2018-03-27 20:06:55 +03:00
|
|
|
import moment from 'moment';
|
2017-01-02 21:49:44 +03:00
|
|
|
import {Response} from 'ember-cli-mirage';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {dasherize} from '@ember/string';
|
2018-07-20 13:57:53 +03:00
|
|
|
import {isArray} from '@ember/array';
|
|
|
|
import {isBlank, isEmpty} from '@ember/utils';
|
2018-03-13 14:17:29 +03:00
|
|
|
import {paginateModelCollection} from '../utils';
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2018-07-20 13:57:53 +03:00
|
|
|
function normalizeBooleanParams(arr) {
|
|
|
|
if (!isArray(arr)) {
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr.map((i) => {
|
|
|
|
if (i === 'true') {
|
|
|
|
return true;
|
|
|
|
} else if (i === 'false') {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use GQL to parse filter string?
|
|
|
|
function extractFilterParam(param, filter) {
|
|
|
|
let filterRegex = new RegExp(`${param}:(.*?)(?:\\+|$)`);
|
|
|
|
let match;
|
|
|
|
|
|
|
|
let [, result] = filter.match(filterRegex) || [];
|
|
|
|
if (result.startsWith('[')) {
|
|
|
|
match = result.replace(/^\[|\]$/g, '').split(',');
|
|
|
|
} else if (result) {
|
|
|
|
match = [result];
|
|
|
|
}
|
|
|
|
|
|
|
|
return normalizeBooleanParams(match);
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
export default function mockPosts(server) {
|
2018-03-13 14:17:29 +03:00
|
|
|
server.post('/posts', function ({posts, users}) {
|
2017-01-02 21:49:44 +03:00
|
|
|
let attrs = this.normalizedRequestAttrs();
|
2018-03-13 14:17:29 +03:00
|
|
|
let authors = [];
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
// NOTE: this is necessary so that ember-cli-mirage has a valid user
|
|
|
|
// schema object rather than a plain object
|
|
|
|
// TODO: should ember-cli-mirage be handling this automatically?
|
|
|
|
attrs.authors.forEach((author) => {
|
|
|
|
authors.push(users.find(author.id));
|
|
|
|
});
|
|
|
|
|
|
|
|
attrs.authors = authors;
|
2017-07-10 14:33:05 +03:00
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
if (isBlank(attrs.slug) && !isBlank(attrs.title)) {
|
|
|
|
attrs.slug = dasherize(attrs.title);
|
|
|
|
}
|
|
|
|
|
|
|
|
return posts.create(attrs);
|
|
|
|
});
|
|
|
|
|
2018-07-20 13:57:53 +03:00
|
|
|
// TODO: handle authors filter
|
2017-01-25 23:05:28 +03:00
|
|
|
server.get('/posts/', function ({posts}, {queryParams}) {
|
2018-07-20 13:57:53 +03:00
|
|
|
let {filter, page, limit} = queryParams;
|
2017-01-25 23:05:28 +03:00
|
|
|
|
2018-07-20 13:57:53 +03:00
|
|
|
page = +page || 1;
|
|
|
|
limit = +limit || 15;
|
2017-01-25 23:05:28 +03:00
|
|
|
|
2018-07-20 13:57:53 +03:00
|
|
|
let statusFilter = extractFilterParam('status', filter);
|
|
|
|
let pageFilter = extractFilterParam('page', filter);
|
2017-01-25 23:05:28 +03:00
|
|
|
|
2018-07-20 13:57:53 +03:00
|
|
|
let collection = posts.all().filter((post) => {
|
|
|
|
let matchesStatus = true;
|
|
|
|
let matchesPage = true;
|
2017-01-25 23:05:28 +03:00
|
|
|
|
2018-07-20 13:57:53 +03:00
|
|
|
if (!isEmpty(statusFilter)) {
|
|
|
|
matchesStatus = statusFilter.includes(post.status);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEmpty(pageFilter)) {
|
|
|
|
matchesPage = pageFilter.includes(post.page);
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchesStatus && matchesPage;
|
|
|
|
});
|
2017-01-25 23:05:28 +03:00
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
return paginateModelCollection('posts', collection, page, limit);
|
2017-01-25 23:05:28 +03:00
|
|
|
});
|
2017-01-02 21:49:44 +03:00
|
|
|
|
|
|
|
server.get('/posts/:id/', function ({posts}, {params}) {
|
|
|
|
let {id} = params;
|
|
|
|
let post = posts.find(id);
|
|
|
|
|
|
|
|
return post || new Response(404, {}, {
|
|
|
|
errors: [{
|
|
|
|
errorType: 'NotFoundError',
|
|
|
|
message: 'Post not found.'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
server.put('/posts/:id/', function ({posts, users}, {params}) {
|
|
|
|
let attrs = this.normalizedRequestAttrs();
|
|
|
|
let post = posts.find(params.id);
|
|
|
|
let authors = [];
|
|
|
|
|
|
|
|
// NOTE: this is necessary so that ember-cli-mirage has a valid user
|
|
|
|
// schema object rather than a plain object
|
|
|
|
// TODO: should ember-cli-mirage be handling this automatically?
|
|
|
|
attrs.authors.forEach((author) => {
|
|
|
|
authors.push(users.find(author.id));
|
|
|
|
});
|
2017-03-14 06:27:57 +03:00
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
attrs.authors = authors;
|
2017-03-14 06:27:57 +03:00
|
|
|
|
2018-03-27 20:06:55 +03:00
|
|
|
attrs.updatedAt = moment.utc().toDate();
|
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
return post.update(attrs);
|
2017-03-14 06:27:57 +03:00
|
|
|
});
|
2017-01-02 21:49:44 +03:00
|
|
|
|
|
|
|
server.del('/posts/:id/');
|
|
|
|
}
|