From 5a2e0f26a96a7880585fbafb9971e9a5c88cebef Mon Sep 17 00:00:00 2001 From: Ezra Keddell Date: Wed, 25 Jul 2018 08:52:39 +1200 Subject: [PATCH] update update post function --- index.js | 2 +- package.json | 2 +- seeders/posts.js | 2 +- server/api/post/controller.js | 78 +++++++++++++++++++++++------------ test/post/post.test.js | 26 ++++++++++-- yarn.lock | 38 ++++++++--------- 6 files changed, 95 insertions(+), 53 deletions(-) diff --git a/index.js b/index.js index 411a68b..720e027 100644 --- a/index.js +++ b/index.js @@ -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); }); diff --git a/package.json b/package.json index 988e219..5a78118 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/seeders/posts.js b/seeders/posts.js index 9bf82f5..94cf9ef 100644 --- a/seeders/posts.js +++ b/seeders/posts.js @@ -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), diff --git a/server/api/post/controller.js b/server/api/post/controller.js index 263b588..4563b5e 100644 --- a/server/api/post/controller.js +++ b/server/api/post/controller.js @@ -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 @@ -120,7 +112,14 @@ 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({ @@ -128,15 +127,42 @@ function updatePost(req, res) { }); } - // 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 diff --git a/test/post/post.test.js b/test/post/post.test.js index b2a289c..e8bdedb 100644 --- a/test/post/post.test.js +++ b/test/post/post.test.js @@ -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), @@ -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') @@ -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') @@ -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(); @@ -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(); - }) + }); }); }); diff --git a/yarn.lock b/yarn.lock index 4049f22..5603a47 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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: @@ -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: @@ -3950,14 +3950,10 @@ 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" @@ -3965,9 +3961,9 @@ validate-npm-package-license@^3.0.1: 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"