Skip to content

project completed #5

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

Open
wants to merge 6 commits 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
395 changes: 89 additions & 306 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -11,15 +11,13 @@
"author": "",
"license": "ISC",
"dependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"express": "^4.17.1",
"mocha": "^7.2.0",
"mysql2": "^2.1.0",
"nyc": "^15.0.1",
"sequelize": "^5.21.7",
"sqlite3": "^4.2.0"
},
"devDependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"mocha": "^7.2.0",
"nyc": "^15.0.1"
}
}
19 changes: 19 additions & 0 deletions src/controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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,
};
58 changes: 13 additions & 45 deletions src/controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,26 @@
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 ],
where
})
let where = {};
if (query.userId) {
where.userId = query.userId;
}

return posts
return await Posts.findAll({
include: [Users, Comments],
where,
});
}

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()
*/
findAllPosts,
};
38 changes: 10 additions & 28 deletions src/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
const { Users } = require('../db/models')
const { genRandomUsername } = require('../utils/username')
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
});
}

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 } })
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 } });
}

async function getUserByUsername(username) {
return await Users.findOne({ where: { username } })
return await Users.findOne({ where: { username } });
}

module.exports = {
createAnonUser,
getUserById,
getUserByUsername,
}

/* Test Code */
/*
async function task () {
console.log(await createAnonUser())
console.log('---------------------')
console.log(await createAnonUser())
console.log('---------------------')
console.log(await createAnonUser())
console.log('---------------------')
console.log(await createAnonUser())
console.log('---------------------')
}

task()
*/
};
48 changes: 24 additions & 24 deletions src/db/models.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
const Sequelize = require('sequelize')
const Sequelize = require("sequelize");

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

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

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

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

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

Users.hasMany(Posts)
Posts.belongsTo(Users)
Users.hasMany(Posts);
Posts.belongsTo(Users);

Users.hasMany(Comments)
Comments.belongsTo(Users)
Users.hasMany(Comments);
Comments.belongsTo(Users);

Posts.hasMany(Comments)
Comments.belongsTo(Posts)
Posts.hasMany(Comments);
Comments.belongsTo(Posts);

module.exports = {
db,
Users,
Posts,
Comments,
}
};
Binary file added src/db/socialmedia.db
Binary file not shown.
Binary file added src/db/testing.db
Binary file not shown.
81 changes: 59 additions & 22 deletions src/public/app/all-posts.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,62 @@
function loadPosts() {
$.get('/api/posts', (posts) => {
$.get("/api/posts", (posts) => {


console.log(posts);

for (let p of posts) {
$('#posts-container').append(
$(`
<div class="col-4">
<div class="card m-2">
<div class="card-body">
<h5 class="card-title">${p.title}</h5>
<h6 class="card-subtitle mb-2 text-muted">${p.user.username}</h6>
<p class="card-text">
${p.body.substr(0, 200)}
<a href="#">...read more</a>
</p>
<a href="#" class="card-link">Comment</a>
<a href="#" class="card-link">Like</a>
</div>
</div>
</div>


let item = $(`
<div class="col-4">
<div class="card m-2">
<div class="card-body">
<h5 class="card-title">${p.title}</h5>
<h6 class="card-subtitle mb-2 text-muted">${
p.user.username
}</h6>
<p class="card-text">
${p.body.substr(0, 200)}
<a href="#">...read more</a>
</p>
<input type="text" placeholder="COMMENTS" class="form-control form-control-sm form-control-range" col-4>
<button class="btn btn-primary">Comment</button>
<p class="comment"></p>
</div>
</div>
</div>
`);
let commentBox = item.find(".comment");
for (let comment of p.comments) {
commentBox.append(
`<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Show
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">

<a class="dropdown-item" href="#">${comment.title} => ${comment.body})</a>
</div>
</div>`

`)
)
}
})
}
);
}

item.find(".btn").on("click", () => {
$.post(
"/api/comments",
{
post_id: p.id,
comment_body: item.find(".form-control").val(),
user_id: JSON.parse(window.localStorage.user).id,
},
(comment) => {
$("#content").load(`/components/all-posts.html`);
}
);
});
$("#posts-container").append(item);

}
});
}
Loading