12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import mongoose from "mongoose";
- import PostMessage from "../models/postMessage.js";
- export const getPosts = async (req, res) => {
- try {
- const postMessages = await PostMessage.find(); //schema things
- res.status(200).json(postMessages.reverse());
- } catch (error) {
- res.status(404).json({ message: error.message });
- }
- };
- export const createPost = async (req, res) => {
- const post = req.body;
- debugger
- const newPostMessage = new PostMessage({
- ...post,
- creator: req.userId,
- createdAt: new Date().toISOString(),
- });
- 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 } = req.body;
- if (!mongoose.Types.ObjectId.isValid(id)) {
- return res.status(404).send("No post with that id");
- }
- const updatedPost = { creator, title, message, selectedFile, _id: id };
- await PostMessage.findByIdAndUpdate(id, updatedPost, { new: true });
- res.json(updatedPost);
- };
- export const deletePost = async (req, res) => {
- const { id } = req.params;
- if (!mongoose.Types.ObjectId.isValid(id)) {
- return res.status(404).send("No post with that id");
- }
- await PostMessage.findByIdAndDelete(id);
- res.json({ message: "Post deleted success" });
- };
- export const likePost = async (req, res) => {
- const { id } = req.params;
- if (!req.userId) return res.json({ message: "Unauthenticated" });
- if (!mongoose.Types.ObjectId.isValid(id)) {
- return res.status(404).send("No post with that id");
- }
- const post = await PostMessage.findById(id);
- const index = post.likes.findIndex((id) => id === String(req.userId));
- if (index === -1) {
- post.likes.push(req.userId);
- } else {
- post.likes = post.likes.filter((id) => {
- return id !== String(req.userId);
- });
-
- }
- const updatedPost = await PostMessage.findByIdAndUpdate(id, post, {
- new: true,
- });
- res.status(200).json(updatedPost);
- };
|