2020-10-16 20:02:58 +03:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
const errors = require('@tryghost/errors');
|
2021-07-19 13:08:55 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-10-16 20:02:58 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
|
2021-07-19 13:08:55 +03:00
|
|
|
const messages = {
|
|
|
|
snippetNotFound: 'Snippet not found.',
|
|
|
|
snippetAlreadyExists: 'Snippet already exists.'
|
|
|
|
};
|
|
|
|
|
2020-10-16 20:02:58 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'snippets',
|
|
|
|
|
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'limit',
|
|
|
|
'order',
|
|
|
|
'page'
|
|
|
|
],
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Snippet.findPage(frame.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
|
|
|
headers: {},
|
|
|
|
data: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Snippet.findOne(frame.data, frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (!model) {
|
|
|
|
return Promise.reject(new errors.NotFoundError({
|
2021-07-19 13:08:55 +03:00
|
|
|
message: tpl(messages.snippetNotFound)
|
2020-10-16 20:02:58 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
|
|
|
headers: {},
|
|
|
|
permissions: true,
|
2021-07-19 13:08:55 +03:00
|
|
|
query(frame) {
|
|
|
|
return models.Snippet.add(frame.data.snippets[0], frame.options)
|
|
|
|
.catch((error) => {
|
|
|
|
if (error.code && error.message.toLowerCase().indexOf('unique') !== -1) {
|
|
|
|
throw new errors.ValidationError({message: tpl(messages.snippetAlreadyExists)});
|
|
|
|
}
|
2020-10-16 20:02:58 +03:00
|
|
|
|
2021-07-19 13:08:55 +03:00
|
|
|
throw error;
|
|
|
|
});
|
2020-10-16 20:02:58 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: {
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Snippet.edit(frame.data.snippets[0], frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (!model) {
|
|
|
|
return Promise.reject(new errors.NotFoundError({
|
2021-07-19 13:08:55 +03:00
|
|
|
message: tpl(messages.snippetNotFound)
|
2020-10-16 20:02:58 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
2021-06-24 11:58:35 +03:00
|
|
|
frame.options.require = true;
|
|
|
|
|
2020-10-16 20:02:58 +03:00
|
|
|
return models.Snippet.destroy(frame.options)
|
|
|
|
.then(() => null)
|
|
|
|
.catch(models.Snippet.NotFoundError, () => {
|
|
|
|
return Promise.reject(new errors.NotFoundError({
|
2021-07-19 13:08:55 +03:00
|
|
|
message: tpl(messages.snippetNotFound)
|
2020-10-16 20:02:58 +03:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|