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);
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:43:58 +03:00
|
|
|
// NOTE: mirage requires Model objects when saving relationships, however the
|
|
|
|
// `attrs` on POST/PUT requests will contain POJOs for authors and tags so we
|
|
|
|
// need to replace them
|
|
|
|
function extractAuthors(postAttrs, users) {
|
|
|
|
return postAttrs.authors.map(author => users.find(author.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractTags(postAttrs, tags) {
|
|
|
|
return postAttrs.tags.map((requestTag) => {
|
|
|
|
let tag = tags.find(requestTag.id);
|
|
|
|
|
|
|
|
if (!tag) {
|
|
|
|
tag = tag.create(requestTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
export default function mockPosts(server) {
|
2018-11-08 16:43:58 +03:00
|
|
|
server.post('/posts', function ({posts, users, tags}) {
|
2017-01-02 21:49:44 +03:00
|
|
|
let attrs = this.normalizedRequestAttrs();
|
2018-03-13 14:17:29 +03:00
|
|
|
|
2018-11-08 16:43:58 +03:00
|
|
|
attrs.authors = extractAuthors(attrs, users);
|
|
|
|
attrs.tags = extractTags(attrs, tags);
|
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);
|
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;
|
2017-01-25 23:05:28 +03:00
|
|
|
|
2018-07-20 13:57:53 +03:00
|
|
|
if (!isEmpty(statusFilter)) {
|
|
|
|
matchesStatus = statusFilter.includes(post.status);
|
|
|
|
}
|
|
|
|
|
2019-02-22 06:17:33 +03:00
|
|
|
return matchesStatus;
|
2018-07-20 13:57:53 +03:00
|
|
|
});
|
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-11-08 16:43:58 +03:00
|
|
|
server.put('/posts/:id/', function ({posts, users, tags}, {params}) {
|
2018-03-13 14:17:29 +03:00
|
|
|
let attrs = this.normalizedRequestAttrs();
|
|
|
|
let post = posts.find(params.id);
|
2017-03-14 06:27:57 +03:00
|
|
|
|
2018-11-08 16:43:58 +03:00
|
|
|
attrs.authors = extractAuthors(attrs, users);
|
|
|
|
attrs.tags = extractTags(attrs, tags);
|
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/');
|
|
|
|
}
|