Social

// First, we'll create a database schema to store information about users, posts, and interactions const User = new Schema({ name: String, username: { type: String, unique: true }, password: String, followers: [{ type: Schema.Types.ObjectId, ref: 'User' }], following: [{ type: Schema.Types.ObjectId, ref: 'User' }] }); const Post = new Schema({ author: { type: Schema.Types.ObjectId, ref: 'User' }, content: String, likes: [{ type: Schema.Types.ObjectId, ref: 'User' }], comments: [{ author: { type: Schema.Types.ObjectId, ref: 'User' }, content: String }] }); // Next, we'll create routes for our app to handle creating and viewing content app.post('/api/posts', (req, res) => { // Create a new post with the provided content and the current user as the author const newPost = new Post({ author: req.user._id, content: req.body.content }); // Save the new post to the database and send a response newPost.save((err, post) => { if (err) { res.send(err); } else { res.json(post); } }); }); app.get('/api/posts', (req, res) => { // Retrieve a list of all posts, populated with their authors' information Post.find().populate('author').exec((err, posts) => { if (err) { res.send(err); } else { res.json(posts); } }); }); // Finally, we'll create routes for users to follow and interact with other users' content app.post('/api/follow/:username', (req, res) => { // Find the user to follow and add the current user to their followers list User.findOneAndUpdate({ username: req.params.username }, { $addToSet: { followers: req.user._id } }, (err, user) => { if (err) { res.send(err); } else { // Add the user to the current user's following list and send a response User.findOneAndUpdate({ _id: req.user._id }, { $addToSet: { following: user._id } }, (err, user) => { if (err) { res.send(err); } else { res.sendStatus(200); } }); } }); }); app.post('/api/unfollow/:username', (req, res) => { // Find the user to unfollow and remove the current user from their followers list User.findOneAndUpdate({ username: req.params.username }, { $pull: { followers: req.