From 74e64eb0279c52b4c61ea4a3b85bd06d42af2c17 Mon Sep 17 00:00:00 2001 From: Deepak Gupta Date: Tue, 26 May 2020 09:40:12 +0530 Subject: [PATCH 1/2] completed with test --- src/controllers/comments.js | 44 ++++++++++++++++++ src/controllers/posts.js | 5 ++- src/db/models.js | 4 +- src/public/app/all-posts.js | 64 ++++++++++++++++++++++++--- src/public/app/common.css | 26 ++++++++++- src/public/app/my-posts.js | 4 +- src/public/components/all-posts.html | 3 +- src/public/components/my-posts.html | 2 +- src/public/index.html | 24 +++++++++- src/routes/posts/comments.js | 20 ++++++++- src/routes/posts/index.js | 2 +- src/server.js | 2 + test/controllers/comments.test.js | 33 ++++++++++++++ test/controllers/posts.test.js | 44 ++++++++++++++++++ test/controllers/users.test.js | 2 - test/test.db | Bin 0 -> 40960 bytes 16 files changed, 259 insertions(+), 20 deletions(-) create mode 100644 test/controllers/comments.test.js create mode 100644 test/controllers/posts.test.js create mode 100644 test/test.db diff --git a/src/controllers/comments.js b/src/controllers/comments.js index e69de29..536ffc6 100644 --- a/src/controllers/comments.js +++ b/src/controllers/comments.js @@ -0,0 +1,44 @@ +const {Comments, Posts, Users} = require('../db/models') + +async function createComment(postId, userId, title, body){ + return await Comments.create({ + postId, + userId, + title, + body + }) +} + +async function findAllComments(query){ + const postId = query.postId + return await Comments.findAll({ + include: [ Users], + where:{postId} + }) +} +module.exports = { + createComment, + findAllComments +} +/* +async function test(){ + console.log( + await createComment( + 1, + 1, + 'gogo', + 'tech is awosome' + ) + ) + +} +test()*/ +/* +async function test(){ + const all = await findAllComments(1) + all.forEach(t=>{ + console.log(`comment id: ${t.id} title: ${t.user.username} body: ${t.body}`) + }) +} +test() +*/ \ No newline at end of file diff --git a/src/controllers/posts.js b/src/controllers/posts.js index 8b87710..e921a47 100644 --- a/src/controllers/posts.js +++ b/src/controllers/posts.js @@ -15,6 +15,7 @@ async function createNewPost(userId, title, body) { * showAllPosts({title: ''}) */ async function findAllPosts(query) { + // console.log("from controller post pooooooooooooo-:",query) let where = {} if (query.userId) { where.userId = query.userId } @@ -48,11 +49,11 @@ async function task() { // 'Some body example here as well' // ) // ) - const posts = await showAllPosts() + const posts = await findAllPosts(1) for (let p of posts) { console.log(`${p.title}\nauthor: ${p.user.username}\n${p.body}\n==========\n`) } } task() -*/ \ No newline at end of file +*/ diff --git a/src/db/models.js b/src/db/models.js index dd5870c..8abdb28 100644 --- a/src/db/models.js +++ b/src/db/models.js @@ -4,7 +4,9 @@ let db if (process.env.NODE_ENV == 'testing') { db = new Sequelize({ dialect: 'sqlite', - storage: ':memory:', + // storage: ':memory:', + storage: __dirname+'/../../test/test.db' + }) } else { db = new Sequelize({ diff --git a/src/public/app/all-posts.js b/src/public/app/all-posts.js index 04aa02d..f9bf4c9 100644 --- a/src/public/app/all-posts.js +++ b/src/public/app/all-posts.js @@ -1,25 +1,77 @@ function loadPosts() { + const user = JSON.parse(window.localStorage.user) + let display='show' $.get('/api/posts', (posts) => { for (let p of posts) { $('#posts-container').append( $(`
-
+
${p.title}
${p.user.username}

${p.body.substr(0, 200)} ...read more -

- Comment - Like +

+ +
+ + + +
+
+
- `) ) + /*handle post req*/ + $(`#${p.id}`).click(()=>{ + const postId = p.id + const userId = user.id + const title = user.username + const body = $('#comment').val() + + $.post('/api/comments', {postId, userId, title, body}, (data)=>{ + console.log('ok sent', data.postId) + }) + }) + /*handle get req*/ + function comments(){ + $.get(`/api/comments?postId=${p.id}`, (comments)=>{ + for(let c of comments){ + $(`#commentListId-${p.id}`).append( + $(` +
+
+
${c.user.username}
+

${c.body}

+
+
+ `) + ) + } + }) + } + /*Handling button click event*/ + $(`#${display}-${p.id}`).click(()=>{ + if(display==='show'){ + comments() + $(`#commentListId-${p.id}`).show() + // console.log($(`#${display}-${p.id}`).text('hide')) + $(`#${display}-${p.id}`).text('hide') + display='hide' + } + else if(display==='hide'){ + $(`#${display}-${p.id}`).text('show') + $(`#commentListId-${p.id}`).hide() + display='show' + } + }) + /*end get req*/ } + /*for loop end here*/ }) -} +} \ No newline at end of file diff --git a/src/public/app/common.css b/src/public/app/common.css index 239ced6..5155aed 100644 --- a/src/public/app/common.css +++ b/src/public/app/common.css @@ -10,6 +10,30 @@ bottom: 0px; background-color: var(--light); } - +/* #inlineFormInputName2{ + width: 100%; +} */ +.btn-sm{ + font-size: 0.75rem; +} +#comment{ + font-size: 0.8rem; + height: 1.60rem; + width: 70%; +} +#comment-card{ + font-size: 0.75rem; + padding: 0; +} +#comment-card .card-body{ + padding:0.3rem; +} +#comment-card .card-body h6{ + padding-top: 0.3rem; + font-size: 0.75rem; +} +#card{ + padding-bottom: 0; +} #content { } \ No newline at end of file diff --git a/src/public/app/my-posts.js b/src/public/app/my-posts.js index 9708598..3a80012 100644 --- a/src/public/app/my-posts.js +++ b/src/public/app/my-posts.js @@ -1,6 +1,6 @@ function loadMyPosts() { const userId = JSON.parse(window.localStorage.user).id - + console.log(userId) $.get(`/api/posts?userId=${userId}`, (posts) => { for (let p of posts) { $('#posts-container').append( @@ -19,7 +19,7 @@ function loadMyPosts() {
- + `) ) } diff --git a/src/public/components/all-posts.html b/src/public/components/all-posts.html index 242a75f..ef060b4 100644 --- a/src/public/components/all-posts.html +++ b/src/public/components/all-posts.html @@ -1,11 +1,10 @@

Recent Posts

-
- + \ No newline at end of file diff --git a/src/public/components/my-posts.html b/src/public/components/my-posts.html index 127bc0b..5657e12 100644 --- a/src/public/components/my-posts.html +++ b/src/public/components/my-posts.html @@ -5,7 +5,7 @@

Recent Posts

- + \ No newline at end of file diff --git a/src/public/index.html b/src/public/index.html index a61f81f..f4b4584 100644 --- a/src/public/index.html +++ b/src/public/index.html @@ -19,4 +19,26 @@
- \ No newline at end of file + + + + + + diff --git a/src/routes/posts/comments.js b/src/routes/posts/comments.js index 632f4c2..4365c40 100644 --- a/src/routes/posts/comments.js +++ b/src/routes/posts/comments.js @@ -1,7 +1,25 @@ const { Router } = require('express') +const {findAllComments, createComment} = require('../../controllers/comments') const commentsRoute = Router() - +//post req router +commentsRoute.post('/', async (req,res)=>{ + console.log(`POST /api/comments`, req.body) + const {postId, userId, title, body} = req.body + if(!postId || !userId ||!title ||!body){ + return res.status(400).send({ + error: 'Need userid, title and body to create post' + }) + } + const comment = await createComment(postId, userId, title, body) + res.status(201).send(comment) +}) +//get req router to find all comments +commentsRoute.get('/', async (req, res)=>{ + const comments = await findAllComments(req.query) + res.status(200).send(comments) +}) +//export module.exports = { commentsRoute } \ No newline at end of file diff --git a/src/routes/posts/index.js b/src/routes/posts/index.js index aa107c2..08afe08 100644 --- a/src/routes/posts/index.js +++ b/src/routes/posts/index.js @@ -8,7 +8,7 @@ const route = Router() route.get('/', async (req, res) => { const posts = await findAllPosts(req.query) - + // console.log("from router post pooooooooooooo-:",req.query==) res.status(200).send(posts) }) diff --git a/src/server.js b/src/server.js index 5d06647..2927f46 100644 --- a/src/server.js +++ b/src/server.js @@ -3,6 +3,7 @@ const express = require('express') const { db } = require('./db/models') const { usersRoute } = require('./routes/users') const { postsRoute } = require('./routes/posts') +const { commentsRoute } = require('./routes/posts/comments') const app = express() app.use(express.json()) @@ -10,6 +11,7 @@ app.use(express.urlencoded({extended: true})) app.use('/api/users', usersRoute) app.use('/api/posts', postsRoute) +app.use('/api/comments', commentsRoute) app.use('/', express.static(__dirname + '/public')) db.sync() diff --git a/test/controllers/comments.test.js b/test/controllers/comments.test.js new file mode 100644 index 0000000..c92d9a8 --- /dev/null +++ b/test/controllers/comments.test.js @@ -0,0 +1,33 @@ +const {expect} = require('chai') +const {findAllComments, createComment} = require('../../src/controllers/comments') + +describe('controllers/comments', ()=>{ + let createdComment = null + it('should create comment with postId, userId, title, body', async ()=>{ + createdComment = await createComment(1, 1, 'testing comment title', 'testing comment body') + expect(createdComment).to.have.property('title') + expect(createdComment).to.have.property('body') + expect(createdComment).to.have.property('postId') + expect(createdComment).to.have.property('userId') + expect(createdComment.id).to.be.a('number') + expect(createdComment.userId).to.be.a('number') + expect(createdComment.postId).to.be.a('number') + expect(createdComment.title).to.be.a('string') + expect(createdComment.body).to.be.a('string') + }) + + it('should find all comment by postId', async ()=>{ + let query ={ + postId: createdComment.postId + } + let allComments = await findAllComments(query) + let arrObj = [] + for(let p of allComments){ + arrObj.push(p) + } + let len=arrObj.length + expect(arrObj[len-1].title).to.equal(createdComment.title) + expect(arrObj[len-1].body).to.equal(createdComment.body) + + }) +}) \ No newline at end of file diff --git a/test/controllers/posts.test.js b/test/controllers/posts.test.js new file mode 100644 index 0000000..db90d70 --- /dev/null +++ b/test/controllers/posts.test.js @@ -0,0 +1,44 @@ +const { expect } = require('chai') +const { + createNewPost, + findAllPosts +} = require('../../src/controllers/posts') +describe('controllers/posts', ()=>{ + let createdPost = null + + it('should create post with userId, title, body parameter', async ()=>{ + createdPost = await createNewPost(1, 'testing title', 'testing body') + expect(createdPost).to.have.property('title') + expect(createdPost).to.have.property('body') + expect(createdPost.id).to.be.a('number') + expect(createdPost.userId).to.be.a('number') + expect(createdPost.title).to.be.a('string') + expect(createdPost.body).to.be.a('string') + } ) + + it('should find all posts', async ()=>{ + let query ={} + let allPosts = await findAllPosts(query) + let arrObj = [] + for(let p of allPosts){ + arrObj.push(p) + } + let len=arrObj.length + expect(arrObj[len-1].title).to.equal(createdPost.title) + expect(arrObj[len-1].body).to.equal(createdPost.body) + }) + + it('should find all posts by userid', async ()=>{ + let query ={ + userId: createdPost.userId + } + let allPosts = await findAllPosts(query.userId) + let arrObj = [] + for(let p of allPosts){ + arrObj.push(p) + } + let len=arrObj.length + expect(arrObj[len-1].title).to.equal(createdPost.title) + expect(arrObj[len-1].body).to.equal(createdPost.body) + }) +}) \ No newline at end of file diff --git a/test/controllers/users.test.js b/test/controllers/users.test.js index 265e52c..f00e28f 100644 --- a/test/controllers/users.test.js +++ b/test/controllers/users.test.js @@ -7,9 +7,7 @@ const { describe('controllers/users', () => { let createdUser = null - it('should create anonymous user', async () => { - createdUser = await createAnonUser() expect(createdUser).to.have.property('username') expect(createdUser.id).to.be.a('number') diff --git a/test/test.db b/test/test.db new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sK0zyqamIO-2a++gtgbg)Q-bITj9nEVF0t*NXq!Y|wQlfy; zdVX^dX#|$Ms`TQJ0ZK>mn&Xpz9jRN}p9DNWX)UigKJRxhk2)FMvU$4@oj%By(CRUm zvkY50C8$45c!1JMeseHy!As9O;C?1tT>_=$yyo~+VficTaCHJoOL@)lxx-Gz@M;Mt zE#@`Hr~A5D;l6<8oc|AUvFSdBEuCSTMtkf3orU-R8)s92JDxxVPyti`6+i`00aO4L xKm||%Q~(t~1^#aopyzYN|NIFqOdIv2(=5^`J(u4c%oKIgZ$?1whMP;E{|1Pj;t2o% literal 0 HcmV?d00001 From adee4c8c95776b129c62e6da33b9b228b967da53 Mon Sep 17 00:00:00 2001 From: Deepak Gupta Date: Sat, 30 May 2020 17:20:30 +0530 Subject: [PATCH 2/2] doubt resoloved by praveen --- src/controllers/posts.js | 4 ++-- src/db/models.js | 4 ++-- src/public/app/all-posts.js | 5 +++-- src/routes/posts/comments.js | 2 +- src/routes/posts/index.js | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/controllers/posts.js b/src/controllers/posts.js index e921a47..f2e0c6a 100644 --- a/src/controllers/posts.js +++ b/src/controllers/posts.js @@ -14,8 +14,8 @@ async function createNewPost(userId, title, body) { * showAllPosts({username: ''}) * showAllPosts({title: ''}) */ -async function findAllPosts(query) { - // console.log("from controller post pooooooooooooo-:",query) +async function findAllPosts(query) { + // console.log("from controller post pooooooooooooo-:",typeof query) let where = {} if (query.userId) { where.userId = query.userId } diff --git a/src/db/models.js b/src/db/models.js index 8abdb28..92dce3f 100644 --- a/src/db/models.js +++ b/src/db/models.js @@ -4,8 +4,8 @@ let db if (process.env.NODE_ENV == 'testing') { db = new Sequelize({ dialect: 'sqlite', - // storage: ':memory:', - storage: __dirname+'/../../test/test.db' + storage: ':memory:', + // storage: __dirname+'/../../test/test.db' }) } else { diff --git a/src/public/app/all-posts.js b/src/public/app/all-posts.js index f9bf4c9..f2078ef 100644 --- a/src/public/app/all-posts.js +++ b/src/public/app/all-posts.js @@ -16,7 +16,7 @@ function loadPosts() {

- +
@@ -32,7 +32,7 @@ function loadPosts() { const postId = p.id const userId = user.id const title = user.username - const body = $('#comment').val() + const body = $(`#comment-${p.id}`).val() $.post('/api/comments', {postId, userId, title, body}, (data)=>{ console.log('ok sent', data.postId) @@ -41,6 +41,7 @@ function loadPosts() { /*handle get req*/ function comments(){ $.get(`/api/comments?postId=${p.id}`, (comments)=>{ + $(`#commentListId-${p.id}`).empty() for(let c of comments){ $(`#commentListId-${p.id}`).append( $(` diff --git a/src/routes/posts/comments.js b/src/routes/posts/comments.js index 4365c40..1f540f8 100644 --- a/src/routes/posts/comments.js +++ b/src/routes/posts/comments.js @@ -4,7 +4,7 @@ const {findAllComments, createComment} = require('../../controllers/comments') const commentsRoute = Router() //post req router commentsRoute.post('/', async (req,res)=>{ - console.log(`POST /api/comments`, req.body) + // console.log(`POST /api/comments`, req.body) const {postId, userId, title, body} = req.body if(!postId || !userId ||!title ||!body){ return res.status(400).send({ diff --git a/src/routes/posts/index.js b/src/routes/posts/index.js index 08afe08..a59113f 100644 --- a/src/routes/posts/index.js +++ b/src/routes/posts/index.js @@ -8,7 +8,7 @@ const route = Router() route.get('/', async (req, res) => { const posts = await findAllPosts(req.query) - // console.log("from router post pooooooooooooo-:",req.query==) + // console.log("from router post pooooooooooooo-:",req.query) res.status(200).send(posts) })