mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-29 07:09:48 +03:00
d37f68adcb
refs TryGhost/Ghost#8525 - fix `saveTitle` action - don't abort title save when we have an empty title - force a "dirty" state so that the save actually happens - add acceptance test for title blur behaviour - extract multiple instances `"(Untitled)"` into a const
73 lines
2.0 KiB
JavaScript
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/');
|
|
}
|