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", diff --git a/src/controllers/comments.js b/src/controllers/comments.js index e69de29..e5af768 100644 --- a/src/controllers/comments.js +++ b/src/controllers/comments.js @@ -0,0 +1,18 @@ +const { Posts, Users, Comments } = require("../db/models"); + +async function createComment(user_id, post_id, comment_body) { + 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, + }); + } +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/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 dd5870c..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: 'cbsocialuser', - password: 'cbsocialpass', - }) + 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 0000000..f16b041 Binary files /dev/null and b/src/db/socialmedia.db differ diff --git a/src/db/test.db b/src/db/test.db new file mode 100644 index 0000000..8043289 Binary files /dev/null and b/src/db/test.db differ 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 = $(`