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

Memory timing issue #202

Open
wants to merge 2 commits into
base: PART_1_and_2
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
7 changes: 5 additions & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"start": "NODE_OPTIONS=--openssl-legacy-provider react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"start-legacy": "NODE_OPTIONS=--openssl-legacy-provider react-scripts start",
"build-legacy": "NODE_OPTIONS=--openssl-legacy-provider react-scripts build",
"test-legacy": "NODE_OPTIONS=--openssl-legacy-provider react-scripts test"
},
"eslintConfig": {
"extends": "react-app"
Expand Down
81 changes: 48 additions & 33 deletions server/controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,53 +28,68 @@ export const getPost = async (req, res) => {
}

export const createPost = async (req, res) => {
const { title, message, selectedFile, creator, tags } = req.body;

const newPostMessage = new PostMessage({ title, message, selectedFile, creator, tags })

try {
await newPostMessage.save();

res.status(201).json(newPostMessage );
} catch (error) {
res.status(409).json({ message: error.message });
}
}
const { title, message, selectedFile, creator, tags } = req.body;

const createdAt = new Date();
const newPostMessage = new PostMessage({
title,
message,
selectedFile,
creator,
tags,
createdAt,
});

try {
await newPostMessage.save();

res.status(201).json(newPostMessage);
} catch (error) {
res.status(409).json({ message: error.message });
}
};

export const updatePost = async (req, res) => {
const { id } = req.params;
const { title, message, creator, selectedFile, tags } = req.body;

if (!mongoose.Types.ObjectId.isValid(id)) return res.status(404).send(`No post with id: ${id}`);
const { id } = req.params;
const { title, message, creator, selectedFile, tags } = req.body;

const updatedPost = { creator, title, message, tags, selectedFile, _id: id };
if (!mongoose.Types.ObjectId.isValid(id))
return res.status(404).send(`No post with id: ${id}`);

await PostMessage.findByIdAndUpdate(id, updatedPost, { new: true });
const updatedPost = { creator, title, message, tags, selectedFile, _id: id };

res.json(updatedPost);
}
await PostMessage.findByIdAndUpdate(id, updatedPost, { new: true });

res.json(updatedPost);
};

export const deletePost = async (req, res) => {
const { id } = req.params;
const { id } = req.params;

if (!mongoose.Types.ObjectId.isValid(id)) return res.status(404).send(`No post with id: ${id}`);
if (!mongoose.Types.ObjectId.isValid(id))
return res.status(404).send(`No post with id: ${id}`);

await PostMessage.findByIdAndRemove(id);
await PostMessage.findByIdAndRemove(id);

res.json({ message: "Post deleted successfully." });
}
res.json({ message: "Post deleted successfully." });
};

export const likePost = async (req, res) => {
const { id } = req.params;
const { id } = req.params;

if (!mongoose.Types.ObjectId.isValid(id)) return res.status(404).send(`No post with id: ${id}`);

const post = await PostMessage.findById(id);
if (!mongoose.Types.ObjectId.isValid(id))
return res.status(404).send(`No post with id: ${id}`);

const updatedPost = await PostMessage.findByIdAndUpdate(id, { likeCount: post.likeCount + 1 }, { new: true });

res.json(updatedPost);
}
const post = await PostMessage.findById(id);

const updatedPost = await PostMessage.findByIdAndUpdate(
id,
{ likeCount: post.likeCount + 1 },
{ new: true }
);

res.json(updatedPost);
};


export default router;
27 changes: 13 additions & 14 deletions server/models/postMessage.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import mongoose from 'mongoose';

const postSchema = mongoose.Schema({
title: String,
message: String,
creator: String,
tags: [String],
selectedFile: String,
likeCount: {
type: Number,
default: 0,
},
createdAt: {
type: Date,
default: new Date(),
},
})
title: String,
message: String,
creator: String,
tags: [String],
selectedFile: String,
likeCount: {
type: Number,
default: 0,
},
createdAt: {
type: Date,
},
});

var PostMessage = mongoose.model('PostMessage', postSchema);

Expand Down
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"mongoose": "^5.9.29"
"mongoose": "^5.13.22",
"nodemon": "^3.1.4"
}
}