Guest User

Untitled

a guest
Feb 3rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. const mongoose = require('mongoose');
  2.  
  3. const UserSchema = new mongoose.Schema({
  4. username: String,
  5. password : String,
  6. rooms : [
  7. {
  8. type : mongoose.Schema.Types.ObjectId,
  9. ref : "Room"
  10. }
  11. ]
  12. });
  13.  
  14. module.exports = mongoose.model("User" , UserSchema);
  15.  
  16. const mongoose = require('mongoose');
  17.  
  18. const ChatRoomSchema = new mongoose.Schema({
  19. name : String,
  20. description : String,
  21. owner : {
  22. type : mongoose.Schema.Types.ObjectId,
  23. ref : "User"
  24. },
  25. messages : [
  26. {
  27. type : mongoose.Schema.Types.ObjectId,
  28. ref : "Message"
  29. }
  30. ]
  31. });
  32.  
  33. module.exports = mongoose.model("Room" , ChatRoomSchema);
  34.  
  35. router.post('/new', middleware.ensureAuthenticated, (req, res) => {
  36. const user_id = req.user._id;
  37. let newChat = {
  38. name: req.body.name,
  39. description: req.body.description,
  40. owner: req.user
  41. };
  42. }
  43. ChatRoom.count({name: req.body.name}, (err, count) => {
  44. if (count === 0) {
  45. //add room
  46. ChatRoom.create(newChat, (err, createdChatRoom) => {
  47. if (err) {
  48. res.json({
  49. success: false,
  50. data: {
  51. header: 'Something Went Wrong',
  52. message: err,
  53. }
  54. })
  55. }
  56. else {
  57. User.findOne({_id: user_id},(err, foundUser) => {
  58. if (err) {
  59.  
  60. } else {
  61. foundUser.rooms.push(createdChatRoom);
  62. foundUser.save();
  63. res.json({
  64. success: true,
  65. data: createdChatRoom
  66. })
  67. }
  68. });
  69. }
  70. })
  71.  
  72. } else {
  73. res.json({
  74. success: false,
  75. data: {
  76. header: req.body.name + " already exist",
  77. message: "Please try another name for your room"
  78. }
  79. })
  80. }
  81. });
  82.  
  83.  
  84. });
Add Comment
Please, Sign In to add comment