Advertisement
bebo231312312321

Untitled

Feb 13th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { userModel, themeModel, postModel } = require('../models');
  2.  
  3. function newPost(text, userId, themeId) {
  4.     return postModel.create({ text, userId, themeId })
  5.         .then(post => {
  6.             return Promise.all([
  7.                 userModel.updateOne({ _id: userId }, { $push: { posts: post._id }, $addToSet: { themes: themeId } }),
  8.                 themeModel.findByIdAndUpdate({ _id: themeId }, { $push: { posts: post._id }, $addToSet: { subscribers: userId } }, { new: true })
  9.             ])
  10.         })
  11. }
  12.  
  13. function getLatestsPosts(req, res, next) {
  14.     const limit = Number(req.query.limit) || 0;
  15.  
  16.     postModel.find()
  17.         .sort({ created_at: -1 })
  18.         .limit(limit)
  19.         .populate('themeId userId')
  20.         .then(posts => {
  21.             res.status(200).json(posts)
  22.         })
  23.         .catch(next);
  24. }
  25.  
  26. function createPost(req, res, next) {
  27.     const { themeId } = req.params;
  28.     const { _id: userId } = req.user;
  29.     const { postText } = req.body;
  30.  
  31.     newPost(postText, userId, themeId)
  32.         .then(([_, updatedTheme]) => res.status(200).json(updatedTheme))
  33.         .catch(next);
  34. }
  35.  
  36. function editPost(req, res, next) {
  37.     const { postId } = req.params;
  38.     const { postText } = req.body;
  39.     const { _id: userId } = req.user;
  40.  
  41.     // if the userId is not the same as this one of the post, the post will not be updated
  42.     postModel.findOneAndUpdate({ _id: postId, userId }, { text: postText }, { new: true })
  43.         .then(updatedPost => {
  44.             if (updatedPost) {
  45.                 res.status(200).json(updatedPost);
  46.             }
  47.             else {
  48.                 res.status(401).json({ message: `Not allowed!` });
  49.             }
  50.         })
  51.         .catch(next);
  52. }
  53.  
  54. function deletePost(req, res, next) {
  55.     const { postId, themeId } = req.params;
  56.     const { _id: userId } = req.user;
  57.  
  58.     Promise.all([
  59.         postModel.findOneAndDelete({ _id: postId, userId }),
  60.         userModel.findOneAndUpdate({ _id: userId }, { $pull: { posts: postId } }),
  61.         themeModel.findOneAndUpdate({ _id: themeId }, { $pull: { posts: postId } }),
  62.     ])
  63.         .then(([deletedOne, _, __]) => {
  64.             if (deletedOne) {
  65.                 res.status(200).json(deletedOne)
  66.             } else {
  67.                 res.status(401).json({ message: `Not allowed!` });
  68.             }
  69.         })
  70.         .catch(next);
  71. }
  72.  
  73. function like(req, res, next) {
  74.     const { postId } = req.params;
  75.     const { _id: userId } = req.user;
  76.  
  77.     console.log('like')
  78.  
  79.     postModel.updateOne({ _id: postId }, { $addToSet: { likes: userId } }, { new: true })
  80.         .then(() => res.status(200).json({ message: 'Liked successful!' }))
  81.         .catch(next)
  82. }
  83.  
  84. module.exports = {
  85.     getLatestsPosts,
  86.     newPost,
  87.     createPost,
  88.     editPost,
  89.     deletePost,
  90.     like,
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement