-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (54 loc) · 1.75 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const express = require('express')
const dotenv = require('dotenv')
const app = express()
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
const mongoose = require('mongoose')
mongoose.Promise = global.Promise
dotenv.config({ path: './config.env' })
const DB = process.env.DATABASE.replace('<PASSWORD>', process.env.DATABASE_PASSWORD)
app.use(express.static(__dirname + "/public/"))
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
mongoose.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
}).then(con => {
console.log('DB connection successful')
})
const nameSchema = new mongoose.Schema({
seatNo: Number,
status: String
})
const Seat = mongoose.model('Seat', nameSchema)
app.get('/bookSeat', (req, res) => {
async function displaySeats() {Seat.find({ status:"vacant"}, "seatNo status", (err, seats) => {
for(var i=1; i<=seats.length; i++) {
console.log("Room no. " + seats[i-1].seatNo + " is available")
}
})
}
displaySeats()
})
app.post('/bookSeat', (req, res) => {
async function bookSeat() {rl.question("these rooms are empty which room do you want to book? ", function(number) {
console.log(`We are booking room number ${number} for you`)
Seat.updateOne({seatNo:number},{ $set: {status:"booked"}}, function(err, res) {
if(err) throw err
console.log("your room has been booked!")
})
rl.close()
})
}
bookSeat()
})
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`Server is listening on port ${port}...`)
})