Skip to content

Commit

Permalink
add likes d.b/controller and test for likes
Browse files Browse the repository at this point in the history
  • Loading branch information
carolrs committed May 16, 2023
1 parent 7b5bbdf commit 3fac572
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
43 changes: 24 additions & 19 deletions api/controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,46 @@ const JWT = require("jsonwebtoken");

const PostsController = {
Index: (req, res) => {
Post.find().populate("authorUserID")
Post.find()
.populate("authorUserID")
.exec(async (err, posts) => {
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 });
});
},

Create: (req, res) => {
if (!req.user_id) {
return res.status(401).json({ message: 'Auth error' })
if (!req.user_id) {
return res.status(401).json({ message: "Auth error" });
}
const post = new Post({ ...req.body, authorUserID: req.user_id });
post.save(async (err) => {
if (err) { throw err }
const token = await TokenGenerator.jsonwebtoken(req.user_id)
res.status(201).json({ message: 'OK', token: token });
if (err) {
throw err;
}
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.status(201).json({ message: "OK", token: token });
});
},
AddLikes: async(req, res) => {
const post = await Post.findById(req.body.post_id, (err, post) => {
if (err) {
throw err }
post.likes.concat(req.body.user_id)
post.save(async (err) => {
if (err) { throw err }
const token = await TokenGenerator.jsonwebtoken(req.user_id)
res.status(201).json({ message: 'OK', token: token });
AddLikes: (req, res) => {
Post.findById(req.body.post_id, (err, post) => {
if (err) {
throw err;
}
post.likes.push(req.body.user_id);

post.save(async (err, post) => {
if (err) {
throw err;
}
res.status(201).json({ message: "OK", post: post });
});
})
}
});
},
};

module.exports = PostsController;

21 changes: 21 additions & 0 deletions api/spec/controllers/posts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ describe("/posts", () => {
expect(posts.length).toEqual(1);
expect(posts[0].message).toEqual("hello world");
});

test("add one like in an existing post", async () => {
await request(app)
.post("/posts")
.set("Authorization", `Bearer ${token}`)
.send({ message: "hello world", token: token });
let posts = await Post.find();

await request(app)
.patch("/posts/likes")
.set("Authorization", `Bearer ${token}`)
.send({post_id: posts[0]._id, user_id: mockUserID});

let updatedPosts = await Post.find();
//testing if the likes array has one element
expect(updatedPosts[0].likes.length).toEqual(1);
//testing if the user id is added to the likes array
expect(updatedPosts[0].likes[0]).toEqual(mockUserID);


});

test("returns a new token", async () => {
let response = await request(app)
Expand Down

0 comments on commit 3fac572

Please sign in to comment.