+
+ `)
+ )
+ }
+ for (let p of post[0].comments) {
+ $('#posts-container').append(
+ $(`
+
+
+
+
+
+ ${p.title}
+
+
+
${p.body}
+
+
+
+
+
+ `)
+ )
+ }
+ })
+ }
\ No newline at end of file
diff --git a/src/public/app/my-posts.js b/src/public/app/my-posts.js
index 9708598..309c445 100644
--- a/src/public/app/my-posts.js
+++ b/src/public/app/my-posts.js
@@ -14,7 +14,7 @@ function loadMyPosts() {
${p.body.substr(0, 200)}
...read more
- Comment
+ CommentLike
diff --git a/src/public/app/write-post.js b/src/public/app/write-post.js
index e92aa9c..ce49ecc 100644
--- a/src/public/app/write-post.js
+++ b/src/public/app/write-post.js
@@ -1,11 +1,12 @@
-$('#write-btn').click(() => {
+$("#write-btn").click(() => {
const userId = JSON.parse(window.localStorage.user).id
- const title = $('#p-title').val()
- const body = $('#p-body').val()
+ const title = $("#p-title").val()
+ const body = $("#p-body").val()
- $.post('/api/posts', { userId, title, body }, (data) => {
- $('#content').load('/components/my-posts.html')
- $('.nav-item .active').removeClass('active')
- $("[data-components='my-posts']").addClass('active')
+ $.post("/api/posts", { userId, title, body }, async (data) => {
+ // console.log(data)
+ await $("#content").load("/components/my-posts.html")
+ await $(".nav-item .active").removeClass("active")
+ await $("[data-components='my-posts']").addClass("active")
})
})
diff --git a/src/public/components/all-posts.html b/src/public/components/all-posts.html
index 242a75f..0d9209d 100644
--- a/src/public/components/all-posts.html
+++ b/src/public/components/all-posts.html
@@ -5,7 +5,7 @@
Recent Posts
-
+
\ No newline at end of file
diff --git a/src/public/components/comment.html b/src/public/components/comment.html
new file mode 100644
index 0000000..86cb5bd
--- /dev/null
+++ b/src/public/components/comment.html
@@ -0,0 +1,73 @@
+
+
+
+
+
+ Document
+
+
+
+
+
+
+
+
+
+
Post With Comments
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Write Comments
+
+
+
+
+
+
+
+
\ 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/routes/posts/comments.js b/src/routes/posts/comments.js
index 632f4c2..1c487f5 100644
--- a/src/routes/posts/comments.js
+++ b/src/routes/posts/comments.js
@@ -1,7 +1,37 @@
const { Router } = require('express')
+const {
+ createNewComment,findAllCommentsbyPosts
+} = require('../../controllers/comments')
+const path=require('path')
-const commentsRoute = Router()
+
+const route = Router()
+
+route.get('/:id', async (req, res) => {
+ const posts = await findAllCommentsbyPosts(req.params.id)
+ res.status(200).send(posts)
+})
+
+route.post('/', async (req, res) => {
+ // console.log("************")
+ // console.log(req.body)
+ // console.log("************")
+ const { postId, title, body } = req.body
+
+ if ((!postId) || (!title) || (!body)) {
+ return res.status(400).send({
+ error: 'Need postId, title and body to create post'
+ })
+ }
+
+ const post = await createNewComment(postId, title, body)
+ res.status(201).send(post)
+})
+route.get('/:id/endpoint', async (req,res)=>{
+ res.sendFile(path.join(__dirname,'../../public/components','comment.html'))
+ // /media/singla/New Volume/Github Projects/Mock-Message-Site/Social-Media-Project/src
+})
module.exports = {
- commentsRoute
+ commentsRoute:route
}
\ No newline at end of file
diff --git a/src/server.js b/src/server.js
index 5d06647..b91617e 100644
--- a/src/server.js
+++ b/src/server.js
@@ -3,6 +3,8 @@ 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.js')
+
const app = express()
app.use(express.json())
@@ -10,12 +12,13 @@ app.use(express.urlencoded({extended: true}))
app.use('/api/users', usersRoute)
app.use('/api/posts', postsRoute)
+app.use('/api/posts/comments', commentsRoute)
app.use('/', express.static(__dirname + '/public'))
db.sync()
.then(() => {
- app.listen(8383, () => {
- console.log('server started on http://localhost:8383')
+ app.listen(8483, () => {
+ console.log('server started on http://localhost:8483')
})
})
.catch((err) => {
diff --git a/terminal-test-run.txt b/terminal-test-run.txt
new file mode 100644
index 0000000..8c069e3
--- /dev/null
+++ b/terminal-test-run.txt
@@ -0,0 +1,188 @@
+singla@singla-Inspiron-3542:/media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May$ npm run test
+
+> social-media-project@1.0.0 test /media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May
+> mocha test/setup.js test/**/*.test.js
+
+
+
+Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `username` VARCHAR(30) NOT NULL UNIQUE, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
+Executing (default): PRAGMA INDEX_LIST(`users`)
+Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_users_1`)
+Executing (default): CREATE TABLE IF NOT EXISTS `posts` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE);
+Executing (default): PRAGMA INDEX_LIST(`posts`)
+(sequelize) Warning: SQLite does not support TEXT with options. Plain `TEXT` will be used instead.
+>> Check: https://www.sqlite.org/datatype3.html
+Executing (default): CREATE TABLE IF NOT EXISTS `comments` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, `postId` INTEGER REFERENCES `posts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE);
+Executing (default): PRAGMA INDEX_LIST(`comments`)
+ controllers/comments
+Executing (default): INSERT INTO `comments` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`postId`) VALUES (NULL,$1,$2,$3,$4,$5);
+ ✓ should create comment with postId, title and body assigned (221ms)
+Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `comments`.`id` AS `comments.id`, `comments`.`title` AS `comments.title`, `comments`.`body` AS `comments.body`, `comments`.`createdAt` AS `comments.createdAt`, `comments`.`updatedAt` AS `comments.updatedAt`, `comments`.`userId` AS `comments.userId`, `comments`.`postId` AS `comments.postId` FROM `posts` AS `post` LEFT OUTER JOIN `comments` AS `comments` ON `post`.`id` = `comments`.`postId` WHERE `post`.`id` = 1;
+ ✓ should find all commments by postId
+
+ controllers/posts
+Executing (default): INSERT INTO `posts` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`userId`) VALUES (NULL,$1,$2,$3,$4,$5);
+ ✓ should create post with userId, title and body assigned (197ms)
+Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`;
+ ✓ should find all posts
+Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`;
+ ✓ should find all posts of a user by its userid
+
+ controllers/users
+Executing (default): INSERT INTO `users` (`id`,`username`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3);
+ 1) should create anonymous user
+ 2) should find user by userid
+ ✓ should through error for non number userid
+ 3) should find user by username
+
+
+ 6 passing (572ms)
+ 3 failing
+
+ 1) controllers/users
+ should create anonymous user:
+ SequelizeUniqueConstraintError: Validation error
+ at Query.formatError (node_modules/sequelize/lib/dialects/sqlite/query.js:416:16)
+ at Query._handleQueryResponse (node_modules/sequelize/lib/dialects/sqlite/query.js:73:18)
+ at Statement.afterExecute (node_modules/sequelize/lib/dialects/sqlite/query.js:250:31)
+ at Statement.replacement (node_modules/sqlite3/lib/trace.js:19:31)
+
+ 2) controllers/users
+ should find user by userid:
+ TypeError: Cannot read property 'id' of null
+ at Context.it (test/controllers/users.test.js:21:51)
+
+ 3) controllers/users
+ should find user by username:
+ TypeError: Cannot read property 'username' of null
+ at Context.it (test/controllers/users.test.js:37:57)
+
+
+
+npm ERR! code ELIFECYCLE
+npm ERR! errno 3
+npm ERR! social-media-project@1.0.0 test: `mocha test/setup.js test/**/*.test.js`
+npm ERR! Exit status 3
+npm ERR!
+npm ERR! Failed at the social-media-project@1.0.0 test script.
+npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
+
+npm ERR! A complete log of this run can be found in:
+npm ERR! /home/singla/.npm/_logs/2020-05-30T06_48_24_509Z-debug.log
+
+
+singla@singla-Inspiron-3542:/media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May$ npm run test
+
+> social-media-project@1.0.0 test /media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May
+> mocha test/setup.js test/**/*.test.js
+
+
+
+Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `username` VARCHAR(30) NOT NULL UNIQUE, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
+Executing (default): PRAGMA INDEX_LIST(`users`)
+Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_users_1`)
+Executing (default): CREATE TABLE IF NOT EXISTS `posts` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE);
+Executing (default): PRAGMA INDEX_LIST(`posts`)
+(sequelize) Warning: SQLite does not support TEXT with options. Plain `TEXT` will be used instead.
+>> Check: https://www.sqlite.org/datatype3.html
+Executing (default): CREATE TABLE IF NOT EXISTS `comments` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, `postId` INTEGER REFERENCES `posts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE);
+Executing (default): PRAGMA INDEX_LIST(`comments`)
+ controllers/comments
+Executing (default): INSERT INTO `comments` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`postId`) VALUES (NULL,$1,$2,$3,$4,$5);
+ ✓ should create comment with postId, title and body assigned (259ms)
+Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `comments`.`id` AS `comments.id`, `comments`.`title` AS `comments.title`, `comments`.`body` AS `comments.body`, `comments`.`createdAt` AS `comments.createdAt`, `comments`.`updatedAt` AS `comments.updatedAt`, `comments`.`userId` AS `comments.userId`, `comments`.`postId` AS `comments.postId` FROM `posts` AS `post` LEFT OUTER JOIN `comments` AS `comments` ON `post`.`id` = `comments`.`postId` WHERE `post`.`id` = 1;
+ ✓ should find all commments by postId
+
+ controllers/posts
+Executing (default): INSERT INTO `posts` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`userId`) VALUES (NULL,$1,$2,$3,$4,$5);
+ ✓ should create post with userId, title and body assigned (185ms)
+Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`;
+ ✓ should find all posts
+Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`;
+ ✓ should find all posts of a user by its userid
+
+ controllers/users
+Executing (default): INSERT INTO `users` (`id`,`username`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3);
+ 1) should create anonymous user
+ 2) should find user by userid
+ ✓ should through error for non number userid
+ 3) should find user by username
+
+
+ 6 passing (607ms)
+ 3 failing
+
+ 1) controllers/users
+ should create anonymous user:
+ SequelizeUniqueConstraintError: Validation error
+ at Query.formatError (node_modules/sequelize/lib/dialects/sqlite/query.js:416:16)
+ at Query._handleQueryResponse (node_modules/sequelize/lib/dialects/sqlite/query.js:73:18)
+ at Statement.afterExecute (node_modules/sequelize/lib/dialects/sqlite/query.js:250:31)
+ at Statement.replacement (node_modules/sqlite3/lib/trace.js:19:31)
+
+ 2) controllers/users
+ should find user by userid:
+ TypeError: Cannot read property 'id' of null
+ at Context.it (test/controllers/users.test.js:21:51)
+
+ 3) controllers/users
+ should find user by username:
+ TypeError: Cannot read property 'username' of null
+ at Context.it (test/controllers/users.test.js:37:57)
+
+
+
+npm ERR! code ELIFECYCLE
+npm ERR! errno 3
+npm ERR! social-media-project@1.0.0 test: `mocha test/setup.js test/**/*.test.js`
+npm ERR! Exit status 3
+npm ERR!
+npm ERR! Failed at the social-media-project@1.0.0 test script.
+npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
+
+npm ERR! A complete log of this run can be found in:
+npm ERR! /home/singla/.npm/_logs/2020-05-30T06_48_36_130Z-debug.log
+
+
+
+singla@singla-Inspiron-3542:/media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May$ npm run test
+
+> social-media-project@1.0.0 test /media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May
+> mocha test/setup.js test/**/*.test.js
+
+
+
+Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `username` VARCHAR(30) NOT NULL UNIQUE, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
+Executing (default): PRAGMA INDEX_LIST(`users`)
+Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_users_1`)
+Executing (default): CREATE TABLE IF NOT EXISTS `posts` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE);
+Executing (default): PRAGMA INDEX_LIST(`posts`)
+(sequelize) Warning: SQLite does not support TEXT with options. Plain `TEXT` will be used instead.
+>> Check: https://www.sqlite.org/datatype3.html
+Executing (default): CREATE TABLE IF NOT EXISTS `comments` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, `postId` INTEGER REFERENCES `posts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE);
+Executing (default): PRAGMA INDEX_LIST(`comments`)
+ controllers/comments
+Executing (default): INSERT INTO `comments` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`postId`) VALUES (NULL,$1,$2,$3,$4,$5);
+ ✓ should create comment with postId, title and body assigned (233ms)
+Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `comments`.`id` AS `comments.id`, `comments`.`title` AS `comments.title`, `comments`.`body` AS `comments.body`, `comments`.`createdAt` AS `comments.createdAt`, `comments`.`updatedAt` AS `comments.updatedAt`, `comments`.`userId` AS `comments.userId`, `comments`.`postId` AS `comments.postId` FROM `posts` AS `post` LEFT OUTER JOIN `comments` AS `comments` ON `post`.`id` = `comments`.`postId` WHERE `post`.`id` = 1;
+ ✓ should find all commments by postId
+
+ controllers/posts
+Executing (default): INSERT INTO `posts` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`userId`) VALUES (NULL,$1,$2,$3,$4,$5);
+ ✓ should create post with userId, title and body assigned (180ms)
+Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`;
+ ✓ should find all posts
+Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`;
+ ✓ should find all posts of a user by its userid
+
+ controllers/users
+Executing (default): INSERT INTO `users` (`id`,`username`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3);
+ ✓ should create anonymous user (174ms)
+Executing (default): SELECT `id`, `username`, `createdAt`, `updatedAt` FROM `users` AS `user` WHERE `user`.`id` = 43;
+ ✓ should find user by userid
+ ✓ should through error for non number userid
+Executing (default): SELECT `id`, `username`, `createdAt`, `updatedAt` FROM `users` AS `user` WHERE `user`.`username` = 'slim-chalk';
+ ✓ should find user by username
+
+
+ 9 passing (748ms)
\ No newline at end of file
diff --git a/test/controllers/comments.test.js b/test/controllers/comments.test.js
new file mode 100644
index 0000000..1fe8a33
--- /dev/null
+++ b/test/controllers/comments.test.js
@@ -0,0 +1,21 @@
+const { expect } = require('chai')
+const {
+ createNewComment,
+ findAllCommentsbyPosts
+} = require('../../src/controllers/comments')
+
+describe('controllers/comments',()=>{
+ let testingComment = null
+
+ it.only('should create comment with postId, title and body assigned', async ()=>{
+ testingComment = await createNewComment(1, 'Coder', 'Arnav Bhaiya')
+ expect(testingComment.postId).to.equal(1)
+ expect(testingComment.title).to.equal('Coder')
+ expect(testingComment.body).to.equal('Arnav Bhaiya')
+ } )
+ it('should find all commments by postId', async ()=>{
+ let where ={postId:1}
+ let allComments = await findAllCommentsbyPosts(where.postId)
+ expect(allComments).to.be.an("array")
+ })
+})
\ 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..0bc2f50
--- /dev/null
+++ b/test/controllers/posts.test.js
@@ -0,0 +1,30 @@
+const {expect}=require('chai')
+const {
+ createNewPost,
+ findAllPosts
+}=require('../../src/controllers/posts')
+
+describe('controllers/posts', ()=>{
+ let testingPost = null
+
+ it('should create post with userId, title and body assigned', async ()=>{
+ testingPost = await createNewPost(1, 'Coder', 'Arnav Bhaiya')
+ expect(testingPost.userId).to.equal(1)
+ expect(testingPost.title).to.equal('Coder')
+ expect(testingPost.body).to.equal('Arnav Bhaiya')
+ } )
+
+ it('should find all posts', async ()=>{
+ let where ={}
+ let allPosts = await findAllPosts(where)
+ expect(allPosts).to.be.an("array")
+ })
+
+ it('should find all posts of a user by its userid', async ()=>{
+ let where ={
+ userId: 1
+ }
+ let allPosts = await findAllPosts(where)
+ expect(allPosts).to.be.an("array")
+ })
+})
\ No newline at end of file
diff --git a/test/setup.js b/test/setup.js
index adb4f2d..d77d164 100644
--- a/test/setup.js
+++ b/test/setup.js
@@ -6,5 +6,5 @@ const chai = require('chai')
chai.use(require('chai-as-promised'))
before(async () => {
- await db.sync()
+ await db.sync({force:true})
})
\ No newline at end of file