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
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ build/Release
# Dependency directories
node_modules/
jspm_packages/
/node_modules

# TypeScript v1 declaration files
typings/
Expand All @@ -54,12 +55,22 @@ typings/
# Yarn Integrity file
.yarn-integrity

#Bower dependency directory (http://bower.io)
bower_components

# dotenv environment variables file
.env
.env.test

# next.js build output
.next

#Nuxt.js build output
.nuxt
#Build output
dist
# cypress.io
cypress/screenshots
cypress/videos
#cypress e2e test videos
/frontend/cypress/videos
/frontend/cypress/screenshots
19 changes: 19 additions & 0 deletions api/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
}
}
51 changes: 41 additions & 10 deletions api/controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,58 @@
const Post = require("../models/post");
const TokenGenerator = require("../models/token_generator");
const tokenDecoder = require("../models/token_decode");

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)
res.status(200).json({ posts: posts, token: token });
console.log(posts);
res.status(200).json({ posts: posts, token: token });//posts are contained here with the token
});
},
Create: (req, res) => {
const post = new Post(req.body);
post.save(async (err) => {
if (err) {
throw err;
}
Create: async (req, res) => {
const post = new Post({message: req.body.message, author: tokenDecoder(req.headers['authorization'].split(' ')[1]).user_id});

try {
await post.save();

await post.populate('author').execPopulate();

const token = await TokenGenerator.jsonwebtoken(req.user_id)
res.status(201).json({ message: 'OK', token: token });
});
return res.status(201).json({ message: 'OK', token: token });
} catch (error) {
return res.status(500).json({ error: 'An error occurred while saving the post' });
}
},
Delete: async (req, res) => {
// try {
const { post_id } = req.params;
await Post.deleteOne({ _id: post_id });
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.status(200).json({ message: 'Post deleted', token: token });
// } catch (err) {
// res.status(401).json({ message: 'Bad request' });
// }
},
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 });
}
};


// , author: tokenDecoder(req.token).user_id
module.exports = PostsController;
22 changes: 22 additions & 0 deletions api/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ const UsersController = {
}
});
},

//This gets the user's username based on their userId
// Find the user by their userId and retrieve only the 'username' field
// If an error is found sends a (plain text) response with a status of 400 (Bad Request)
// If a user is found, sends a response with a status of 200 (OK) and the retrieved username
// If a user is not found, sends a response with a status of 404 (Not Found)
GetUsername: (req, res) => {
const { user_Id } = req.params;

User.findById(user_Id, 'username', (err, user) => {
if (err) {

res.status(400).send({ message: 'Bad request' });
} else {
if (user) {
res.status(200).send({ username: user.username }); //can use .send instead of returning the json object
} else {
res.status(404).send({ message: 'User not found' });
}
}
});
}
};

module.exports = UsersController;
8 changes: 7 additions & 1 deletion api/models/post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const mongoose = require("mongoose");
// const tokenDecoder = require("./token_decode");


const PostSchema = new mongoose.Schema({
message: String
message: String,
author: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
});

const Post = mongoose.model("Post", PostSchema);
Expand Down
25 changes: 25 additions & 0 deletions api/models/token_decode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// const jwt_decode = require('jwt-decode');
const jwt = require('jsonwebtoken');
const secret = process.env.JWT_SECRET;
// idk if we need that ^

// const tokenDecoder = (token) => {
// try {
// return jwt_decode(token);
// } catch (error) {
// return null;
// }
// }

const tokenDecoder = (token) => {
try {
const decoded = jwt.decode(token, secret);
return decoded;
} catch (error) {
return null;
}
}



module.exports = tokenDecoder;
Empty file added api/models/token_decoder.js
Empty file.
1 change: 1 addition & 0 deletions api/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
email: { type: String, required: true },
password: { type: String, required: true },
username: { type: String, required: true}, //// Defines the 'username' field as a string. username is not a string, Mongoose will throw error
});

const User = mongoose.model("User", UserSchema);
Expand Down
Loading