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';
|
|
|
|
import {isBlank} from '@ember/utils';
|
2017-01-25 23:05:28 +03:00
|
|
|
import {paginateModelArray} from '../utils';
|
2017-01-02 21:49:44 +03:00
|
|
|
|
|
|
|
export default function mockPosts(server) {
|
|
|
|
server.post('/posts', function ({posts}) {
|
|
|
|
let attrs = this.normalizedRequestAttrs();
|
|
|
|
|
2017-07-10 14:33:05 +03:00
|
|
|
// mirage expects `author` to be a reference but we only have an ID
|
|
|
|
attrs.authorId = attrs.author;
|
|
|
|
delete attrs.author;
|
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
if (isBlank(attrs.slug) && !isBlank(attrs.title)) {
|
|
|
|
attrs.slug = dasherize(attrs.title);
|
|
|
|
}
|
|
|
|
|
|
|
|
return posts.create(attrs);
|
|
|
|
});
|
|
|
|
|
2017-01-25 23:05:28 +03:00
|
|
|
// TODO: handle author filter
|
|
|
|
server.get('/posts/', function ({posts}, {queryParams}) {
|
|
|
|
let page = +queryParams.page || 1;
|
|
|
|
let limit = +queryParams.limit || 15;
|
|
|
|
let {status, staticPages} = queryParams;
|
|
|
|
let query = {};
|
|
|
|
let models;
|
|
|
|
|
|
|
|
if (status && status !== 'all') {
|
|
|
|
query.status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (staticPages === 'false') {
|
|
|
|
query.page = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (staticPages === 'true') {
|
|
|
|
query.page = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
models = posts.where(query).models;
|
|
|
|
|
|
|
|
return paginateModelArray('posts', models, page, limit);
|
|
|
|
});
|
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.'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-14 06:27:57 +03:00
|
|
|
// Handle embedded author in post
|
2017-09-28 05:33:00 +03:00
|
|
|
server.put('/posts/:id/', function ({posts}, request) {
|
|
|
|
let post = this.normalizedRequestAttrs();
|
2017-03-14 06:27:57 +03:00
|
|
|
let {author} = post;
|
|
|
|
delete post.author;
|
|
|
|
|
|
|
|
let savedPost = posts.find(request.params.id).update(post);
|
|
|
|
savedPost.authorId = author;
|
|
|
|
savedPost.save();
|
|
|
|
|
|
|
|
return savedPost;
|
|
|
|
});
|
2017-01-02 21:49:44 +03:00
|
|
|
|
|
|
|
server.del('/posts/:id/');
|
|
|
|
}
|