-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
177 lines (162 loc) · 4.86 KB
/
database.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const mysql = require("mysql2");
// const connection = mysql.createConnection({
// host: process.env.DB_HOST,
// user: process.env.DB_USER,
// password: process.env.DB_PASSWORD,
// database: process.env.DB_NAME,
// port: 3306,
// });
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "ticket94",
port: 3306,
});
// Connect to the database
connection.connect((err) => {
if (err) throw err;
console.log("Connected to the database.");
createTables();
});
function createTables() {
const createSectionsTable = `
CREATE TABLE IF NOT EXISTS sections (
id INT AUTO_INCREMENT PRIMARY KEY,
section_name CHAR(10) NOT NULL,
price INT NOT NULL,
event_id BIGINT NOT NULL,
FOREIGN KEY (event_id) REFERENCES event_lists(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
`;
const createSeatingRowsTable = `
CREATE TABLE IF NOT EXISTS seating_rows (
id INT AUTO_INCREMENT PRIMARY KEY,
row_num INT NOT NULL,
section_id INT NOT NULL,
FOREIGN KEY (section_id) REFERENCES sections(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
`;
const createSeatsTable = `
CREATE TABLE IF NOT EXISTS seats (
id INT AUTO_INCREMENT PRIMARY KEY,
seat_num VARCHAR(10) NOT NULL,
status ENUM('V', 'T', 'R', 'I') NOT NULL DEFAULT 'V',
row_id INT NOT NULL,
member_id BIGINT NULL,
hold_expires_at DATETIME NULL,
order_number VARCHAR(16) NULL,
FOREIGN KEY (row_id) REFERENCES seating_rows(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (member_id) REFERENCES members(id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
`;
// Execute table creation queries in sequence
connection.query(createSectionsTable, (err, results) => {
if (err) throw err;
console.log("Sections table created.");
createSeatingRows();
});
function createSeatingRows() {
connection.query(createSeatingRowsTable, (err, results) => {
if (err) throw err;
console.log("Seating Rows table created.");
createSeats();
});
}
function createSeats() {
connection.query(createSeatsTable, (err, results) => {
if (err) throw err;
console.log("Seats table created.");
insertData();
});
}
}
async function insertData() {
try {
await new Promise((resolve, reject) => {
connection.query(
"ALTER TABLE sections AUTO_INCREMENT = 1",
(err, results) => {
if (err) return reject(err);
resolve();
}
);
});
await new Promise((resolve, reject) => {
connection.query(
"ALTER TABLE seating_rows AUTO_INCREMENT = 1",
(err, results) => {
if (err) return reject(err);
resolve();
}
);
});
const sections = [
{ name: "A", price: 4321, eventId: 8 }, // 示例:eventId 設為 1
{ name: "B", price: 4321, eventId: 8 },
{ name: "C", price: 3388, eventId: 8 },
{ name: "D", price: 3388, eventId: 8 },
];
const sectionIds = await Promise.all(
sections.map(({ name, price, eventId }) => {
return new Promise((resolve, reject) => {
connection.query(
"INSERT INTO sections (section_name, price, event_id) VALUES (?, ?, ?)",
[name, price, eventId],
(err, results) => {
if (err) return reject(err);
resolve(results.insertId);
}
);
});
})
);
// Insert rows 1-25 for each section
const rowIds = await Promise.all(
sectionIds.flatMap((sectionId) => {
return Array.from({ length: 25 }, (_, i) => i + 1).map((rowNumber) => {
return new Promise((resolve, reject) => {
connection.query(
"INSERT INTO seating_rows (row_num, section_id) VALUES (?, ?)",
[rowNumber, sectionId],
(err, results) => {
if (err) return reject(err);
resolve(results.insertId);
}
);
});
});
})
);
// Insert seats 1-20 for each row
await Promise.all(
rowIds.flatMap((rowId) => {
return Array.from({ length: 20 }, (_, i) => i + 1).map((seatNumber) => {
return new Promise((resolve, reject) => {
connection.query(
"INSERT INTO seats (seat_num, row_id) VALUES (?, ?)",
[seatNumber.toString().padStart(2, "0"), rowId],
(err, results) => {
if (err) return reject(err);
resolve();
}
);
});
});
})
);
console.log("Data inserted into tables.");
} catch (err) {
console.error("Error:", err);
} finally {
connection.end();
}
}