Ghost/ghost/admin/mirage/config/posts.js
Kevin Ansfield 983110d931 Switched from ember-cli-shims to new module imports (#779)
no issue

- add eslint-plugin-ember, configure no-old-shims rule
- run `eslint --fix` on `app`, `lib`, `mirage`, and `tests` to move imports to the new module imports
- further cleanup of Ember globals usage
- remove event-dispatcher initializer now that `canDispatchToEventManager` is deprecated
2017-08-22 14:53:26 +07:00

73 lines
2.0 KiB
JavaScript

import {Response} from 'ember-cli-mirage';
import {dasherize} from '@ember/string';
import {isBlank} from '@ember/utils';
import {paginateModelArray} from '../utils';
export default function mockPosts(server) {
server.post('/posts', function ({posts}) {
let attrs = this.normalizedRequestAttrs();
// mirage expects `author` to be a reference but we only have an ID
attrs.authorId = attrs.author;
delete attrs.author;
if (isBlank(attrs.slug) && !isBlank(attrs.title)) {
attrs.slug = dasherize(attrs.title);
}
return posts.create(attrs);
});
// 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);
});
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.'
}]
});
});
// Handle embedded author in post
server.put('/posts/:id/', ({posts}, request) => {
let {posts: [post]} = JSON.parse(request.requestBody);
let {author} = post;
delete post.author;
let savedPost = posts.find(request.params.id).update(post);
savedPost.authorId = author;
savedPost.save();
return savedPost;
});
server.del('/posts/:id/');
}