Simplify destroy post API endpoint implementation

no issue

- no need to fetch the post before
- the model implementation does that already
This commit is contained in:
kirrg001 2018-04-06 14:58:08 +02:00 committed by Katharina Irrgang
parent c8f2dd11ba
commit 25cd7c7756

View File

@ -1,6 +1,8 @@
'use strict';
// # Posts API
// RESTful API for the Post resource
var Promise = require('bluebird'),
const Promise = require('bluebird'),
_ = require('lodash'),
pipeline = require('../lib/promise/pipeline'),
localUtils = require('./utils'),
@ -13,8 +15,9 @@ var Promise = require('bluebird'),
allowedIncludes = [
'created_by', 'updated_by', 'published_by', 'author', 'tags', 'fields', 'authors', 'authors.roles'
],
unsafeAttrs = ['author_id', 'status', 'authors'],
posts;
unsafeAttrs = ['author_id', 'status', 'authors'];
let posts;
/**
* ### Posts API Methods
@ -220,7 +223,8 @@ posts = {
/**
* ## Destroy
* Delete a post, cleans up tag relations, but not unused tags
* Delete a post, cleans up tag relations, but not unused tags.
* You can only delete a post by `id`.
*
* @public
* @param {{id (required), context,...}} options
@ -234,17 +238,14 @@ posts = {
* @param {Object} options
*/
function deletePost(options) {
var Post = models.Post,
data = _.defaults({status: 'all'}, options),
fetchOpts = _.defaults({require: true, columns: 'id'}, options);
const opts = _.defaults({require: true}, options);
return Post.findOne(data, fetchOpts).then(function () {
return Post.destroy(options).return(null);
}).catch(Post.NotFoundError, function () {
throw new common.errors.NotFoundError({
message: common.i18n.t('errors.api.posts.postNotFound')
return models.Post.destroy(opts).return(null)
.catch(models.Post.NotFoundError, function () {
throw new common.errors.NotFoundError({
message: common.i18n.t('errors.api.posts.postNotFound')
});
});
});
}
// Push all of our tasks into a `tasks` array in the correct order