Post related Api
//search post
router.get(’/search’,async (req,res)=>{
if (!req.query.caption && !req.query.tag) {
return res.status(400).json(‘Please enter either title of a tag’)
}
let posts=;
if (req.query.caption) {
const regex = new RegExp(req.query.caption, “i”);
posts = await Post.find({ caption: regex });
}
if (req.query.tag) {
posts = posts.concat([await Post.find({ tags: req.query.tag })]);
}
res.json(posts)
});
//toggle save
router.get(’:/id/toggleSave’,async (req,res)=>{
const post = await Post.findById(req.params.id);
if (!post) {
return res.status(404).json(‘Post not found’)
}
const { user } = req;
if (user.savedPosts.includes(req.params.id)) {
console.log(“removing saved post”);
await User.findByIdAndUpdate(user.id, {
$pull: { savedPosts: req.params.id },
});
} else {
console.log(“saving post”);
await User.findByIdAndUpdate(user.id, {
$push: { savedPosts: req.params.id },
});
}
res.json(post);
})
PROFILE RELATED API:=>
//@route Get api/profile
//does get all profiles
//@acess Public
router.get("/",async (req,res)=>{
try {
let profiles = await Profile.find().lean().exec();
profiles.forEach((profile) => {
profile.isFollowing = false;
const followers = profile.followers.map((follower) => follower._id.toString());
if (followers.includes(req.profile.id)) {
profile.isFollowing = true;
}
});
profiles = profiles.filter((profile) => profile._id.toString() !== req.profile.id);
res.json(profiles);
} catch (err) {
console.error(err.message);
res.status(500).send('Server error')
}
});
//@route Get api/profile
//does get profiles by user id
//@acess Private
router.get("/user/:user_id",async (req,res)=>{
try {
const profile=await Profile.findOne({user:req.params.user_id})(‘user’,
[‘name’,‘avatar’,‘followers’,‘following’,‘posts’]);
if(!profile) return res.status(400).json({msg:"There is no profile for this user"});
//otherwise
else
//show follow following button
profile.isFollowing = false;
const followers = profile.followers.map((follower) => follower._id.toString());
profile.followers.forEach((follower) => {
follower.isFollowing = false;
if (req.profile.following.includes(follower._id.toString())) {
follower.isFollowing = true;
}
});
profile.following.forEach((profile) => {
profile.isFollowing = false;
if (req.profile.following.includes(profile._id.toString())) {
profile.isFollowing = true;
}
});
if (followers.includes(req.profile.id)) {
profile.isFollowing = true;
}
profile.isMe = req.profile.id === profile._id.toString();
res.json(profile);
} catch (err) {
console.error(err.message);
if(err.kind==='ObjectId'){
return res.status(400).json({msg:"Profile not found"});
}
res.status(500).send('Server error')
}
});
//follow and following
router.put('/:id/follow',async (req,res)=>{
const user = await User.findById(req.params.id);
if (!user) {
return res.status(404).json('User not found');
}
// make the sure the user is not the logged in user
if (req.params.id === req.user.id) {
return res.status(400).json("You can't unfollow/follow yourself")
}
// only follow if the user is not following already
if (user.followers.includes(req.user.id)) {
return res.status(400).json('User already followed')
}
await User.findByIdAndUpdate(req.params.id, {
$push: { followers: req.user.id },
$inc: { followersCount: 1 },
});
await User.findByIdAndUpdate(req.user.id, {
$push: { following: req.params.id },
$inc: { followingCount: 1 },
});
res.json(user);
});
//unfollow
router.put(’:/id/unfollow’,async (req,res)=>{
const user = await User.findById(req.params.id);
if (!user) {
return res.status(404).json('User not found')
}
// make the sure the user is not the logged in user
if (req.params.id === req.user.id) {
return next({ message: "You can't follow/unfollow yourself", status: 400 });
}
await User.findByIdAndUpdate(req.params.id, {
$pull: { followers: req.user.id },
$inc: { followersCount: -1 },
});
await User.findByIdAndUpdate(req.user.id, {
$pull: { following: req.params.id },
$inc: { followingCount: -1 },
});
})