mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-30 21:40:39 +03:00
Wired up adding post to collections to API
This commit is contained in:
parent
b48a1d0a57
commit
93bad82a24
@ -116,9 +116,9 @@ module.exports = {
|
|||||||
// @NOTE: should have permissions when moving out of Alpha
|
// @NOTE: should have permissions when moving out of Alpha
|
||||||
permissions: false,
|
permissions: false,
|
||||||
async query(frame) {
|
async query(frame) {
|
||||||
const collectionPost = await collectionsService.api.addPost(Object.assign(frame.data.collections[0], {
|
const collectionPost = await collectionsService.api.addPost(frame.options.id, {
|
||||||
collection_id: frame.options.id
|
id: frame.data.posts[0].id
|
||||||
}), frame.options);
|
});
|
||||||
|
|
||||||
if (!collectionPost) {
|
if (!collectionPost) {
|
||||||
throw new errors.NotFoundError({
|
throw new errors.NotFoundError({
|
||||||
|
@ -1,27 +1,41 @@
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {import('@tryghost/collections').Collection} collection
|
* @param {import('@tryghost/collections').Collection | object} collection
|
||||||
*
|
*
|
||||||
* @returns {SerializedCollection}
|
* @returns {SerializedCollection}
|
||||||
*/
|
*/
|
||||||
const mapper = (collection) => {
|
const mapper = (collection) => {
|
||||||
const json = collection.toJSON();
|
let json;
|
||||||
|
let posts;
|
||||||
|
if (collection.toJSON) {
|
||||||
|
json = collection.toJSON();
|
||||||
|
posts = json.posts.map((postId, index) => {
|
||||||
|
return {
|
||||||
|
id: postId,
|
||||||
|
sort_order: index
|
||||||
|
};
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
json = collection;
|
||||||
|
posts = json.posts.map((post) => {
|
||||||
|
return {
|
||||||
|
id: post.id,
|
||||||
|
sort_order: post.sort_order
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const serialized = {
|
const serialized = {
|
||||||
id: json.id,
|
id: json.id,
|
||||||
title: json.title,
|
title: json.title || null,
|
||||||
slug: json.slug,
|
slug: json.slug,
|
||||||
description: json.description,
|
description: json.description || null,
|
||||||
type: json.type,
|
type: json.type,
|
||||||
filter: json.filter,
|
filter: json.filter,
|
||||||
feature_image: json.featureImage,
|
feature_image: json.feature_image || json.featureImage || null,
|
||||||
created_at: json.createdAt.toISOString().replace(/\d{3}Z$/, '000Z'),
|
created_at: (json.created_at || json.createdAt).toISOString().replace(/\d{3}Z$/, '000Z'),
|
||||||
updated_at: json.updatedAt.toISOString().replace(/\d{3}Z$/, '000Z'),
|
updated_at: (json.updated_at || json.updatedAt).toISOString().replace(/\d{3}Z$/, '000Z'),
|
||||||
posts: json.posts.map(post => ({
|
posts
|
||||||
id: post.id,
|
|
||||||
title: post.title,
|
|
||||||
slug: post.slug
|
|
||||||
}))
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return serialized;
|
return serialized;
|
||||||
|
@ -1,30 +1,24 @@
|
|||||||
const models = require('../../models');
|
|
||||||
const {
|
const {
|
||||||
CollectionsService,
|
CollectionsService,
|
||||||
CollectionsRepositoryInMemory
|
CollectionsRepositoryInMemory
|
||||||
} = require('@tryghost/collections');
|
} = require('@tryghost/collections');
|
||||||
const PostsDataRepositoryBookshelf = require('./PostsDataRepositoryBookshelf');
|
|
||||||
|
|
||||||
class CollectionsServiceWrapper {
|
class CollectionsServiceWrapper {
|
||||||
api;
|
api;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const collectionsRepositoryInMemory = new CollectionsRepositoryInMemory();
|
const collectionsRepositoryInMemory = new CollectionsRepositoryInMemory();
|
||||||
const postsDataRepositoryBookshelf = new PostsDataRepositoryBookshelf({
|
|
||||||
Post: models.Post
|
|
||||||
});
|
|
||||||
|
|
||||||
const collectionsService = new CollectionsService({
|
const collectionsService = new CollectionsService({
|
||||||
collectionsRepository: collectionsRepositoryInMemory,
|
collectionsRepository: collectionsRepositoryInMemory
|
||||||
postsRepository: postsDataRepositoryBookshelf
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.api = {
|
this.api = {
|
||||||
browse: collectionsService.getAll.bind(collectionsService),
|
browse: collectionsService.getAll.bind(collectionsService),
|
||||||
read: collectionsService.getById.bind(collectionsService),
|
read: collectionsService.getById.bind(collectionsService),
|
||||||
add: collectionsService.save.bind(collectionsService),
|
add: collectionsService.createCollection.bind(collectionsService),
|
||||||
edit: collectionsService.edit.bind(collectionsService),
|
edit: collectionsService.edit.bind(collectionsService),
|
||||||
addPost: collectionsService.addPost.bind(collectionsService),
|
addPost: collectionsService.addPostToCollection.bind(collectionsService),
|
||||||
destroy: collectionsService.destroy.bind(collectionsService)
|
destroy: collectionsService.destroy.bind(collectionsService)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -322,12 +322,15 @@ Object {
|
|||||||
"posts": Array [
|
"posts": Array [
|
||||||
Object {
|
Object {
|
||||||
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": 0,
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": 1,
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": 2,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
"title": "Test Collection Edited",
|
"title": "Test Collection Edited",
|
||||||
@ -342,7 +345,7 @@ exports[`Collections API edit Can add Posts and append Post to a Collection 2: [
|
|||||||
Object {
|
Object {
|
||||||
"access-control-allow-origin": "http://127.0.0.1:2369",
|
"access-control-allow-origin": "http://127.0.0.1:2369",
|
||||||
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
|
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
|
||||||
"content-length": "346",
|
"content-length": "391",
|
||||||
"content-type": "application/json; charset=utf-8",
|
"content-type": "application/json; charset=utf-8",
|
||||||
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
|
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
|
||||||
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
|
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
|
||||||
@ -364,12 +367,15 @@ Object {
|
|||||||
"posts": Array [
|
"posts": Array [
|
||||||
Object {
|
Object {
|
||||||
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": 0,
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": 1,
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": 2,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
"title": "Test Collection Edited",
|
"title": "Test Collection Edited",
|
||||||
@ -384,7 +390,103 @@ exports[`Collections API edit Can add Posts and append Post to a Collection 4: [
|
|||||||
Object {
|
Object {
|
||||||
"access-control-allow-origin": "http://127.0.0.1:2369",
|
"access-control-allow-origin": "http://127.0.0.1:2369",
|
||||||
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
|
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
|
||||||
"content-length": "346",
|
"content-length": "391",
|
||||||
|
"content-type": "application/json; charset=utf-8",
|
||||||
|
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
|
||||||
|
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
|
||||||
|
"vary": "Accept-Version, Origin, Accept-Encoding",
|
||||||
|
"x-powered-by": "Express",
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`Collections API edit Can add Posts and append Post to a Collection 5: [body] 1`] = `
|
||||||
|
Object {
|
||||||
|
"collections": Array [
|
||||||
|
Object {
|
||||||
|
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||||
|
"description": null,
|
||||||
|
"feature_image": null,
|
||||||
|
"filter": null,
|
||||||
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"posts": Array [
|
||||||
|
Object {
|
||||||
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": Any<Number>,
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": Any<Number>,
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": Any<Number>,
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": Any<Number>,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"title": "Test Collection Edited",
|
||||||
|
"type": "manual",
|
||||||
|
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`Collections API edit Can add Posts and append Post to a Collection 6: [headers] 1`] = `
|
||||||
|
Object {
|
||||||
|
"access-control-allow-origin": "http://127.0.0.1:2369",
|
||||||
|
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
|
||||||
|
"content-length": "440",
|
||||||
|
"content-type": "application/json; charset=utf-8",
|
||||||
|
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
|
||||||
|
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
|
||||||
|
"vary": "Accept-Version, Origin, Accept-Encoding",
|
||||||
|
"x-powered-by": "Express",
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`Collections API edit Can add Posts and append Post to a Collection 7: [body] 1`] = `
|
||||||
|
Object {
|
||||||
|
"collections": Array [
|
||||||
|
Object {
|
||||||
|
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||||
|
"description": null,
|
||||||
|
"feature_image": null,
|
||||||
|
"filter": null,
|
||||||
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"posts": Array [
|
||||||
|
Object {
|
||||||
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": Any<Number>,
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": Any<Number>,
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": Any<Number>,
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||||
|
"sort_order": Any<Number>,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"title": "Test Collection Edited",
|
||||||
|
"type": "manual",
|
||||||
|
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`Collections API edit Can add Posts and append Post to a Collection 8: [headers] 1`] = `
|
||||||
|
Object {
|
||||||
|
"access-control-allow-origin": "http://127.0.0.1:2369",
|
||||||
|
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
|
||||||
|
"content-length": "440",
|
||||||
"content-type": "application/json; charset=utf-8",
|
"content-type": "application/json; charset=utf-8",
|
||||||
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
|
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
|
||||||
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
|
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
|
||||||
|
@ -11,7 +11,8 @@ const {
|
|||||||
anyErrorId,
|
anyErrorId,
|
||||||
anyLocationFor,
|
anyLocationFor,
|
||||||
anyObjectId,
|
anyObjectId,
|
||||||
anyISODateTime
|
anyISODateTime,
|
||||||
|
anyNumber
|
||||||
} = matchers;
|
} = matchers;
|
||||||
|
|
||||||
const matchCollection = {
|
const matchCollection = {
|
||||||
@ -24,12 +25,16 @@ const matchCollection = {
|
|||||||
*
|
*
|
||||||
* @param {number} postCount
|
* @param {number} postCount
|
||||||
*/
|
*/
|
||||||
const buildMatcher = (postCount) => {
|
const buildMatcher = (postCount, opts = {}) => {
|
||||||
|
let obj = {
|
||||||
|
id: anyObjectId
|
||||||
|
};
|
||||||
|
if (opts.withSortOrder) {
|
||||||
|
obj.sort_order = anyNumber;
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
...matchCollection,
|
...matchCollection,
|
||||||
posts: Array(postCount).fill({
|
posts: Array(postCount).fill(obj)
|
||||||
id: anyObjectId
|
|
||||||
})
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -234,9 +239,7 @@ describe('Collections API', function () {
|
|||||||
etag: anyEtag
|
etag: anyEtag
|
||||||
})
|
})
|
||||||
.matchBodySnapshot({
|
.matchBodySnapshot({
|
||||||
posts: [{
|
collections: [buildMatcher(4, {withSortOrder: true})]
|
||||||
id: anyObjectId
|
|
||||||
}]
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// verify the posts are persisted across requests
|
// verify the posts are persisted across requests
|
||||||
@ -248,7 +251,7 @@ describe('Collections API', function () {
|
|||||||
etag: anyEtag
|
etag: anyEtag
|
||||||
})
|
})
|
||||||
.matchBodySnapshot({
|
.matchBodySnapshot({
|
||||||
collections: [buildMatcher(3)]
|
collections: [buildMatcher(4, {withSortOrder: true})]
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(readResponse.body.collections[0].posts.length, 4, 'Post should have been added to a Collection');
|
assert.equal(readResponse.body.collections[0].posts.length, 4, 'Post should have been added to a Collection');
|
||||||
|
Loading…
Reference in New Issue
Block a user