Skip to content

Commit

Permalink
update update post function
Browse files Browse the repository at this point in the history
  • Loading branch information
ezy committed Jul 24, 2018
1 parent ec09988 commit 5a2e0f2
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 53 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ const config = require('./server/config/config');

// Start listening
app.listen(config.port, () => {
console.log('Sever started http://localhost:%s', config.port);
console.log('Server started http://localhost:%s', config.port);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"passport-local": "^1.0.0",
"pg": "^7.4.3",
"pg-hstore": "^2.3.2",
"sequelize": "^4.37.8",
"sequelize": "^4.38.0",
"sequelize-cli": "^4.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion seeders/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const changeCase = require('change-case');

let postsList = [];

for (let i = 0; i < 6; i++) {
for (let i = 0; i < 2; i++) {
let title = faker.lorem.sentence(5);
const postObj = {
id: faker.random.number(100000),
Expand Down
78 changes: 52 additions & 26 deletions server/api/post/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,18 @@ function createPost(req, res) {
postMedia,
postStatus,
postExpiry,
postFrequency
postFrequency,
postTerms
};

Post.create(newPost)
Post.create(newPost, {
include: [{
model: Term,
as: 'postTerms'
}]
})
.then((post) => {
newPost = post.dataValues;
newPost.postTerms = postTerms;
if (postTerms.length) {
postTerms.forEach((term) => {
let { termType, termName, termSlug } = term;
Term.findOrCreate({
where: { termSlug },
defaults: { termType, termName }
})
.spread((term2) => {
return post.addPostTerm(term2);
});
});
}
return res.json({'post': newPost});
return res.json({post: post});
})
.catch((err) => res.status(400).send({
error: err.errors
Expand Down Expand Up @@ -120,23 +112,57 @@ function updatePost(req, res) {

const postSlug = req.params.postSlug;

Post.findOne({where: { postSlug }})
Post.findOne({where: { postSlug }, include: [{
model: Term,
as: 'postTerms',
required: false,
attributes: ['id','termType','termName','termSlug'],
through: { attributes: [] }
}]
})
.then((post) => {
if (!post) {
return res.status(404).send({
error: 'No post found'
});
}

// Change the postSlug if the postTitle is different
let newTitle = changeCase.paramCase(req.body.postTitle);
if (!post.dataValues.postSlug.includes(newTitle)) {
req.body.postSlug = `${newTitle}-${Date.now()}`;
// Update the post slug based on the title if title is new
const postTitle = req.body.postTitle ? req.body.postTitle.trim() : null;
if (req.body.postTitle && !post.dataValues.postSlug.includes(`${changeCase.paramCase(postTitle)}`)) {
let slug = postTitle ? `${changeCase.paramCase(postTitle)}-${Date.now()}` : null;
req.body.postSlug = slug;
}
return post.updateAttributes(req.body);
})
.then((updatedPost) => {
res.json(updatedPost);

let termsPromises = [];

if (req.body.postTerms) {
post.setPostTerms();
req.body.postTerms.forEach((term) => {
let { termType, termName } = term;
term.termSlug = `${changeCase.paramCase(termType)}-${changeCase.paramCase(termName)}`;
let termReq = Term.findOrCreate({where: {termSlug: term.termSlug}, defaults: { termType, termName }})
.spread((term2) => {
post.addPostTerm(term2);
});
termsPromises.push(termReq);
});
}

post.updateAttributes(req.body);

Promise.all([
termsPromises
])
.then(() => {
return post.save();
})
.then((post2) => {
return res.json({ post: post2 });
})
.catch((err) => res.status(400).send({
error: err.message
}));
})
.catch((err) => res.status(400).send({
error: err.message
Expand Down
26 changes: 23 additions & 3 deletions test/post/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('[POST] /api/posts Testing', () => {
],
postRequest = {
postTitle: title,
postSlug: `${changeCase.paramCase(title)}-${Date.now()}`,
postType: faker.random.arrayElement(['post','page']),
postDate: new Date(),
postContent: faker.lorem.sentences(3,3),
Expand Down Expand Up @@ -91,6 +90,17 @@ describe('[POST] /api/posts Testing', () => {
});
});

it('should error with wrong post slug', (done) => {
request(app)
.get('/api/posts/no-post-here')
.expect(400)
.end((err, res) => {
expect(res.body).to.have.property('error');
expect(res.body).to.have.deep.property('error', 'No post found');
done();
});
});

it('should be able to create a post if logged in', (done) => {
request(app)
.post('/api/posts')
Expand Down Expand Up @@ -119,6 +129,17 @@ describe('[POST] /api/posts Testing', () => {
});
});

it('should error with wrong delete post slug', (done) => {
request(app)
.get(`/api/posts/${postSlug}`)
.expect(400)
.end((err, res) => {
expect(res.body).to.have.property('error');
expect(res.body).to.have.deep.property('error', 'No post found');
done();
});
});

it('it should reject post with no title', (done) => {
request(app)
.post('/api/posts')
Expand All @@ -128,7 +149,6 @@ describe('[POST] /api/posts Testing', () => {
.expect('Content-Type', /json/)
.expect(422)
.end((err, res) => {
console.log('***********************',res.body);
expect(res.body).to.have.property('error');
expect(res.body).to.have.deep.property('error', 'A postTitle is required.');
done();
Expand Down Expand Up @@ -157,6 +177,6 @@ describe('[POST] /api/posts Testing', () => {
expect(res.body).to.have.property('error');
expect(res.body).to.have.deep.property('error', 'All terms require a termType and termName.');
done();
})
});
});
});
38 changes: 17 additions & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-1.0.6.tgz#3e02972728c69248c2af08d60a48cbb8680fffdf"

"@types/node@*":
version "10.1.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.1.2.tgz#1b928a0baa408fc8ae3ac012cc81375addc147c6"
version "10.5.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.5.2.tgz#f19f05314d5421fe37e74153254201a7bf00a707"

abbrev@1:
version "1.1.1"
Expand Down Expand Up @@ -2471,14 +2471,14 @@ mocha@^3.2.0:
supports-color "3.1.2"

moment-timezone@^0.5.14:
version "0.5.17"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.17.tgz#3c8fef32051d84c3af174d91dc52977dcb0ad7e5"
version "0.5.21"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.21.tgz#3cba247d84492174dbf71de2a9848fa13207b845"
dependencies:
moment ">= 2.9.0"

"moment@>= 2.9.0", moment@^2.20.0:
version "2.22.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.22.1.tgz#529a2e9bf973f259c9643d237fda84de3a26e8ad"
version "2.22.2"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.22.2.tgz#3c257f9839fc0e93ff53149632239eb90783ff66"

morgan@^1.7.0:
version "1.9.0"
Expand Down Expand Up @@ -3342,9 +3342,9 @@ sequelize-cli@^4.0.0:
umzug "^2.1.0"
yargs "^8.0.2"

sequelize@^4.37.8:
version "4.37.8"
resolved "https://registry.yarnpkg.com/sequelize/-/sequelize-4.37.8.tgz#5c849b6bc0c2e1c0cf97dab9831ed5cafff51238"
sequelize@^4.38.0:
version "4.38.0"
resolved "https://registry.yarnpkg.com/sequelize/-/sequelize-4.38.0.tgz#330c1aa445d4e46b80a97d895603c01666cdc357"
dependencies:
bluebird "^3.5.0"
cls-bluebird "^2.1.0"
Expand All @@ -3361,7 +3361,7 @@ sequelize@^4.37.8:
terraformer-wkt-parser "^1.1.2"
toposort-class "^1.0.1"
uuid "^3.2.1"
validator "^9.4.1"
validator "^10.4.0"
wkx "^0.4.1"

serve-static@1.13.2:
Expand Down Expand Up @@ -3717,9 +3717,9 @@ terraformer-wkt-parser@^1.1.2:
terraformer "~1.0.5"

terraformer@~1.0.5:
version "1.0.8"
resolved "https://registry.yarnpkg.com/terraformer/-/terraformer-1.0.8.tgz#51e0ad89746fcf2161dc6f65aa70e42377c8b593"
dependencies:
version "1.0.9"
resolved "https://registry.yarnpkg.com/terraformer/-/terraformer-1.0.9.tgz#77851fef4a49c90b345dc53cf26809fdf29dcda6"
optionalDependencies:
"@types/geojson" "^1.0.0"

text-table@~0.2.0:
Expand Down Expand Up @@ -3950,24 +3950,20 @@ utils-merge@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"

uuid@^3.1.0:
uuid@^3.1.0, uuid@^3.2.1:
version "3.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131"

uuid@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14"

validate-npm-package-license@^3.0.1:
version "3.0.3"
resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.3.tgz#81643bcbef1bdfecd4623793dc4648948ba98338"
dependencies:
spdx-correct "^3.0.0"
spdx-expression-parse "^3.0.0"

validator@^9.4.1:
version "9.4.1"
resolved "https://registry.yarnpkg.com/validator/-/validator-9.4.1.tgz#abf466d398b561cd243050112c6ff1de6cc12663"
validator@^10.4.0:
version "10.4.0"
resolved "https://registry.yarnpkg.com/validator/-/validator-10.4.0.tgz#ee99a44afb3bb5ed350a159f056ca72a204cfc3c"

vary@^1, vary@~1.1.2:
version "1.1.2"
Expand Down

0 comments on commit 5a2e0f2

Please sign in to comment.