+ `)
+ )
+ }
+ })
+ }
+ comments()
+ console.log(p)
+
+
+
+ })
+
+ })
+})
+
+
+function loginIfNeeded() {
+ window.currentUser = window.localStorage.user
+ ? JSON.parse(window.localStorage.user)
+ : null
+ if (!currentUser) {
+ $.post('/api/users', {}, (user) => {
+ if (user) {
+ console.log('registered current user as ', user.username)
+ window.localStorage.user = JSON.stringify(user)
+ currentUser = user
+ $('#nav-username').text(currentUser.username)
+ }
+ })
+ } else {
+ console.log('resuming session as ', currentUser.username)
+ console.log($('#nav-username'))
+ $('#nav-username').text(currentUser.username)
+ }
+}
\ No newline at end of file
diff --git a/src/public/app/write-post.js b/src/public/app/write-post.js
index e92aa9c..a501762 100644
--- a/src/public/app/write-post.js
+++ b/src/public/app/write-post.js
@@ -1,5 +1,6 @@
$('#write-btn').click(() => {
const userId = JSON.parse(window.localStorage.user).id
+ console.log(userId + "asdasdas")
const title = $('#p-title').val()
const body = $('#p-body').val()
@@ -8,4 +9,4 @@ $('#write-btn').click(() => {
$('.nav-item .active').removeClass('active')
$("[data-components='my-posts']").addClass('active')
})
-})
+})
\ No newline at end of file
diff --git a/src/public/components/all-posts.html b/src/public/components/all-posts.html
index 242a75f..ee68b10 100644
--- a/src/public/components/all-posts.html
+++ b/src/public/components/all-posts.html
@@ -5,7 +5,8 @@
Recent Posts
-
+
\ No newline at end of file
+
+
diff --git a/src/public/components/comments.html b/src/public/components/comments.html
new file mode 100644
index 0000000..a01e3e5
--- /dev/null
+++ b/src/public/components/comments.html
@@ -0,0 +1,10 @@
+
\ 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/components/singlepost.html b/src/public/components/singlepost.html
new file mode 100644
index 0000000..57f5df9
--- /dev/null
+++ b/src/public/components/singlepost.html
@@ -0,0 +1,32 @@
+
+
+
+
+
+ Post
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/public/components/write-post.html b/src/public/components/write-post.html
index 12dc14a..25308fb 100644
--- a/src/public/components/write-post.html
+++ b/src/public/components/write-post.html
@@ -25,4 +25,4 @@
Write Post
-
+
\ No newline at end of file
diff --git a/src/routes/posts/comments.js b/src/routes/posts/comments.js
index 632f4c2..4b30947 100644
--- a/src/routes/posts/comments.js
+++ b/src/routes/posts/comments.js
@@ -1,7 +1,33 @@
const { Router } = require('express')
+const {
+ createNewComment,
+ showAllComments
+} = require('../../controllers/comments')
-const commentsRoute = Router()
+const route = Router()
+
+route.get('/', async (req, res)=>{
+ console.log("test")
+ const comments = await showAllComments(req.query)
+ res.status(200).send(comments)
+})
+
+/* route.get('/post/:id', async (req, res) => {
+ const comment = await showCommentsPost(req.params.id)
+ res.status(400).send(comment)
+}) */
+
+route.post('/', async (req, res) => {
+ const { title, body, userId, postId } = req.body
+
+ if((!title) || (!body) || (!userId) || (!postId)){
+ res.status.send({error:'title or body or userId or postId not valid'})
+ }
+ const newComment = await createNewComment(title,body,userId,postId)
+ res.status(200).send(newComment)
+
+})
module.exports = {
- commentsRoute
+ commentsRoute: route
}
\ No newline at end of file
diff --git a/src/routes/posts/index.js b/src/routes/posts/index.js
index aa107c2..0c62cd4 100644
--- a/src/routes/posts/index.js
+++ b/src/routes/posts/index.js
@@ -1,11 +1,26 @@
const { Router } = require('express')
const {
findAllPosts,
- createNewPost
+ createNewPost,
+ showpostbypostid
} = require('../../controllers/posts')
const route = Router()
+route.get("/:id",async(req,res)=>{
+ let post;
+ if(isNaN(parseInt(req.params.id)))
+ {
+ post=await showpostsbykeyword(req.params.id);
+ }
+ else
+ {
+ post=await showpostbypostid(req.params.id);
+ }
+
+ res.send(post);
+})
+
route.get('/', async (req, res) => {
const posts = await findAllPosts(req.query)
diff --git a/src/server.js b/src/server.js
index 5d06647..70f3ac6 100644
--- a/src/server.js
+++ b/src/server.js
@@ -2,7 +2,8 @@ const express = require('express')
const { db } = require('./db/models')
const { usersRoute } = require('./routes/users')
-const { postsRoute } = require('./routes/posts')
+const { postsRoute } = require('./routes/posts/index')
+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()
@@ -21,4 +23,4 @@ db.sync()
.catch((err) => {
console.error(new Error('Could not start database'))
console.error(err)
- })
+ })
\ 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..4297c9b
--- /dev/null
+++ b/test/controllers/comments.test.js
@@ -0,0 +1,47 @@
+const { expect } = require('chai')
+
+const {
+ createNewComment,
+ showAllComments
+} = require('../../src/controllers/comments')
+
+describe('controllers/comments', () => {
+ let createComment = null
+
+ it('it should create a comment', async() => {
+ createComment = await createNewComment('title','body',1,1)
+
+ expect(createComment).to.have.property('title')
+ expect(createComment).to.have.property('body')
+ expect(createComment).to.have.property('userId')
+ expect(createComment).to.have.property('postId')
+ expect(createComment.title).to.be.a('string')
+ expect(createComment.body).to.be.a('string')
+ expect(createComment.userId).to.be.a('number')
+ expect(createComment.postId).to.be.a('number')
+ })
+
+ it('it should show all comments', async() => {
+
+ let query ={
+ postId: createComment.postId
+ }
+ console.log(query + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz")
+ let foundcomment = await showAllComments(query)
+ console.log(foundcomment[0].title + "===================== foundcomment =====================")
+
+ expect(foundcomment[0]).to.property('title')
+ expect(foundcomment[0]).to.property('body')
+ expect(foundcomment[0]).to.property('userId')
+ expect(foundcomment[0]).to.property('postId')
+ expect(foundcomment[0].title).to.equal(createComment.title)
+ expect(foundcomment[0].body).to.equal(createComment.body)
+ expect(foundcomment[0].userId).to.equal(createComment.userId)
+ expect(foundcomment[0].postId).to.equal(createComment.postId)
+
+
+ })
+
+
+ })
+
diff --git a/test/controllers/posts.test.js b/test/controllers/posts.test.js
new file mode 100644
index 0000000..d415c26
--- /dev/null
+++ b/test/controllers/posts.test.js
@@ -0,0 +1,58 @@
+const { expect } = require('chai')
+
+const {
+ createNewPost,
+ findAllPosts,
+ showpostbypostid
+} = require('../../src/controllers/posts')
+
+console.log("working or not ===============================")
+
+describe('controller/posts', () => {
+
+ let createdPost = null
+
+ it('should create new post', async() => {
+
+ createdPost = await createNewPost(1,'title1','body1')
+ console.log(createdPost + '=========================================')
+
+ expect(createdPost).to.have.property('title')
+ expect(createdPost).to.have.property('body')
+ expect(createdPost).to.have.property('userId')
+
+ })
+
+ it('should find all posts', async ()=>{
+
+ /* let query = {}
+
+ let foundPost = findAllPosts(query)*/
+
+ let query ={
+
+ }
+ let foundPost = await findAllPosts(query)
+ let array = []
+ for(let f of foundPost){
+ array.push(f)
+ }
+
+ expect(array[0].title).to.equal(createdPost.title)
+ expect(array[0].body).to.equal(createdPost.body)
+
+ })
+
+ it('should show post by id', async() => {
+
+ let query ={
+ userId: createdPost.userId
+ }
+ let foundPost = await showpostbypostid(query.userId)
+
+ expect(foundPost.title).to.equal(createdPost.title)
+ expect(foundPost.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..fc99c6f 100644
--- a/test/controllers/users.test.js
+++ b/test/controllers/users.test.js
@@ -1,41 +1,37 @@
-const { expect } = require('chai')
-const {
- createAnonUser,
- getUserById,
- getUserByUsername
+const { expect } =require('chai')
+
+const{
+ createAnonUser,
+ getUserById,
+ getUserByUsername
} = require('../../src/controllers/users')
describe('controllers/users', () => {
+
let createdUser = null
- it('should create anonymous user', async () => {
+ it('should create anonymous user', async() => {
+
+
+ createdUser = await createAnonUser()
- createdUser = await createAnonUser()
expect(createdUser).to.have.property('username')
- expect(createdUser.id).to.be.a('number')
+ expect(createdUser.id).to.be.a('number')
+
+
})
- it('should find user by userid', async () => {
-
+ it('should find user by userid', async() => {
let foundUser = await getUserById(createdUser.id)
expect(foundUser.username).to.equal(createdUser.username)
-
})
- it('should through error for non number userid', async () => {
-
- await expect(getUserById('sss')).to.be.rejectedWith('user id should be integer')
- await expect(getUserById(null)).to.be.rejectedWith('user id not provided')
- await expect(getUserById(true)).to.be.rejectedWith('user id should be integer')
- await expect(getUserById(0)).to.be.rejectedWith('user id not provided')
-
- })
-
- it('should find user by username', async () => {
-
+ it('should find user by username', async() => {
let foundUser = await getUserByUsername(createdUser.username)
expect(foundUser.id).to.equal(createdUser.id)
+ expect(foundUser.username).to.equal(createdUser.username)
+
})
-})
+})
\ No newline at end of file
diff --git a/test/setup.js b/test/setup.js
index adb4f2d..523f6dd 100644
--- a/test/setup.js
+++ b/test/setup.js
@@ -1,10 +1,7 @@
-// setup testing environment before requiring anything
-process.env.NODE_ENV = 'testing'
-
const { db } = require('../src/db/models')
-const chai = require('chai')
-chai.use(require('chai-as-promised'))
before(async () => {
- await db.sync()
+ //console.log('running before all tests')
+
+ await db.sync()
})
\ No newline at end of file
diff --git a/test/test.db b/test/test.db
new file mode 100644
index 0000000..529cefb
Binary files /dev/null and b/test/test.db differ