-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (64 loc) · 2.05 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const path = require('path');
// Initialize seat availability
const totalSeats = 80;
const seatsPerRow = 7;
const rows = Math.floor(totalSeats / seatsPerRow);
const seats = Array(rows)
.fill()
.map(() => Array(seatsPerRow).fill(true));
// Configure Express to parse request body
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Serve static files from the "public" directory
app.use(express.static(path.join(__dirname, 'public')));
// Reserve seats endpoint
app.post('/reserve', (req, res) => {
const numSeats = parseInt(req.body.seats);
if (isNaN(numSeats) || numSeats <= 0) {
return res.status(400).json({ message: 'Invalid number of seats' });
}
const seatNumbers = [];
for (let row = 0; row < rows; row++) {
let consecutiveSeats = 0;
for (let seat = 0; seat < seatsPerRow; seat++) {
if (seats[row][seat]) {
consecutiveSeats++;
if (consecutiveSeats === numSeats) {
for (let i = seat - numSeats + 1; i <= seat; i++) {
seats[row][i] = false;
seatNumbers.push(`Row ${row + 1}, Seat ${i + 1}`);
}
return res.json({ seats: seatNumbers });
}
} else {
consecutiveSeats = 0;
}
}
}
return res.status(404).json({ message: 'Seats not available' });
});
// Get all seats availability endpoint
app.get('/seats', (req, res) => {
const seatStatus = seats.map((row, rowIndex) => {
return row.map((seat, seatIndex) => {
return {
seatNumber: `Row ${rowIndex + 1}, Seat ${seatIndex + 1}`,
available: seat,
};
});
});
res.json({ seats: seatStatus });
});
// Serve static files from the "public" directory
app.use(express.static(path.join(__dirname, 'public')));
// Serve the index.html file
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Start the server
app.listen(3000, () => {
console.log('Server is running on port 3000');
});