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
+
+
+
+
+ comment
+ show
+
+
-
`)
)
+ /*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(
+ $(`
+
+ `)
+ )
+ }
+ })
+ }
+ /*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 @@
-
+
\ 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 @@