|
| 1 | +const express = require("express"); |
| 2 | +const bodyParser = require("body-parser"); |
| 3 | +const app = express(); |
| 4 | +const http = require("http").Server(app); |
| 5 | +const io = require("socket.io")(http); |
| 6 | +const models = require('./models'); |
| 7 | +const dbConnection = require('./dbconnection'); |
| 8 | + |
| 9 | +var server = require('./server') |
| 10 | + |
| 11 | +app.use(bodyParser.json()); |
| 12 | +app.use(bodyParser.urlencoded({ extended: false })); |
| 13 | +app.use(express.static(__dirname)); |
| 14 | + |
| 15 | +let Chats = models.ChatsModel; |
| 16 | + |
| 17 | +// Connect to the database |
| 18 | +dbConnection.Connect(); |
| 19 | +server.init(); |
| 20 | + |
| 21 | +app.post("/chats", async (req, res) => { |
| 22 | + try { |
| 23 | + let chat = new Chats(req.body); |
| 24 | + await chat.save(); |
| 25 | + res.sendStatus(202).send(chat._id); |
| 26 | + io.emit("chat", req.body); |
| 27 | + } catch (error) { |
| 28 | + res.sendStatus(500); |
| 29 | + console.error(error); |
| 30 | + } |
| 31 | +}); |
| 32 | + |
| 33 | +// Post request on chats endpoind with ID parameter |
| 34 | +app.post("/chats/:id", async (req, res) => { |
| 35 | + try { |
| 36 | + let chatsFound; |
| 37 | + let chat = Chats.find({_id: req.params.id}, (error, chats) => { |
| 38 | + chatsFound = chats; |
| 39 | + }); |
| 40 | + if(chat){ |
| 41 | + // We found a chat with the supplied ID |
| 42 | + let user1 = search(chatsFound.user1ID, connections); |
| 43 | + let user2 = search(chatsFound.user2ID, connections); |
| 44 | + // Are both users currently online? |
| 45 | + if( user1 && user2 ){ |
| 46 | + // Then broadcast the message over sockets |
| 47 | + socket.broadcast.to(user1.socketID).emit('chat', req.body); |
| 48 | + socket.broadcast.to(user2.socketID).emit('chat', req.body); |
| 49 | + } |
| 50 | + } |
| 51 | + await Chats.findByIdAndUpdate( |
| 52 | + req.params.id, |
| 53 | + req.body, |
| 54 | + (err, chat) => { |
| 55 | + if(err) return res.status(500).send(err); |
| 56 | + return res.send(chat); |
| 57 | + } |
| 58 | + ) |
| 59 | + } catch (error) { |
| 60 | + res.sendStatus(500) |
| 61 | + console.error(error) |
| 62 | + } |
| 63 | +}); |
| 64 | + |
| 65 | +app.get("/chats", (req, res) => { |
| 66 | + Chats.find({}, (error, chats) => { |
| 67 | + res.send(chats) |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +app.get("/chats/:id", (req, res) => { |
| 72 | + Chats.find({ _id: req.params.id}, (error, chats) => { |
| 73 | + console.log(req.params.id); |
| 74 | + console.log(chats); |
| 75 | + res.sendStatus(202).send(chats); |
| 76 | + res.send(chats) |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +app.delete("/chats/:id", (req, res) => { |
| 81 | + Chats.deleteOne({ _id: req.params.id}, (error, chats) => { |
| 82 | + if(error){ |
| 83 | + return res.status(error.statusCode).send({ |
| 84 | + message: res.statusCode |
| 85 | + }); |
| 86 | + } |
| 87 | + return res.status(202); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +process.on('exit', () => { |
| 92 | + dbConnection.Disconnect(); |
| 93 | +}); |
| 94 | + |
| 95 | +function search(nameKey, myArray){ |
| 96 | + for (var i=0; i < myArray.length; i++) { |
| 97 | + if (myArray[i].name === nameKey) { |
| 98 | + return myArray[i]; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
0 commit comments