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..f2e0c6a 100644 --- a/src/controllers/posts.js +++ b/src/controllers/posts.js @@ -14,7 +14,8 @@ async function createNewPost(userId, title, body) { * showAllPosts({username: ''}) * showAllPosts({title: ''}) */ -async function findAllPosts(query) { +async function findAllPosts(query) { + // console.log("from controller post pooooooooooooo-:",typeof 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..92dce3f 100644 --- a/src/db/models.js +++ b/src/db/models.js @@ -5,6 +5,8 @@ if (process.env.NODE_ENV == 'testing') { db = new Sequelize({ dialect: 'sqlite', 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..f2078ef 100644 --- a/src/public/app/all-posts.js +++ b/src/public/app/all-posts.js @@ -1,25 +1,78 @@ 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-${p.id}`).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)=>{ + $(`#commentListId-${p.id}`).empty() + 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..1f540f8 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..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) 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 file mode 100644 index 0000000..8a61864 Binary files /dev/null and b/test/test.db differ