0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Added PUT /collections/id to Admin API

refs https://github.com/TryGhost/Team/issues/3167

- This is part of scaffolding for collections API. Allows to edit collection resource
This commit is contained in:
Naz 2023-05-17 14:42:37 +07:00 committed by naz
parent d3a8aad319
commit f3f3d58acf
5 changed files with 140 additions and 1 deletions

View file

@ -1,5 +1,11 @@
const errors = require('@tryghost/errors');
const tpl = require('@tryghost/tpl');
const collectionsService = require('../../services/collections');
const messages = {
collectionNotFound: 'Collection not found.'
};
module.exports = {
docName: 'collections',
@ -26,5 +32,41 @@ module.exports = {
async query(frame) {
return await collectionsService.api.add(frame.data.collections[0], frame.options);
}
},
edit: {
headers: {},
options: [
'id'
],
validation: {
options: {
id: {
required: true
}
}
},
// @NOTE: should have permissions when moving out of Alpha
permissions: false,
async query(frame) {
const model = await collectionsService.api.edit(frame.data.collections[0], frame.options);
if (!model) {
return Promise.reject(new errors.NotFoundError({
message: tpl(messages.tagNotFound)
}));
}
// @NOTE: cache invalidation has to be done manually for now
// because the model's wasChanged is not returned from
// the service using in-memory repository layer
// if (model.wasChanged()) {
this.headers.cacheInvalidate = true;
// } else {
// this.headers.cacheInvalidate = false;
// }
return model;
}
}
};

View file

@ -11,7 +11,8 @@ class CollectionsServiceWrapper {
this.api = {
browse: collectionsService.getAll.bind(collectionsService),
add: collectionsService.save.bind(collectionsService)
add: collectionsService.save.bind(collectionsService),
edit: collectionsService.save.bind(collectionsService)
};
}
}

View file

@ -21,6 +21,7 @@ module.exports = function apiRoutes() {
// ## Collections
router.get('/collections', mw.authAdminApi, labs.enabledMiddleware('collections'), http(api.collections.browse));
router.post('/collections', mw.authAdminApi, labs.enabledMiddleware('collections'), http(api.collections.add));
router.put('/collections/:id', mw.authAdminApi, labs.enabledMiddleware('collections'), http(api.collections.edit));
// ## Configuration
router.get('/config', mw.authAdminApi, http(api.config.read));

View file

@ -63,3 +63,56 @@ Object {
"x-powered-by": "Express",
}
`;
exports[`Collections API Can edit a Collection 1: [body] 1`] = `
Object {
"collections": Array [
Object {
"deleted": false,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"title": "Test Collection to Edit",
},
],
}
`;
exports[`Collections API Can edit a Collection 2: [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": "101",
"content-type": "application/json; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"location": StringMatching /https\\?:\\\\/\\\\/\\.\\*\\?\\\\/collections\\\\/\\[a-f0-9\\]\\{24\\}\\\\//,
"vary": "Accept-Version, Origin, Accept-Encoding",
"x-cache-invalidate": "/*",
"x-powered-by": "Express",
}
`;
exports[`Collections API Can edit a Collection 3: [body] 1`] = `
Object {
"collections": Array [
Object {
"deleted": false,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"title": "Test Collection Edited",
},
],
}
`;
exports[`Collections API Can edit a Collection 4: [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": "100",
"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-cache-invalidate": "/*",
"x-powered-by": "Express",
}
`;

View file

@ -1,3 +1,4 @@
const assert = require('assert');
const {
agentProvider,
fixtureManager,
@ -62,4 +63,45 @@ describe('Collections API', function () {
collections: [matchCollection]
});
});
it('Can edit a Collection', async function () {
const collection = {
title: 'Test Collection to Edit'
};
const addResponse = await agent
.post('/collections/')
.body({
collections: [collection]
})
.expectStatus(201)
.matchHeaderSnapshot({
'content-version': anyContentVersion,
etag: anyEtag,
location: anyLocationFor('collections')
})
.matchBodySnapshot({
collections: [matchCollection]
});
const collectionId = addResponse.body.collections[0].id;
const editResponse = await agent
.put(`/collections/${collectionId}/`)
.body({
collections: [{
title: 'Test Collection Edited'
}]
})
.expectStatus(200)
.matchHeaderSnapshot({
'content-version': anyContentVersion,
etag: anyEtag
})
.matchBodySnapshot({
collections: [matchCollection]
});
assert.equal(editResponse.body.collections[0].title, 'Test Collection Edited');
});
});