| 0 | kirasiris | Categories: Tags: , , , , , , , ,

NodeJS s3

Follow/Unfollow System

// @route       GET api/v1/users/follow/:userId
// @description Follow user by userId
// @access      Private
// @task        DONE
exports.addFollowing = asyncHandler(async (req, res, next) => {
  const following = await YOURMODEL.findByIdAndUpdate(req.user.id, {
    $push: { following: req.params.userId }
  });
  res.status(200).json({ success: true, data: following });
  next();
});

// @route       GET api/v1/users/follow/:userId
// @description Follow user by userId
// @access      Private
// @task        DONE
exports.addFollower = asyncHandler(async (req, res, next) => {
  const follower = await YOURMODEL.findByIdAndUpdate(
    req.params.userId,
    { $push: { followers: req.user.id } },
    { new: true }
  );
  res.status(200).json({ success: true, data: follower });
});

// @route       GET api/v1/users/unfollow/:userId
// @description Unfollow user by userId
// @access      Private
// @task        DONE
exports.removeFollowing = asyncHandler(async (req, res, next) => {
  const following = await YOURMODEL.findByIdAndUpdate(req.user.id, {
    $pull: { following: req.params.userId }
  });
  res.status(200).json({ success: true, data: following });
  next();
});

// @route       GET api/v1/users/unfollow/:userId
// @description Unfollow user by userId
// @access      Private
// @task        DONE
exports.removeFollower = asyncHandler(async (req, res, next) => {
  const follower = await YOURMODEL.findByIdAndUpdate(
    req.params.userId,
    { $pull: { followers: req.user.id } },
    { new: true }
  );
  res.status(200).json({ success: true, data: follower });
});

Posts by Users Followed by loggedInUser with Pagination

// @desc    Get all posts by friends
// @route   GET /api/v1/timeline
// @access  Private
// @status  DONE
exports.getPostsByFriends = asyncHandler(async (req, res, next) => {
  // Pagination
  const page = parseInt(req.query.page, 10) || 1;
  const limit = parseInt(req.query.limit, 10) || 25;
  const startIndex = (page - 1) * limit;
  const endIndex = page * limit;

  const { following } = req.user;
  // Include currentLoggedIn user posts
  following.push(req.user._id);

  // Fetch posts by following users
  const posts = await YOURMODEL.find({
    // user: { $in: following.map(post => post._id) }
    user: { $in: following }
  })
    .populate({ path: 'user', select: 'username avatar bio' })
    .sort('-date')
    .skip(startIndex)
    .limit(limit);

  const total = await YOURMODEL.countDocuments(posts);

  // Executing query
  const results = await posts;

  // Pagination result
  const pagination = {
    current: page,
    totalpages: total
  };

  if (endIndex < total) {
    pagination.next = {
      page: page + 1,
      limit
    };
  }

  if (startIndex > 0) {
    pagination.prev = {
      page: page - 1,
      limit
    };
  }

  // Send response
  res.status(201).json({
    success: true,
    requestedAt: req.requestTime,
    count: results.length,
    pagination,
    data: results
  });
});

Related Posts

// @desc    Get all related posts according to :id
// @route   GET /api/v1/posts/:id/related
// @access  Private
// @status  DONE
exports.getRelatedProducts = asyncHandler(async (req, res, next) => {
  const product = await YOURMODEL.findById(req.params.id);

  if (!req.params.id.match(/^[0-9a-fA-F]{24}$/) || !product) {
    return next(
      new ErrorResponse(`Document not found with id of ${req.params.id}`, 404)
    );
  }

  const related = await YOURMODEL.find({
    _id: { $ne: req.params.id },
    category: req.query.category
  }).limit(6);

  if (!related) {
    return next(
      new ErrorResponse(
        `No documents related to id of ${req.params.id} found`,
        404
      )
    );
  }

  res.status(200).json({ success: true, data: related });
});

TO KEEP ADDING…

Bye-Bye

Leave a Reply

Back to Top