2020-02-14 12:33:10 +03:00
|
|
|
const Promise = require('bluebird');
|
2020-05-22 21:22:20 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
|
|
|
const errors = require('@tryghost/errors');
|
2020-02-14 12:33:10 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
|
|
|
|
const ALLOWED_INCLUDES = ['count.members'];
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'labels',
|
|
|
|
|
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'filter',
|
|
|
|
'fields',
|
|
|
|
'limit',
|
|
|
|
'order',
|
|
|
|
'page'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Label.findPage(frame.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'filter',
|
|
|
|
'fields'
|
|
|
|
],
|
|
|
|
data: [
|
|
|
|
'id',
|
|
|
|
'slug'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Label.findOne(frame.data, frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (!model) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.labels.labelNotFound')
|
2020-02-14 12:33:10 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
2020-06-04 14:42:15 +03:00
|
|
|
headers: {},
|
2020-02-14 12:33:10 +03:00
|
|
|
options: [
|
|
|
|
'include'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
2020-06-04 15:23:10 +03:00
|
|
|
async query(frame) {
|
|
|
|
try {
|
|
|
|
return await models.Label.add(frame.data.labels[0], frame.options);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code && error.message.toLowerCase().indexOf('unique') !== -1) {
|
|
|
|
throw new errors.ValidationError({message: i18n.t('errors.api.labels.labelAlreadyExists')});
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
2020-02-14 12:33:10 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: {
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'id',
|
|
|
|
'include'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Label.edit(frame.data.labels[0], frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (!model) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.labels.labelNotFound')
|
2020-02-14 12:33:10 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model.wasChanged()) {
|
|
|
|
this.headers.cacheInvalidate = true;
|
|
|
|
} else {
|
|
|
|
this.headers.cacheInvalidate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
2020-04-13 13:21:47 +03:00
|
|
|
return models.Label.destroy(frame.options)
|
|
|
|
.then(() => null)
|
|
|
|
.catch(models.Label.NotFoundError, () => {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.labels.labelNotFound')
|
2020-04-13 13:21:47 +03:00
|
|
|
}));
|
|
|
|
});
|
2020-02-14 12:33:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|