Skip to content
Snippets Groups Projects
Commit 7e93c013 authored by Morris, George (UG - Computer Science)'s avatar Morris, George (UG - Computer Science)
Browse files

Created the create, delete and like posts functions to handle create, delete, and liking posts

parent ea1b7402
No related branches found
No related tags found
2 merge requests!8CI/CD,!2Posts service
......@@ -11,7 +11,14 @@ export const getPosts = async (req, res) => {
};
export const createPost = async (req, res) => {
const post = new Post({ ...req.body, createdAt: new Date().toISOString() });
try {
await post.save();
return res.status(201).json(post);
} catch (error) {
return res.status(409).json({ message: "Something went wrong" });
}
}
export const getPostById = async (req, res) => {
......@@ -26,11 +33,26 @@ export const getPostById = async (req, res) => {
}
export const deletePost = async (req, res) => {
const { id } = req.params;
if (!mongoose.Types.ObjectId.isValid(id)) return res.status(404).send(`No post with id: ${id}`);
await Post.findByIdAndRemove(id);
return res.status(200).json({ message: "Post Deleted" });
}
export const likePost = async (req, res) => {
const { id, userId } = req.params;
const post = await Post.findById(id);
if (post.likes.find((like) => like === userId)) {
post.likes = post.likes.filter((like) => like !== userId);
} else {
post.likes.push(userId);
}
await post.save();
return res.status(200).json(post);
}
export const addComment = async (req, res) => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment