Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added comments with tests #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
"main": "index.js",
"scripts": {
"start": "node src/server.js",
"test": "mocha test/setup.js test/**/*.test.js",
"test": "NODE_ENV=testing mocha test/setup.js test/**/*.test.js",
"cover": "nyc --reporter=lcov npm run test"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"mysql": "^2.18.1",
"mysql2": "^2.1.0",
"sequelize": "^5.21.7",
"sequelize": "^5.21.11",
"sqlite3": "^4.2.0"
},
"devDependencies": {
Expand Down
39 changes: 39 additions & 0 deletions src/controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { Posts, Users, Comments } = require('../db/models')

async function createNewComment(title, body, userId, postId) {
const Comment = await Comments.create({
title,
body,
userId,
postId
})

return Comment
}


async function showAllComments(query) {

//let postId = query.postId
let where = {}
if (query.postId) { where.postId = query.postId }

const comment = await Comments.findAll({
include: [ Users ],
where
})
return comment
}

/* async function showCommentsPost(Id){
const commentsPost = await Comments.findAll({
where: {postId:Id},
include: [Users,Posts]
})
return commentsPost
} */

module.exports = {
createNewComment,
showAllComments
}
13 changes: 11 additions & 2 deletions src/controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,19 @@ async function findAllPosts(query) {
return posts
}

async function showpostbypostid(id)
{
const p=await Posts.findOne({
where:{id},
include:[Users]
});
return p;
}

module.exports = {
createNewPost,
findAllPosts
findAllPosts,
showpostbypostid
}

/* Test Code */
Expand All @@ -53,6 +63,5 @@ async function task() {
console.log(`${p.title}\nauthor: ${p.user.username}\n${p.body}\n==========\n`)
}
}

task()
*/
6 changes: 1 addition & 5 deletions src/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ async function createAnonUser() {
}

async function getUserById(id) {
if (!id) throw new Error('user id not provided')
if (typeof id !== 'number') throw new Error('user id should be integer')

return await Users.findOne({ where: { id } })
}

Expand All @@ -38,6 +35,5 @@ async function task () {
console.log(await createAnonUser())
console.log('---------------------')
}

task()
*/
*/
79 changes: 42 additions & 37 deletions src/db/models.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
const Sequelize = require('sequelize')

let db
if (process.env.NODE_ENV == 'testing') {
db = new Sequelize({
dialect: 'sqlite',
storage: ':memory:',
})
if(process.env.NODE_ENV == 'testing') {
db = new Sequelize({
dialect: 'sqlite',
storage: __dirname + '/../../test/test.db'
})
} else {
db = new Sequelize({
dialect: 'mysql',
database: 'cbsocialmediadb',
username: 'cbsocialuser',
password: 'cbsocialpass',
})
db = new Sequelize({
dialect: 'mysql',
database: 'cbsocialmediadb',
username: 'cbsocialuser',
password: 'cbsocialpass',
port: 3303
})
}



const COL_ID_DEF = {
type: Sequelize.DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
type: Sequelize.DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
}
const COL_USERNAME_DEF = {
type: Sequelize.DataTypes.STRING(30),
unique: true,
allowNull: false,
type: Sequelize.DataTypes.STRING(30),
unique: true,
allowNull: false
}
const COL_TITLE_DEF = {
type: Sequelize.DataTypes.STRING(140),
allowNull: false,
type: Sequelize.DataTypes.STRING(140),
allowNull: false
}

const Users = db.define('user', {
id: COL_ID_DEF,
username: COL_USERNAME_DEF,
id: COL_ID_DEF,
username: COL_USERNAME_DEF
})

const Posts = db.define('post', {
id: COL_ID_DEF,
title: COL_TITLE_DEF,
body: {
type: Sequelize.DataTypes.TEXT,
allowNull: false,
},
id: COL_ID_DEF,
title: COL_TITLE_DEF,
body: {
type: Sequelize.DataTypes.TEXT,
allowNull: false
}
})

const Comments = db.define('comment', {
id: COL_ID_DEF,
title: COL_TITLE_DEF,
body: {
type: Sequelize.DataTypes.TEXT('tiny'),
},
id: COL_ID_DEF,
title: COL_TITLE_DEF,
body: {
type: Sequelize.DataTypes.TEXT('tiny')
}
})

Users.hasMany(Posts)
Expand All @@ -61,9 +64,11 @@ Comments.belongsTo(Users)
Posts.hasMany(Comments)
Comments.belongsTo(Posts)



module.exports = {
db,
Users,
Posts,
Comments,
}
db,
Users,
Posts,
Comments
}
Loading