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';
|
|
|
|
import {isBlank} from '@ember/utils';
|
2018-03-13 14:17:29 +03:00
|
|
|
import {paginateModelCollection} from '../utils';
|
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);
|
|
|
|
});
|
|
|
|
|
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 = {};
|
|
|
|
|
|
|
|
if (status && status !== 'all') {
|
|
|
|
query.status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (staticPages === 'false') {
|
|
|
|
query.page = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (staticPages === 'true') {
|
|
|
|
query.page = true;
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
let collection = posts.where(query);
|
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/');
|
|
|
|
}
|