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

Delete post #155

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Changes from 8 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fded4f6
Annc Initial Setup
csanann Jun 5, 2023
77f9897
Adds and Rechecks files and folders
csanann Jun 6, 2023
ff6658e
fixed failing cypress test in signing in
Jun 7, 2023
96bfb07
all stuff so far so cyryl can see it
Jun 7, 2023
e725fb9
test passes but needs to clear the db before ech test
Jun 7, 2023
63bef3d
added method on the frontend to make a new post, tested the frontend …
Jun 7, 2023
14119d6
added comments so viewers know whats happen
Jun 7, 2023
f84f221
Merge pull request #2 from cshjp/making-post-frontend
cshjp Jun 7, 2023
3d4f2e2
Merge pull request #1 from cshjp/ann-setup
cshjp Jun 7, 2023
74f7ddb
test added and passed
CyrylG Jun 8, 2023
c2d64d9
Created test for adding multiple posts through the post model
bwilton93 Jun 8, 2023
b298e7b
Created a test to find by a single post ID
bwilton93 Jun 8, 2023
a98f915
tested post model for findByIdAndUpdate()
bwilton93 Jun 8, 2023
196d20a
Not working yet
bwilton93 Jun 8, 2023
4814637
bunch of failed bs added
CyrylG Jun 8, 2023
9688d3e
username- implementation + tests pass- backend
Ormeline Jun 8, 2023
dbbe755
wip app.js and usernamepage.js
Ormeline Jun 8, 2023
da66f4a
Test for returning a single code, and code to satisfy test, implemented
bwilton93 Jun 9, 2023
5ce083c
Show single post is now working
bwilton93 Jun 9, 2023
a213437
updated
Jun 9, 2023
5fe20d0
changed .json to .send -receive plain text rspnse
Ormeline Jun 9, 2023
d7e4e00
auhtor added to posts
Jun 9, 2023
a409d0f
little bit of refactoring
Jun 9, 2023
0c7e515
Can update a post in the backend
bwilton93 Jun 9, 2023
33f2bc1
Started work on showing a single post on the front end
bwilton93 Jun 9, 2023
d02d1df
Started playing around with react
bwilton93 Jun 9, 2023
3eefc2b
Figured out how to extract the post_id from the params in react
bwilton93 Jun 9, 2023
ff5779e
Can extract the placeholder content to put in the form, so the user k…
bwilton93 Jun 9, 2023
173948b
Created frontend react component for updating a post - still need to …
bwilton93 Jun 9, 2023
ea83d64
Added update button to posts on feed. This doesn't check if the user …
bwilton93 Jun 9, 2023
5ef15b2
ready to merge
bwilton93 Jun 9, 2023
b737dc5
Merge pull request #5 from cshjp/update-posts-backend
bwilton93 Jun 9, 2023
80c06f3
Merge pull request #6 from cshjp/update-posts-frontend
bwilton93 Jun 9, 2023
aa49409
Ignore cypress videos & screenshots and remove checked in ones
neoeno Jun 9, 2023
589b38d
Merge branch 'main' into postcreationtest
bwilton93 Jun 12, 2023
fd64872
Merge pull request #4 from cshjp/postcreationtest
bwilton93 Jun 12, 2023
6384774
Merge pull request #8 from neoeno/remove-cypress-videos-and-screenshots
bwilton93 Jun 12, 2023
235ef51
adds delete function
Ormeline Jun 12, 2023
855dcee
Fixed failing tests for deleting posts, and creating new users
bwilton93 Jun 12, 2023
d918c80
Merge branch 'main' into delete-post
bwilton93 Jun 12, 2023
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
17 changes: 16 additions & 1 deletion api/controllers/posts.js
Original file line number Diff line number Diff line change
@@ -2,12 +2,13 @@ const Post = require("../models/post");
const TokenGenerator = require("../models/token_generator");

const PostsController = {
Index: (req, res) => {
Index: (req, res) => {
Post.find(async (err, posts) => {
if (err) {
throw err;
}
const token = await TokenGenerator.jsonwebtoken(req.user_id)
console.log(posts);
res.status(200).json({ posts: posts, token: token });
});
},
@@ -22,6 +23,20 @@ const PostsController = {
res.status(201).json({ message: 'OK', token: token });
});
},
ShowPost: async (req, res) => {
let post_id = req.params.id;
console.log(post_id);
let post = await Post.findById(post_id);
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.status(200).json({post, token: token});
},
UpdatePost: async (req, res) => {
let post_id = req.params.id;
await Post.findByIdAndUpdate(post_id, { message: req.body.message });

const token = await TokenGenerator.jsonwebtoken(req.user_id)
res.status(201).json({ message: 'OK', token: token });
}
};

module.exports = PostsController;
Empty file added api/models/token_decoder.js
Empty file.
2 changes: 2 additions & 0 deletions api/routes/posts.js
Original file line number Diff line number Diff line change
@@ -5,5 +5,7 @@ const PostsController = require("../controllers/posts");

router.get("/", PostsController.Index);
router.post("/", PostsController.Create);
router.get("/:id", PostsController.ShowPost);
router.patch("/:id/update", PostsController.UpdatePost);

module.exports = router;
34 changes: 34 additions & 0 deletions api/spec/controllers/posts.spec.js
Original file line number Diff line number Diff line change
@@ -85,6 +85,27 @@ describe("/posts", () => {
});
})

describe("PATCH, when token is present", () => {
test("it updates a post", async() => {
let post = new Post({message: "this text will change"});
await post.save();

let response = await request(app)
.patch("/posts/" + post._id + "/update")
.set("Authorization", `Bearer ${token}`)
.send({message: "this is a new message", token: token});
expect(response.status).toEqual(201);

response = await request(app)
.get("/posts/" + post._id)
.set("Authorization", `Bearer ${token}`)
.send({token: token});

let result = response.body;
expect(result.post.message).toEqual("this is a new message");
})
})

describe("GET, when token is present", () => {
test("returns every post in the collection", async () => {
let post1 = new Post({message: "howdy!"});
@@ -111,6 +132,19 @@ describe("/posts", () => {
expect(response.status).toEqual(200);
})

// Trying to write a test for returning a single post, can't get it to work though
test("returns a single post", async () => {
let post = new Post({message: "this should be returned"});
let post_id = post._id;
await post.save();
let response = await request(app)
.get("/posts/" + post_id)
.set("Authorization", `Bearer ${token}`)
.send({token: token});
let result = response.body;
expect(result.post.message).toEqual("this should be returned");
})

test("returns a new token", async () => {
let post1 = new Post({message: "howdy!"});
let post2 = new Post({message: "hola!"});
34 changes: 34 additions & 0 deletions api/spec/models/post.spec.js
Original file line number Diff line number Diff line change
@@ -36,5 +36,39 @@ describe("Post model", () => {
done();
});
});

});

it("can save multiple posts", async () => {
var post1 = new Post({ message: "this is a post" });
var post2 = new Post({ message: "this is another post" });

await post1.save();
await post2.save();

let posts = await Post.find();
expect(posts.length).toBe(2);
})

it("can find a single post once multiple posts are added", async () => {
var post1 = new Post({ message: "this is a post" });
var post2 = new Post({ message: "this is another post" });

await post1.save();
await post2.save();

let post = await Post.findById(post1._id);
expect(post).toMatchObject({ message: "this is a post" });
})

it("can update a post based on post object id", async () => {
var post = new Post({ message: "This text will change" });
const newMessage = "This is the new text";

await post.save();

await Post.findByIdAndUpdate(post._id, { message: newMessage });
let result = await Post.findById(post._id);
expect(result).toMatchObject({ message: "This is the new text" });
})
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frontend/cypress/videos/signing_in.cy.js.mp4
Binary file not shown.
Binary file modified frontend/cypress/videos/signing_up.cy.js.mp4
Binary file not shown.
6 changes: 6 additions & 0 deletions package-lock.json

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