text: import mongoose from 'mongoose';
  import PostMessage from '../models/postMessage.js';
  
  export const getPosts = async (req, res) => {
      try {
          // Takes time, so we need to add asyncronous function
          const postMessages = await PostMessage.find();
          // HTTP 200: OK
          res.status(200).json(postMessages);
      } catch (error) {
          console.error(error);
          // HTTP 404: Not Found
          res.status(404).json({ message: error.message });
      }
  };
  
  export const createPost = async (req, res) => {
      const post = req.body;
  
      const newPost = new PostMessage(post);
  
      try {
          await newPost.save();
          // HTTP 201: Created
          res.status(201).json(newPost);
      } catch (error) {
          console.error(error);
          // HTTP 409: Conflict
          res.status(409).json({ message: error.message });
      }
  };
  
  export const updatePost = async (req, res) => {
      const { id: _id } = req.params;
      const post = req.body;
  
      if(!mongoose.Types.ObjectId.isValid(_id)) res.status(404).send('No post with that id');
  
      const updatedPost = PostMessage.findByIdAndUpdate(_id, post, { new: true });
  
      res.json(updatedPost);
  }