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

Bug fixes #31

Open
wants to merge 48 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
4c3c18d
Update README.md
williamlines Nov 28, 2022
747e826
Merge pull request #1 from williamlines/williamlines-patch-1
williamlines Nov 28, 2022
fa6072c
Understanding the code
Nov 30, 2022
3074b52
fixed broken tests
williamlines Dec 1, 2022
18a29cc
added the rest of the files missed by first commit
williamlines Dec 1, 2022
98b5d13
fixed spelling error
williamlines Dec 1, 2022
de6f9a2
Home page established and route to /home created
Dec 1, 2022
a2f1908
Add tests for date and implement method
kwatts949 Dec 1, 2022
6c24d68
added route to new post form and mostly built the functionality
williamlines Dec 1, 2022
9a53af5
fixed post request
williamlines Dec 1, 2022
cd7d956
driver switch
robin277t Dec 1, 2022
5f1c9c5
now updates token (maybe?)
tomfyfe85 Dec 1, 2022
c460972
cypress homepage test
tbuller Dec 1, 2022
061ba58
switch driver
kwatts949 Dec 1, 2022
32a458a
Merge pull request #2 from williamlines/feature_newest_post
kwatts949 Dec 1, 2022
ccfeafc
can post to feed with an updated token (NewPostForm.js). NewPostForm.…
tomfyfe85 Dec 1, 2022
879419c
Homepage complete with testing in end to end
tbuller Dec 2, 2022
8a5715d
started test
williamlines Dec 2, 2022
f0a4e15
fixed component test
williamlines Dec 2, 2022
181eb90
feature complete and tested
robin277t Dec 2, 2022
be98534
making_a_post_on_a_page.cy.js completed
tomfyfe85 Dec 2, 2022
0e14dce
Merge pull request #4 from williamlines/post-have-users-2
robin277t Dec 2, 2022
5e01bf6
merge conflict
williamlines Dec 2, 2022
78409f4
Merge pull request #5 from williamlines/expl
williamlines Dec 2, 2022
dd7ed5e
solve conflicts
tbuller Dec 2, 2022
de5593d
Merge branch 'main' into HB1
robin277t Dec 2, 2022
58c4cd6
Merge pull request #6 from williamlines/HB1
H6enryB Dec 2, 2022
7c72007
updated model
williamlines Dec 2, 2022
7c5f224
home fix
tbuller Dec 2, 2022
70538ff
Merge pull request #7 from williamlines/comments
tomfyfe85 Dec 2, 2022
1a3d2f8
.
tomfyfe85 Dec 2, 2022
a296312
merge fix
tomfyfe85 Dec 2, 2022
b9a671c
comments2 commit 1
tbuller Dec 2, 2022
86f6709
todo like button in Feed.js (see comment in file)
tomfyfe85 Dec 2, 2022
88e958a
continued work on comments function
robin277t Dec 5, 2022
0fb7298
likes are unique
tomfyfe85 Dec 5, 2022
e255f11
built post request on like method in Feed.js
tomfyfe85 Dec 5, 2022
9375a40
built routing for post request to /likes. started building controller…
tomfyfe85 Dec 5, 2022
e23bc93
collected relevant data from post request to /likes
tomfyfe85 Dec 5, 2022
6b2d5e3
comments schema works
tbuller Dec 5, 2022
de97692
likers added to post document
tomfyfe85 Dec 5, 2022
49fed2d
reloads the page on like, and displays likes
tomfyfe85 Dec 5, 2022
26a8345
comments working but navigate bug and not displaued yet
robin277t Dec 5, 2022
5ba5c04
comments feature mostly working
robin277t Dec 5, 2022
d6bdf72
Merge pull request #8 from williamlines/feature-likes
robin277t Dec 5, 2022
28af3dd
Merge branch 'main' into comments2
tbuller Dec 5, 2022
d8df1b5
Merge pull request #9 from williamlines/comments2
tbuller Dec 5, 2022
7ac4607
chevron and formatting fixes
tbuller Dec 5, 2022
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Acebook

# Trello Board:

https://trello.com/b/Ckf68HAB/acebook

In this project, you are tasked with working on an existing application. A significant part of the challenge will be to familiarise yourself with the codebase you've inherited, as you work to **improve and extend** it.

## Videos
Expand Down
23 changes: 14 additions & 9 deletions api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@ const JWT = require("jsonwebtoken");
const postsRouter = require("./routes/posts");
const tokensRouter = require("./routes/tokens");
const usersRouter = require("./routes/users");
const commentsRouter = require("./routes/comments");
const likesRouter = require("./routes/likes")


const app = express();

// setup for receiving JSON
app.use(express.json())
app.use(express.json());

app.use(logger("dev"));
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));

// middleware function to check for valid tokens
const tokenChecker = (req, res, next) => {

let token;
const authHeader = req.get("Authorization")
const authHeader = req.get("Authorization");

if(authHeader) {
token = authHeader.slice(7)
if (authHeader) {
token = authHeader.slice(7);
}

JWT.verify(token, process.env.JWT_SECRET, (err, payload) => {
if(err) {
console.log(err)
res.status(401).json({message: "auth error"});
if (err) {
//console.log(err);
res.status(401).json({ message: "auth error" });
} else {
req.user_id = payload.user_id;
next();
Expand All @@ -40,8 +42,11 @@ const tokenChecker = (req, res, next) => {

// route setup
app.use("/posts", tokenChecker, postsRouter);
app.use("/comments", tokenChecker, commentsRouter);
app.use("/tokens", tokensRouter);
app.use("/users", usersRouter);
app.use("/likes", tokenChecker, likesRouter);
//app.use("/", homeRouter);

// catch 404 and forward to error handler
app.use((req, res, next) => {
Expand All @@ -55,7 +60,7 @@ app.use((err, req, res) => {
res.locals.error = req.app.get("env") === "development" ? err : {};

// respond with details of the error
res.status(err.status || 500).json({message: 'server error'})
res.status(err.status || 500).json({ message: "server error" });
});

module.exports = app;
61 changes: 61 additions & 0 deletions api/controllers/likes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const Post = require("../models/post");
const TokenGenerator = require("../models/token_generator");

// const addLike = (req, res) => {
// // this method will:
// // 1. retrieve the post using post_id
// // 2. insert user_id into likes array
// const data = req.body;
// const post_id = data.post_id;
// const user_id = req.user_id;

// console.log("method called");
// console.log(`post_id is ${post_id}`);
// console.log(`user_id is ${user_id}`);

// Post.findByIdAndUpdate(
// post_id,
// {
// $push: {
// likes: { likerId: user_id },
// },
// },
// async (err) => {
// if (err) {
// throw err;
// }
// const token = await TokenGenerator.jsonwebtoken(user_id);
// res.status(201).json({ message: "OK", token: token });
// }
// );
// };

const LikesController = {
Create: (req, res) => {
const data = req.body;
const post_id = data.post_id;
const user_id = req.user_id;

console.log("method called");
console.log(`post_id is ${post_id}`);
console.log(`user_id is ${user_id}`);

Post.findByIdAndUpdate(
post_id,
{
$push: {
likes: { likerId: user_id },
},
},
async (err) => {
if (err) {
throw err;
}
const token = await TokenGenerator.jsonwebtoken(user_id);
res.status(201).json({ message: "OK", token: token });
}
);
},
};

module.exports = LikesController;
35 changes: 31 additions & 4 deletions api/controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const router = require("../routes/posts");
const Post = require("../models/post");
const TokenGenerator = require("../models/token_generator");

Expand All @@ -7,21 +8,47 @@ const PostsController = {
if (err) {
throw err;
}
const token = await TokenGenerator.jsonwebtoken(req.user_id)
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.status(200).json({ posts: posts, token: token });
});
}).sort({ time: -1 });
},
Create: (req, res) => {
req.body.time = Date.now();
req.body.posterUserId = req.user_id;
req.body.comments = [];
const post = new Post(req.body);
post.save(async (err) => {
if (err) {
throw err;
}

const token = await TokenGenerator.jsonwebtoken(req.user_id)
res.status(201).json({ message: 'OK', token: token });
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.status(201).json({ message: "OK", token: token });
});
},
CreateComment: (req, res) => {
req.body.time = Date.now();
req.body.posterUserId = req.user_id;
Post.findByIdAndUpdate(
req.body.postId,
{
$push: {
comments: {
time: req.body.time,
user: req.user_id,
comment: req.body.comment,
},
},
},
async (err) => {
if (err) {
throw err;
}
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.status(201).json({ message: "OK", token: token });
}
);
},
};

module.exports = PostsController;
11 changes: 5 additions & 6 deletions api/controllers/tokens.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
const User = require("../models/user");
const TokenGenerator = require("../models/token_generator")
const TokenGenerator = require("../models/token_generator");

const SessionsController = {

Create: (req, res) => {
const email = req.body.email;
const password = req.body.password;

User.findOne({ email: email }).then(async (user) => {
if (!user) {
console.log("auth error: user not found")
console.log("auth error: user not found");
res.status(401).json({ message: "auth error" });
} else if (user.password !== password) {
console.log("auth error: passwords do not match")
console.log("auth error: passwords do not match");
res.status(401).json({ message: "auth error" });
} else {
const token = await TokenGenerator.jsonwebtoken(user.id)
const token = await TokenGenerator.jsonwebtoken(user.id);
res.status(201).json({ token: token, message: "OK" });
}
});
}
},
};

module.exports = SessionsController;
4 changes: 2 additions & 2 deletions api/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const UsersController = {
const user = new User(req.body);
user.save((err) => {
if (err) {
res.status(400).json({message: 'Bad request'})
res.status(400).json({ message: "Bad request" });
} else {
res.status(201).json({ message: 'OK' });
res.status(201).json({ message: "OK" });
}
});
},
Expand Down
12 changes: 11 additions & 1 deletion api/models/post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
const mongoose = require("mongoose");

const PostSchema = new mongoose.Schema({
message: String
message: String,
time: Number,
posterUserId: String,
comments: [
{
time: { type: Number },
user: { type: String },
comment: { type: String },
},
],
likes: Array
});

const Post = mongoose.model("Post", PostSchema);
Expand Down
4 changes: 2 additions & 2 deletions api/models/token_generator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const JWT = require("jsonwebtoken");
const options = {expiresIn: "10m"};
const options = { expiresIn: "10m" };
const secret = process.env.JWT_SECRET;

class TokenGenerator {
static jsonwebtoken(user_id) {
return JWT.sign({user_id: user_id, iat: Date.now()}, secret, options);
return JWT.sign({ user_id: user_id, iat: Date.now() }, secret, options);
}
}

Expand Down
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"start": "nodemon ./bin/www",
"start:test": "MONGODB_URL='mongodb://0.0.0.0/acebook_test' npm start",
"test": "jest"
"test": "jest --runInBand"
},
"engines": {
"node": ">=18.1.0"
Expand Down
10 changes: 10 additions & 0 deletions api/routes/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

const express = require("express");
const router = express.Router();


const PostsController = require("../controllers/posts");

router.post("/", PostsController.CreateComment);

module.exports = router;
13 changes: 13 additions & 0 deletions api/routes/likes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

const express = require("express");
const router = express.Router();


const LikesController = require("../controllers/likes");



// router.get("/", PostsController.Index);
router.post("/", LikesController.Create);

module.exports = router;
4 changes: 4 additions & 0 deletions api/routes/posts.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

const express = require("express");
const router = express.Router();


const PostsController = require("../controllers/posts");



router.get("/", PostsController.Index);
router.post("/", PostsController.Create);

Expand Down
Loading