From 720813395c524667e347a08e520ba38cac2f75ad Mon Sep 17 00:00:00 2001 From: 99Nistha Date: Thu, 28 May 2020 22:09:04 +0530 Subject: [PATCH 01/10] organising package.json file --- package.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 9ad48f4..3e5c5a0 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "social-media-project", "version": "1.0.0", "description": "", - "main": "index.js", + "main": "server.js", "scripts": { "start": "node src/server.js", "test": "mocha test/setup.js test/**/*.test.js", @@ -14,9 +14,7 @@ "express": "^4.17.1", "mysql2": "^2.1.0", "sequelize": "^5.21.7", - "sqlite3": "^4.2.0" - }, - "devDependencies": { + "sqlite3": "^4.2.0", "chai": "^4.2.0", "chai-as-promised": "^7.1.1", "mocha": "^7.2.0", From 21692d52bb9d195934aa1e623a621f4aaa599654 Mon Sep 17 00:00:00 2001 From: 99Nistha Date: Fri, 29 May 2020 12:18:50 +0530 Subject: [PATCH 02/10] test for posts and comments --- src/controllers/comments.js | 23 +++++++++++++ src/controllers/posts.js | 48 ++++------------------------ src/db/models.js | 12 +++---- src/public/app/all-posts.js | 39 ++++++++++++++++------ src/public/components/all-posts.html | 2 +- src/public/components/my-posts.html | 2 +- src/routes/posts/comments.js | 45 ++++++++++++++++++++++++-- src/server.js | 2 ++ test/setup.js | 2 +- 9 files changed, 112 insertions(+), 63 deletions(-) diff --git a/src/controllers/comments.js b/src/controllers/comments.js index e69de29..3ea8ea4 100644 --- a/src/controllers/comments.js +++ b/src/controllers/comments.js @@ -0,0 +1,23 @@ +const { Posts, Users, Comments } = require("../db/models"); + +async function createComment(user_id, post_id, comment_body) { + try { + let user = await Users.findOne({ + where: { + id: user_id, + }, + }); + return await Comments.create({ + body: comment_body, + title: user.username, + userId: user_id, + postId: post_id, + }); + } catch (err) { + console.log(err); + return null; + } +} +module.exports = { + createComment +}; \ No newline at end of file diff --git a/src/controllers/posts.js b/src/controllers/posts.js index 8b87710..7027bf2 100644 --- a/src/controllers/posts.js +++ b/src/controllers/posts.js @@ -1,58 +1,24 @@ -const { Posts, Users } = require('../db/models') +const { Posts, Users, Comments} = require('../db/models') async function createNewPost(userId, title, body) { - const post = await Posts.create({ + return await Posts.create({ title, body, userId, - }) - - return post + }); } -/** - * showAllPosts({username: ''}) - * showAllPosts({title: ''}) - */ async function findAllPosts(query) { let where = {} if (query.userId) { where.userId = query.userId } - const posts = await Posts.findAll({ - include: [ Users ], + return await Posts.findAll({ + include: [ Users, Comments ], where - }) - - return posts + }); } module.exports = { createNewPost, findAllPosts -} - -/* Test Code */ -/* -async function task() { - // console.log( - // await createNewPost( - // 1, - // 'This is a sample post', - // 'Body of the post goes here' - // ) - // ), - // console.log( - // await createNewPost( - // 2, - // 'Another sample post', - // 'Some body example here as well' - // ) - // ) - const posts = await showAllPosts() - 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 +} \ No newline at end of file diff --git a/src/db/models.js b/src/db/models.js index dd5870c..e9273a5 100644 --- a/src/db/models.js +++ b/src/db/models.js @@ -6,14 +6,14 @@ if (process.env.NODE_ENV == 'testing') { dialect: 'sqlite', storage: ':memory:', }) -} else { +} /*else { db = new Sequelize({ - dialect: 'mysql', - database: 'cbsocialmediadb', - username: 'cbsocialuser', - password: 'cbsocialpass', + dialect:'mysql', + database: 'cbSocialMediaDb', + username: 'cbSocialMediaUser', + password: 'Social_1' }) -} +}*/ const COL_ID_DEF = { type: Sequelize.DataTypes.INTEGER, diff --git a/src/public/app/all-posts.js b/src/public/app/all-posts.js index 04aa02d..6ee04da 100644 --- a/src/public/app/all-posts.js +++ b/src/public/app/all-posts.js @@ -1,8 +1,7 @@ function loadPosts() { - $.get('/api/posts', (posts) => { + $.get("/api/posts", (posts) => { for (let p of posts) { - $('#posts-container').append( - $(` + let item = $(`
@@ -12,14 +11,34 @@ function loadPosts() { ${p.body.substr(0, 200)} ...read more

- Comment - Like + + +
    "}.text(`[${comment.title}]:${comment.body}`) + ); + } + + item.find(".btnComment").on("click",()=>{ + $.post( + "/api/commments", + { + post_id:p.id, + comment_body: item.find(".newComment").val(), + }, + (comment)=>{ + $("#content").load('/components/all-posts.html'); + } + ); + }); + $("#posts-container").append(item); } - }) + }); } 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/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..4dee6d1 100644 --- a/src/routes/posts/comments.js +++ b/src/routes/posts/comments.js @@ -1,7 +1,46 @@ -const { Router } = require('express') +const route = require('express').Router() +const {createComment} = require("../../controllers/comments"); -const commentsRoute = Router() +//create comments +route.post("/", async (req, res) => { + try { + let { user_id, post_id, comment_body } = req.body; + if (!user_id || !post_id || !comment_body) { + res.status(403).send("Bad Request"); + } else { + console.log("============================================="); + console.log("going to create comment"); + console.log(user_id, post_id, comment_body); + console.log("============================================="); + + let comment = await createComment(user_id, post_id, comment_body); + + if (comment) { + res.status(201).send(comment); + } else { + res.status(501).send("not created Please try again"); + } + } + } catch (e) { + res.status(501).send(e); + } +}); + +// get all comments of a post id +route.get("/:post_id", async (req, res) => { + try { + let allComments = await Comments.findAll({ + where: { + postId: req.params.post_id, + }, + }); + res.status(200).send(allComments); + } catch (e) { + console.log(e); + res.status(404).send("Not Found"); + } +}); module.exports = { - commentsRoute + commentsRoute: route } \ No newline at end of file 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/setup.js b/test/setup.js index adb4f2d..583f8ec 100644 --- a/test/setup.js +++ b/test/setup.js @@ -1,5 +1,5 @@ // setup testing environment before requiring anything -process.env.NODE_ENV = 'testing' +//process.env.NODE_ENV = 'testing' const { db } = require('../src/db/models') const chai = require('chai') From 94cd2db0637dd667f44648fa120eb6bb32837db6 Mon Sep 17 00:00:00 2001 From: 99Nistha Date: Fri, 29 May 2020 15:45:26 +0530 Subject: [PATCH 03/10] test for posts and commnets --- src/db/models.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/db/models.js b/src/db/models.js index e9273a5..56242c4 100644 --- a/src/db/models.js +++ b/src/db/models.js @@ -6,14 +6,14 @@ if (process.env.NODE_ENV == 'testing') { dialect: 'sqlite', storage: ':memory:', }) -} /*else { +} else { db = new Sequelize({ dialect:'mysql', database: 'cbSocialMediaDb', username: 'cbSocialMediaUser', password: 'Social_1' }) -}*/ +} const COL_ID_DEF = { type: Sequelize.DataTypes.INTEGER, From 67cb5bdd5a0415a6a2578b939d599b95fd00e78c Mon Sep 17 00:00:00 2001 From: 99Nistha Date: Fri, 29 May 2020 16:20:06 +0530 Subject: [PATCH 04/10] test for posts and commnets --- src/db/models.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/db/models.js b/src/db/models.js index 56242c4..1542ab4 100644 --- a/src/db/models.js +++ b/src/db/models.js @@ -8,10 +8,10 @@ if (process.env.NODE_ENV == 'testing') { }) } else { db = new Sequelize({ - dialect:'mysql', - database: 'cbSocialMediaDb', - username: 'cbSocialMediaUser', - password: 'Social_1' + dialect: 'mysql', + database: 'cbsocialmediadb', + username: 'cbsocialuser', + password: 'cbsocialpass' }) } From e47bcb67c6bf0cb8131607a0ac340c27a6fb844b Mon Sep 17 00:00:00 2001 From: 99Nistha Date: Fri, 29 May 2020 17:10:11 +0530 Subject: [PATCH 05/10] organising my-post.js file --- src/db/models.js | 8 ++++---- src/public/app/my-posts.js | 33 ++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/db/models.js b/src/db/models.js index 1542ab4..56242c4 100644 --- a/src/db/models.js +++ b/src/db/models.js @@ -8,10 +8,10 @@ if (process.env.NODE_ENV == 'testing') { }) } else { db = new Sequelize({ - dialect: 'mysql', - database: 'cbsocialmediadb', - username: 'cbsocialuser', - password: 'cbsocialpass' + dialect:'mysql', + database: 'cbSocialMediaDb', + username: 'cbSocialMediaUser', + password: 'Social_1' }) } diff --git a/src/public/app/my-posts.js b/src/public/app/my-posts.js index 9708598..ee5832f 100644 --- a/src/public/app/my-posts.js +++ b/src/public/app/my-posts.js @@ -3,8 +3,7 @@ function loadMyPosts() { $.get(`/api/posts?userId=${userId}`, (posts) => { for (let p of posts) { - $('#posts-container').append( - $(` + let item = $(`
@@ -14,14 +13,34 @@ function loadMyPosts() { ${p.body.substr(0, 200)} ...read more

- Comment - Like + + +
    - - `) - ) + `); + let commentBox = item.find(".comment"); + for (let comment of p.comments) { + commentBox.append( + $("
  • ").text(`[${comment.title}] : ${comment.body}`) + ); + } + + item.find(".btnComment").on("click", () => { + $.post( + "/api/comments", + { + post_id: p.id, + comment_body: item.find(".newComment").val(), + user_id: JSON.parse(window.localStorage.user).id, + }, + (comment) => { + $("#content").load(`/components/my-posts.html`); + } + ); + }); + $("#posts-container").append(item); } }) } From 40f99d57885fab34540b895b1eeac59b223644db Mon Sep 17 00:00:00 2001 From: 99Nistha Date: Fri, 29 May 2020 17:25:27 +0530 Subject: [PATCH 06/10] adding test for post and comment controller --- src/controllers/comments.js | 5 ----- src/controllers/users.js | 4 ++-- src/db/models.js | 14 ++++++-------- src/db/socialmedia.db | Bin 0 -> 24576 bytes src/db/test.db | Bin 0 -> 24576 bytes test/controllers/comments.test.js | 11 +++++++++++ test/controllers/posts.test.js | 18 ++++++++++++++++++ test/setup.js | 2 +- 8 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 src/db/socialmedia.db create mode 100644 src/db/test.db create mode 100644 test/controllers/comments.test.js create mode 100644 test/controllers/posts.test.js diff --git a/src/controllers/comments.js b/src/controllers/comments.js index 3ea8ea4..e5af768 100644 --- a/src/controllers/comments.js +++ b/src/controllers/comments.js @@ -1,7 +1,6 @@ const { Posts, Users, Comments } = require("../db/models"); async function createComment(user_id, post_id, comment_body) { - try { let user = await Users.findOne({ where: { id: user_id, @@ -13,11 +12,7 @@ async function createComment(user_id, post_id, comment_body) { userId: user_id, postId: post_id, }); - } catch (err) { - console.log(err); - return null; } -} module.exports = { createComment }; \ No newline at end of file diff --git a/src/controllers/users.js b/src/controllers/users.js index 1b52937..3b433df 100644 --- a/src/controllers/users.js +++ b/src/controllers/users.js @@ -2,9 +2,9 @@ const { Users } = require('../db/models') const { genRandomUsername } = require('../utils/username') async function createAnonUser() { - const user = await Users.create({ + return await Users.create({ username: genRandomUsername(), - }) + }); return user } diff --git a/src/db/models.js b/src/db/models.js index 56242c4..8f03321 100644 --- a/src/db/models.js +++ b/src/db/models.js @@ -3,16 +3,14 @@ const Sequelize = require('sequelize') let db if (process.env.NODE_ENV == 'testing') { db = new Sequelize({ - dialect: 'sqlite', - storage: ':memory:', - }) + dialect: "sqlite", + storage: __dirname + "/test.db", + }); } else { db = new Sequelize({ - dialect:'mysql', - database: 'cbSocialMediaDb', - username: 'cbSocialMediaUser', - password: 'Social_1' - }) + dialect: "sqlite", + storage: __dirname + "/socialmedia.db", + }); } const COL_ID_DEF = { diff --git a/src/db/socialmedia.db b/src/db/socialmedia.db new file mode 100644 index 0000000000000000000000000000000000000000..f16b0417b70e49f27bf287ecf77b6e0e8a513e96 GIT binary patch literal 24576 zcmeI%&5F}N0KoBN`>`U{_7*P-a}gSL5tkJNK{U3b7TdI&reaS`w~bKf$8M8?9$oZF zd=KBi2k=ciIx}uDU06}{pyfZ1`N(9xelrw?c8{BrG#0NH%h@m$M^?qMZR@cRmSyGC zQ&i77=yHBtP^q2CUzN>ScOHEymA_hr8~3eprTqEkr_x82B_MzR0tg_000IagfWZGF za6T#QI(vKehui7!WEzhavspY(lXY6G1=0(p2)!pwDWdi3NK~WAI1&v%l+R=!Izgl5 z1%2^C_J!9A+YMi}Ye_#m5K%fwr*R}+dO_{E7gP`L-F1cE4u#)qHdXb>V*EA|p?syk z9WCQw8jrm+5_L6f*l5Y@x2x0fPpgx7*~rW)kWXbG{hI8GXr=0si)p)}?Tfl>O0}@A z{5})?s@KtjikjE0d3C9#bGk^%LIe;%009ILKmY**5I_I{ c1i1fm1`t310R#|0009ILKmY**5ZHc!Z{~HV%7zf~U?6d@Euj0!rNVimK6cRZfr6dvplOABR z3jPvy2G|%_VPS`n9Y!QZ&Ms9QleQojTKco(`0UsB`tGq6$(GOcC9~r>>|TF;wdJuq znIe)zt}sRjIVR>YF>g(>C@5PiVkw2nI~9+S%;irL^gD?kpCfdNewcha@j?_aK>z{} zfB*y_009U<00IX`V51WsSI?c3o=ev=DDh+;}*I2b?EacmB&#eo zSfy#2B7C=RKe1SY-}d)yuRATzv31X4^Wv}#V}XbF9jw{AhFxdf2(_xlukjkMlzE+5 z0}*q7Hap1{D{P*dTy(6?H(TOwYF7P&va((;>+@VRXRYsgJ0U2&a91E<$^BCE;}RwOvnYppux zMa|^Xk`!QX)PtPC`44_#L-mI)jNiicllVv3zNGV^E1ESc#!}P2c>cmSnT(C8msE*# zdbacUXBS%ouOF=VI+d@M)+Tch`eVXj0DKe?lux& zJ8H^%XUk`bh2qF}?EO&(;iwtgBb!#r&t { + it("should create comment on a post by user", async () => { + await expect(createComment(1, 12, "good comment")).to.be.rejectedWith( + "SQLITE_CONSTRAINT: FOREIGN KEY constraint failed" + ); + }); +}); \ 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..e4fe787 --- /dev/null +++ b/test/controllers/posts.test.js @@ -0,0 +1,18 @@ +const { expect } = require("chai"); +const { createNewPost, findAllPosts } = require("../../src/controllers/posts"); + +describe("/controllers/posts", () => { + it("should create a new post", async () => { + const post = await createNewPost(1, "title", "body"); + expect(post.userId).to.equal(1); + expect(post.title).to.equal("title"); + expect(post.body).to.equal("body"); + }); + it("should find all the posts ", async () => { + let query = { + userId: 1, + }; + let posts = await findAllPosts(query); + expect(posts).to.be.an("array"); + }); +}); \ No newline at end of file diff --git a/test/setup.js b/test/setup.js index 583f8ec..adb4f2d 100644 --- a/test/setup.js +++ b/test/setup.js @@ -1,5 +1,5 @@ // setup testing environment before requiring anything -//process.env.NODE_ENV = 'testing' +process.env.NODE_ENV = 'testing' const { db } = require('../src/db/models') const chai = require('chai') From e44e836841c7de27bc20b92389056ff8c079f2d3 Mon Sep 17 00:00:00 2001 From: Nistha Agarwal Date: Tue, 2 Jun 2020 07:05:57 +0530 Subject: [PATCH 07/10] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3525aa7..eccc792 100644 --- a/README.md +++ b/README.md @@ -116,3 +116,4 @@ userId= title= body= ``` +Great learning Experience From f7f8624c266a3bad34116e8a539d8357d326322d Mon Sep 17 00:00:00 2001 From: Nistha Agarwal Date: Tue, 2 Jun 2020 07:06:41 +0530 Subject: [PATCH 08/10] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index eccc792..3525aa7 100644 --- a/README.md +++ b/README.md @@ -116,4 +116,3 @@ userId= title= body= ``` -Great learning Experience From bd2d2c6ae67f889fa97ab1506c9546702c0ec388 Mon Sep 17 00:00:00 2001 From: Nistha Agarwal Date: Tue, 2 Jun 2020 07:09:29 +0530 Subject: [PATCH 09/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3525aa7..23956a5 100644 --- a/README.md +++ b/README.md @@ -115,4 +115,4 @@ Required fields in body - userId= title= body= -``` +```. From f3f341cc6cab5d9ab3575594b5391aa4705eb142 Mon Sep 17 00:00:00 2001 From: Nistha Agarwal Date: Tue, 2 Jun 2020 07:09:44 +0530 Subject: [PATCH 10/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 23956a5..3525aa7 100644 --- a/README.md +++ b/README.md @@ -115,4 +115,4 @@ Required fields in body - userId= title= body= -```. +```