Last active
June 14, 2020 22:41
-
-
Save superirale/dbc834333c3477ac515125fdfccc02c7 to your computer and use it in GitHub Desktop.
io_bug_fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BookingServices = require('../services/bookingServices'); | |
exports.getAdminNotification = async (req, res, next) => { | |
try{ | |
const bookedRoom = await BookingServices.getAdminNotification(); | |
return res.status(200).json(bookedRoom); | |
}catch(err){ | |
return res.status(500).json({ body: err.message }); | |
} | |
}; | |
// I have forgotten how to do multiple async | |
// you need to refactor this part | |
// the goodnews is that you can access io now | |
exports.getBookings = async (io) => { | |
console.log(io) | |
return async (req, res, next) => { | |
try{ | |
const bookings = await BookingServices.getBookings({}); | |
io.emit("sendNotif", {}); //emit here | |
return res.status(200).json(bookings); | |
}catch(err){ | |
return res.status(500).json({ body: err.message }); | |
} | |
}; | |
} | |
exports.getABooking = async (req, res, next) => { | |
try{ | |
const { id } = req.params; | |
const booking = await BookingServices.getABooking(id); | |
return res.status(201).json(booking); | |
}catch(err){ | |
return res.status(500).json({ body: err.message }); | |
} | |
}; | |
exports.createBooking = async (req, res, next) => { | |
try{ | |
const data = req.body; | |
const newBooking = await BookingServices.createBooking(data); | |
return res.status(200).json(newBooking); | |
}catch(err){ | |
return res.status(500).json({ body: err.message }); | |
} | |
}; | |
exports.updateBooking = async (req, res, next) => { | |
try{ | |
const { id } = req.params; | |
const data = req.body; | |
const updatedBooking = await BookingServices.updateBooking(id, data); | |
return res.status(201).json(updatedBooking); | |
}catch(err){ | |
return res.status(500).json({ body: err.message }); | |
} | |
}; | |
exports.deleteBooking = async (req, res, next) => { | |
try{ | |
const { id } = req.params; | |
const bookingToDelete = await BookingServices.deleteBooking(id); | |
return res.status(200).json(bookingToDelete); | |
}catch(err){ | |
return res.status(500).json({ body: err.message }); | |
} | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const mongoose = require('mongoose'); | |
// const events = require('events'); | |
const port = process.env.PORT || 3030; | |
const app = require('./index.js'); | |
// const socketHandler = require('./globals.js'); | |
const BookingControllers = require('./controllers/bookingControllers'); | |
app.set('port', port); | |
const server = http.createServer(app); | |
const io = require('socket.io')(server); | |
// global.io = io; | |
let MONGO_URL = 'mongodb://localhost:27017/'; | |
mongoose.connect(MONGO_URL, { useUnifiedTopology: true, useNewUrlParser: true }); | |
const db = mongoose.connection; | |
db.on('error', () => console.log('error connecting to database')); | |
db.once('open', () => console.log('connection established')); | |
io.on("connection", socket => { | |
console.log("client conneected"), | |
socket.on("newData", data => { | |
io.emit("sendNotif", data); | |
}); | |
socket.on("disconect", () => console.log("client disconnected")); | |
}); | |
io.sockets.on('connection', BookingControllers.getBookings) | |
// socketHandler.configure(server); | |
server.listen(port, (err, response) => { | |
if (err) { | |
console.log(err); | |
} else { | |
console.log(`Asb test server is listening at: ${port}`); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment