Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require('mongoose');
- const UserSchema = new mongoose.Schema({
- username: String,
- password : String,
- rooms : [
- {
- type : mongoose.Schema.Types.ObjectId,
- ref : "Room"
- }
- ]
- });
- module.exports = mongoose.model("User" , UserSchema);
- const mongoose = require('mongoose');
- const ChatRoomSchema = new mongoose.Schema({
- name : String,
- description : String,
- owner : {
- type : mongoose.Schema.Types.ObjectId,
- ref : "User"
- },
- messages : [
- {
- type : mongoose.Schema.Types.ObjectId,
- ref : "Message"
- }
- ]
- });
- module.exports = mongoose.model("Room" , ChatRoomSchema);
- router.post('/new', middleware.ensureAuthenticated, (req, res) => {
- const user_id = req.user._id;
- let newChat = {
- name: req.body.name,
- description: req.body.description,
- owner: req.user
- };
- }
- ChatRoom.count({name: req.body.name}, (err, count) => {
- if (count === 0) {
- //add room
- ChatRoom.create(newChat, (err, createdChatRoom) => {
- if (err) {
- res.json({
- success: false,
- data: {
- header: 'Something Went Wrong',
- message: err,
- }
- })
- }
- else {
- User.findOne({_id: user_id},(err, foundUser) => {
- if (err) {
- } else {
- foundUser.rooms.push(createdChatRoom);
- foundUser.save();
- res.json({
- success: true,
- data: createdChatRoom
- })
- }
- });
- }
- })
- } else {
- res.json({
- success: false,
- data: {
- header: req.body.name + " already exist",
- message: "Please try another name for your room"
- }
- })
- }
- });
- });
Add Comment
Please, Sign In to add comment